Connecting to the database
We have chosen to store the data the app needs in a PostgreSQL database, which we will need to connect to. To do this, I like to use the Quart extension called Quart-DB, which is a great wrapper around fast lower-level PostgreSQL drivers. It is installed by running the following command in the backend directory:
pdm add quart-db
We can activate QuartDB
by adding the following code to backend/src/backend/run.py:
from quart_db import QuartDB quart_db = QuartDB(app)
We also need to configure which database QuartDB
should connect to. This is achieved by adding a TOZO_QUART_DB_DATABASE_URL
environment variable, the value of which is constructed as follows, with the highlighted parts being configurable:
postgresql://username:password@0.0.0.0:5432/db_name
We’ll use tozo
for the username, password, and database name in development as they are very obvious and easy to remember. To do this, add the following to backend/development.env: