django-pgmigrate is a Python library that integrates with Django to minimize downtime during PostgreSQL database migrations. It addresses a common issue where long-running transactions can block `python manage.py migrate` operations, causing subsequent queries to queue up. Version 1.6.0 is the current release, and the project is actively maintained with a regular release cadence to support new Django and Python versions. It automatically detects and terminates blocking queries by default, or can be configured to merely show them or set a lock timeout.
pip install django-pgmigrateVerified import paths — ran on the pinned version, not inferred.
After installing the library, add `pgactivity`, `pglock`, and `pgmigrate` to your `INSTALLED_APPS` in `settings.py`. Then, simply run `python manage.py migrate` as you normally would. `django-pgmigrate` will automatically detect and terminate blocking queries by default.
Upgrade Python to 3.10 or later before upgrading django-pgmigrate to versions >= 1.5.0.
Upgrade Django and PostgreSQL to supported versions (e.g., Django 4.2+ and PostgreSQL 14+ for 1.6.0) before upgrading django-pgmigrate.
Add 'pgactivity', 'pglock', and 'pgmigrate' to your `INSTALLED_APPS` list in `settings.py`.
Set `PGMIGRATE_BLOCKING_ACTION = 'pgmigrate.Show'` or `PGMIGRATE_BLOCKING_ACTION = None` in `settings.py` to change default behavior. You can also define custom actions.
Design migrations to be as small and atomic as possible to minimize potential mid-failure states. Be prepared for manual database inspection and repair if a non-atomic migration fails.
Set `PGMIGRATE_PATCH_MIGRATE = False` in `settings.py` to disable the patching. You will then need to use `python manage.py pgmigrate` instead of `python manage.py migrate`.