Django PG Zero Downtime Migrations

0.19 · active · verified Thu Apr 16

Django postgresql backend that applies migrations with respect to database locks, enabling zero downtime during schema changes. It modifies the standard Django PostgreSQL backend to minimize locks during schema and `RunSQL` operations, helping to ensure continuous application availability during deployments. The library is actively maintained, with frequent releases to support new Django, Python, and PostgreSQL versions, and is currently at version 0.19.

Common errors

Warnings

Install

Imports

Quickstart

To enable zero-downtime migrations, configure your Django `DATABASES` setting to use the library's PostgreSQL backend. Additionally, it's highly recommended to set specific timeouts and enable raising errors for unsafe operations to enforce zero-downtime principles.

import os

DATABASES = {
    'default': {
        'ENGINE': 'django_pg_zero_downtime_migrations.backends.postgres',
        'NAME': os.environ.get('DB_NAME', 'your_database_name'),
        'USER': os.environ.get('DB_USER', 'your_user'),
        'PASSWORD': os.environ.get('DB_PASSWORD', 'your_password'),
        'HOST': os.environ.get('DB_HOST', 'localhost'),
        'PORT': os.environ.get('DB_PORT', '5432'),
    }
}

# Recommended settings for zero-downtime operations
ZERO_DOWNTIME_MIGRATIONS_LOCK_TIMEOUT = '2s'
ZERO_DOWNTIME_MIGRATIONS_STATEMENT_TIMEOUT = '2s'
ZERO_DOWNTIME_MIGRATIONS_FLEXIBLE_STATEMENT_TIMEOUT = True
ZERO_DOWNTIME_MIGRATIONS_RAISE_FOR_UNSAFE = True

# For Django < 5.0 when adding columns with code defaults
# ZERO_DOWNTIME_MIGRATIONS_KEEP_DEFAULT = True

view raw JSON →