Getting StartedConnect your framework
Django
Django's standard Postgres settings, pointed at CapyDB.
Environment
DATABASE_URL="postgres://user:password@host:6432/db?sslmode=require"
DATABASE_DIRECT_URL="postgres://user:password@host:5432/db?sslmode=require"Settings
With dj-database-url (or django-environ):
import os
import dj_database_url
DATABASES = {
"default": dj_database_url.config(
env="DATABASE_URL",
conn_max_age=0, # let the server-side pooler do the pooling
ssl_require=True,
)
}Or spelled out:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "db",
"USER": "user",
"PASSWORD": "password",
"HOST": "host",
"PORT": "6432",
"OPTIONS": {"sslmode": "require"},
}
}Migrations
Run manage.py migrate with the direct URL — easiest is an env override:
DATABASE_URL="$DATABASE_DIRECT_URL" python manage.py migratePitfalls
conn_max_age+ transaction pooling: persistent Django connections on the pooled port add little and can pin pooler slots;conn_max_age=0is the safe default. If you prefer Django-side persistent connections, use the direct URL and mind the plan's connection budget.CONN_HEALTH_CHECKS: enable it if you see stale-connection errors after idle periods.- Server-side cursors (
QuerySet.iterator()) need session state; if you rely on them heavily, run those code paths against the direct URL or setDISABLE_SERVER_SIDE_CURSORS = True.