Skip to content

Django

Tested with: Django 5.1 · psycopg 3.2 · mysqlclient 2.2 · django-redis 5.4 · Python 3.12

settings.py
import os
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"HOST": "pg-7f3k.us-east.databasezy.com",
"PORT": 5432,
"NAME": "app",
"USER": "app",
"PASSWORD": os.environ["ZB_PASSWORD"],
"CONN_MAX_AGE": 300, # persistent connections per worker
"CONN_HEALTH_CHECKS": True, # survive free-tier sleeps and rotations
"OPTIONS": {
"sslmode": "verify-full",
"sslrootcert": os.environ.get("ZB_CA", "databasezy-ca.pem"),
},
}
}
settings.py
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": "rediss://app:%s@valkey-7f3k.us-east.databasezy.com:6379/0" % os.environ["ZB_VALKEY_PASSWORD"],
"OPTIONS": {"ssl_cert_reqs": "required", "ssl_ca_certs": os.environ.get("ZB_CA", "databasezy-ca.pem")},
}
}
SESSION_ENGINE = "django.contrib.sessions.backends.cache"

python manage.py migrate --noinput in the release step (Kubernetes Job, Render preDeployCommand, Fly release_command). Keep CONN_MAX_AGE × workers under the size’s max_connections; with gunicorn --workers 4 --threads 2 that is up to 8 connections per replica.