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.
Documentation: pgmig.readthedocs.io
Say the current database has a users table, and the desired one adds an email column with a unique index:
-- current
CREATE TABLE users (id bigint PRIMARY KEY, name text NOT NULL);
-- desired
CREATE TABLE users (id bigint PRIMARY KEY, name text NOT NULL, email text NOT NULL);
CREATE UNIQUE INDEX users_email_idx ON users (email);Print the migration SQL that makes current match desired, using uvx:
$ uvx pgmig generate \
--source postgresql://user:pass@localhost:5432/current \
--target postgresql://user:pass@localhost:5432/desired
ALTER TABLE "public"."users" ADD COLUMN "email" text NOT NULL;
CREATE UNIQUE INDEX users_email_idx ON public.users USING btree (email);Or from Python:
import pgmig
sql = pgmig.generate(
source="postgresql://user:pass@localhost:5432/current",
target="postgresql://user:pass@localhost:5432/desired",
)
print(sql)
# ALTER TABLE "public"."users" ADD COLUMN "email" text NOT NULL;
# CREATE UNIQUE INDEX users_email_idx ON public.users USING btree (email);Contributions are welcome!
See CONTRIBUTING.md for details.
pgmig is distributed under the terms of the MIT license.
