question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting `AssertionError: DatabaseBackend is not running` for Postgres

See original GitHub issue

Describe the bug I used the sample code from README to connect to a sqlite DB and query items and it worked fine (See https://github.com/mei-li/tsougkrizo/blob/persistency/persistence.py). When I switched to a Postgres DB the schema initialisation worked, but when I query the items I get:

  File "./main.py", line 139, in websocket_host
    game = await game_manager.get_game(game_id)
  File "./main.py", line 82, in get_game
    game = await Game.objects.get(uuid=game_id)
  File "/home/olkos/.pyenv/versions/3.8.2/envs/tsougkrizo/lib/python3.8/site-packages/ormar/queryset/queryset.py", line 829, in get
    return await self.filter(*args, **kwargs).get()
  File "/home/olkos/.pyenv/versions/3.8.2/envs/tsougkrizo/lib/python3.8/site-packages/ormar/queryset/queryset.py", line 845, in get
    rows = await self.database.fetch_all(expr)
  File "/home/olkos/.pyenv/versions/3.8.2/envs/tsougkrizo/lib/python3.8/site-packages/databases/core.py", line 139, in fetch_all
    async with self.connection() as connection:
  File "/home/olkos/.pyenv/versions/3.8.2/envs/tsougkrizo/lib/python3.8/site-packages/databases/core.py", line 219, in __aenter__
    await self._connection.acquire()
  File "/home/olkos/.pyenv/versions/3.8.2/envs/tsougkrizo/lib/python3.8/site-packages/databases/backends/postgres.py", line 148, in acquire
    assert self._database._pool is not None, "DatabaseBackend is not running"
AssertionError: DatabaseBackend is not running

To Reproduce Steps to reproduce the behavior:

  1. Go to https://github.com/mei-li/tsougkrizo/tree/persistency (only persistence.py and the sample url /demo_ormar_issue in main.py matter)
  2. Create and install environment of the project with python 3.8
  3. Export an existing postgres DB url eg. export DATABASE_URL=postgresql://dev:test@localhost/egg_db
  4. Run python create_db.py # this works and creates the DB schema
  5. Run uvicorn main:app --reload
  6. Click on http://127.0.0.1:8000/demo_ormar_issue
  7. See error

(Note: this should be a complete and concise piece of code that allows reproduction of an issue)

Expected behavior I expect the query to objects in the DB to work without this error

Versions (please complete the following information):

  • Database backend used (mysql/sqlite/postgress)
  • Python version 3.8.2
  • ormar version 0.10.4
  • pydantic version 1.8
  • if applicable fastapi version 0.54.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
collerekcommented, Apr 24, 2021

For all backends except sqlite you need to connect the database to use it.

You use fastapi so you should follow example in fastapi section of the ormar documentation. Basically you need to add connect and disconnect to fastapi instance events.

@app.on_event("startup")
async def startup() -> None:
    database_ = app.state.database
    if not database_.is_connected:
        await database_.connect()


@app.on_event("shutdown")
async def shutdown() -> None:
    database_ = app.state.database
    if database_.is_connected:
        await database_.disconnect()

As described in https://github.com/collerek/ormar/blob/master/docs/fastapi.md#database-connection

0reactions
ppostnovcommented, Nov 22, 2022

For all backends except sqlite you need to connect the database to use it.

You use fastapi so you should follow example in fastapi section of the ormar documentation. Basically you need to add connect and disconnect to fastapi instance events.

@app.on_event("startup")
async def startup() -> None:
    database_ = app.state.database
    if not database_.is_connected:
        await database_.connect()


@app.on_event("shutdown")
async def shutdown() -> None:
    database_ = app.state.database
    if database_.is_connected:
        await database_.disconnect()

As described in https://github.com/collerek/ormar/blob/master/docs/fastapi.md#database-connection

Thanks a lot !

Read more comments on GitHub >

github_iconTop Results From Across the Web

AssertionError: DatabaseBackend is not running
So I have tried this: import databases @app.post("/computers/", response_model=Computer) async def create_computer(computer: ComputerIn): ...
Read more >
tiangolo/fastapi - Gitter
Need help! Anybody got the issue about AssertionError: DatabaseBackend is not running? I encountered this problem when I use testClient for unit testing....
Read more >
Database Backend Is Not Running - Nagios Support
Commercial Support Clients: Clients with support contracts can get escalated support assistance by visiting Nagios Answer Hub. These forums are ...
Read more >
Testing a Database - FastAPI
... client.get(f"/users/{user_id}") assert response.status_code == 200, response.text data = response.json() assert data["email"] == "deadpool@example.com" ...
Read more >
[Solved]-'django.db.backends.posgresql_psycopg2' isn't an ...
Coding example for the question 'django.db.backends.posgresql_psycopg2' isn't an available database backend-postgresql.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found