Skip to content

Connecting to Postgres

Before we can create any tables, we need a database to create them in. We'll use Postgres, running in a Docker container.

Start the Container

Our project describes the container in a file named compose.yaml, provided in this chapter's starting branch. Start the container:

shell
docker compose up -d --wait

The DATABASES Setting

Django connects to the database named in the DATABASES setting, and startproject set ours to a SQLite file. Open config/settings.py and replace that setting:

config/settings.py
# The values match the db service in compose.yaml.
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "rescuescout",
        "USER": "rescuescout",
        "PASSWORD": "rescuescout",
        "HOST": "localhost",
        "PORT": "5432",
    }
}

ENGINE picks the database backend. Django's Postgres backend connects through psycopg, a driver package that our project doesn't have yet. Add it:

shell
uv add "psycopg[binary]~=3.3.6"

Info

These values are hardcoded in settings.py in development. In prod, read them from environment variables.