Skip to main content

Generate migrations between Postgres databases

Project description

image image image

Generate migrations between Postgres databases.

Use pgmig to compare the structure of two Postgres databases — a source and a target — and generate the SQL that turns the source into the target.

pgmig connects read-only to both databases and never runs the generated SQL for you: you review it and apply it yourself.

pgmig officially supports Postgres 14–18 — the majors currently maintained upstream — and is tested against each in CI. Other versions may work but are not tested.

This project is currently in active development, see Roadmap.

Table of Contents

  1. Getting Started
  2. Configuration
  3. FAQ
  4. Contributing
  5. License

Getting Started

Installation

pgmig is available as pgmig on PyPI.

Invoke pgmig directly with uvx:

uvx pgmig generate \
  --source postgresql://user:pass@localhost:5432/current \
  --target postgresql://user:pass@localhost:5432/desired

Or install pgmig with uv (recommended) or pip:

# With uv.
uv tool install pgmig@latest  # Install pgmig globally.
uv add --dev pgmig            # Or add pgmig to your project.

# With pip.
pip install pgmig

Usage

pgmig can be used directly in the command line or as a Python library.

Command line

Print the migration SQL that makes source match target:

pgmig generate \
  --source postgresql://user:pass@localhost:5432/current \
  --target postgresql://user:pass@localhost:5432/desired

When the two structures already match, nothing is printed.

Library

The same diff is available as a function that returns the SQL as a string:

import pgmig

sql = pgmig.generate(
    source="postgresql://user:pass@localhost:5432/current",
    target="postgresql://user:pass@localhost:5432/desired",
)

print(sql)  # the migration SQL

generate returns an empty string when the structures already match.

Configuration

pgmig has no configuration file — everything is passed on the command line (or as arguments to pgmig.generate).

The CLI (pgmig generate) and the library (pgmig.generate) share the same options; the CLI adds a few more ( in the library column):

CLI option Library argument Description
--source, -s source DSN of the source (current) database. Falls back to the PGMIG_SOURCE environment variable.
--target, -t target DSN of the target (desired) database. Falls back to the PGMIG_TARGET environment variable.
--index-concurrently, -C index_concurrently Whether to emit CREATE/DROP INDEX (including CREATE UNIQUE INDEX) with CONCURRENTLY. Using CONCURRENTLY avoids blocking index read/write operations, but takes longer to execute and cannot be run inside a transaction block.
--ignore-extension-version ignore_extension_version Names of extensions whose version mismatch is ignored: no ALTER EXTENSION ... UPDATE TO is emitted for them. Repeatable on the CLI; a list of names in the library.
--ignore-owner ignore_owner Suppress all ALTER ... OWNER TO statements.
--output, -o Write the migration SQL to this file instead of stdout.
--check, -c Exit non-zero if the databases differ (CI gate); the migration is still emitted.

Connections

Each DSN can be passed as a flag or through its environment variable; an explicit flag wins. Command-line arguments are visible in ps output and shell history, so prefer the environment variables for anything containing secrets — for example, in CI:

- run: pgmig generate --check
  env:
    PGMIG_SOURCE: ${{ secrets.PROD_DATABASE_URL }}
    PGMIG_TARGET: postgresql://postgres:postgres@localhost:5432/desired

Other commands:

Command Description
pgmig --version Print the installed version.
pgmig --help Show help for any command.

A DSN is any libpq connection string, e.g. postgresql://user:pass@host:5432/dbname.

FAQ

Do I need libpq installed?

By default, yes — pgmig requires the Postgres client library (libpq) on the machine. For standalone / CLI use you can skip that by installing the binary extra, which bundles it:

pip install 'pgmig[binary]'

Contributing

Contributions are welcome!

See CONTRIBUTING.md for details.

License

pgmig is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgmig-0.1.1.tar.gz (681.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pgmig-0.1.1-py3-none-any.whl (94.8 kB view details)

Uploaded Python 3

File details

Details for the file pgmig-0.1.1.tar.gz.

File metadata

  • Download URL: pgmig-0.1.1.tar.gz
  • Upload date:
  • Size: 681.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pgmig-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9d7bfc5c6b90b7ff6276d8fc599ec443a86b92a00cce31137bccfc6f72503d72
MD5 c1749a27c56644cee664037e5436d05c
BLAKE2b-256 3d41ef7acb0849594de4cc1e841a35fc344382ba8e83738139689458cb41d4fc

See more details on using hashes here.

File details

Details for the file pgmig-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pgmig-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 94.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pgmig-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f9eaee68abec3b11c5451bdfab652905e824f851afe5ee8e9d71d1657506d290
MD5 eeadaa6e9f4703038e7d61473fc2106f
BLAKE2b-256 127cb7159db8e10bf1ce086f162e55a59ced58709cbb591fca3bfb14e9ef9458

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page