Articles

Host Django and Postgres with safe pre-traffic migrations and rollback

Run Gunicorn, separate read/write DATABASE_URL from pre-traffic MIGRATION_DATABASE_URL, and recover code plus PostgreSQL state together on Gregale.

Updated

Host Django with managed PostgreSQL on a platform that runs manage.py migrate inside a pre-traffic release command with a separate migration role and lets you roll back application code and database state together. On Gregale, Django apps auto-detect manage.py and wsgi.py, separate runtime DATABASE_URL (no DDL privileges) from MIGRATION_DATABASE_URL, and recover from bad schema changes with gregale rollback --restore-databases.

Why Django migrations break standard rollbacks

Reverting Python code after a destructive migrate (DROP COLUMN, non-null alterations, or backfills) leaves your database schema ahead of the old release. Most PaaS platforms fix only half the problem:

  • Pre-traffic release commands (release:). On Gregale, declared release commands run in a disposable one-off microVM before Gunicorn instances start receiving production traffic. If python manage.py migrate --noinput fails, traffic stays on the healthy release.
  • Least-privilege DB credentials. DATABASE_URL connects through a transaction-mode pooler with data-only access; schema changes use a scoped MIGRATION_DATABASE_URL granted only during the release hook.
  • Coordinated code + PITR database recovery. Paid plans maintain a 7-day continuous WAL window so gregale rollback --restore-databases clones Postgres to the timestamp immediately before the faulty migration and rebinds the rolled-back release cleanly.

Configure Django for Gregale + Gunicorn

Add gunicorn, dj-database-url, and psycopg[binary] to requirements.txt, and configure Procfile and settings.py:

text
# Procfile
web: gunicorn mysite.wsgi:application --bind 0.0.0.0:$PORT --workers 2
release: python manage.py migrate --noinput
python
# settings.py
import os
import dj_database_url

SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
DEBUG = os.environ.get("DJANGO_DEBUG") == "1"
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS", "").split(",")

DATABASES = {
    "default": dj_database_url.parse(
        os.environ.get("MIGRATION_DATABASE_URL") or os.environ["DATABASE_URL"],
        conn_max_age=0,
    )
}
DATABASES["default"]["DISABLE_SERVER_SIDE_CURSORS"] = True

Provision PostgreSQL with separated runtime and migration bindings:

bash
gregale deploy --create-only --name my-django-app
gregale add postgres --app my-django-app --region <region>
gregale postgres attach my-django-app-postgres my-django-app --access migration --env MIGRATION_DATABASE_URL
gregale env set --app my-django-app DJANGO_SECRET_KEY="$(python -c 'import secrets; print(secrets.token_urlsafe(50))')" DJANGO_ALLOWED_HOSTS="my-django-app.gregale.app"
gregale deploy --name my-django-app

You can also inspect how coordinated code + database rollback behaves in a real repository:

How Django hosting compares on migrations and rollbacks

PlatformPre-traffic migrate hookSeparate migration DB roleCode rollback + DB PITR togetherIdle billing behavior
Gregalerelease: / release hookBuilt-in (--access migration)Single command (--restore-databases)Pauses ~1s after last request
HerokuProcfile release: phaseManual credentialsSeparate app + PG CLI stepsEco sleeps; Basic runs 24/7
RenderpreDeployCommandManual permissionsManual rollback + new DB restorePaid services run 24/7
RailwaypreDeployCommandManual permissionsManual rollback + sibling restoreServerless sleeps when quiet
Fly.iofly.toml release_commandManual permissionsImage redeploy + cluster restoreAuto-stop/start on traffic

Stay on Heroku or Render when you depend on existing add-on ecosystems or fixed monthly worker allocations. Choose Gregale when you want least-privilege migration roles by default, automatic 1%→10%→50%→100% 5xx canary rollbacks, and coordinated code + Postgres point-in-time recovery. See Deploy a Django app, Rollback code and database together, and PaaS rollback & recovery matrix.

Questions

How does Gregale separate Django runtime and migration database credentials?

Runtime instances receive pooled DATABASE_URL with data-only privileges and DISABLE_SERVER_SIDE_CURSORS enabled, while pre-traffic release commands receive MIGRATION_DATABASE_URL scoped to schema migrations.

What if a Django migration breaks production after deploy?

Production releases step through 1%, 10%, 50% and 100% traffic and stop automatically on 5xx errors without crossing a migration boundary, while paid plans let you restore code and Postgres together via PITR.

Do I need to configure ALLOWED_HOSTS for Gregale deploys?

Yes. Include your default gregale.app domain and any custom domains in DJANGO_ALLOWED_HOSTS before deploying so health and post-deploy verification requests succeed.