PostgreSQL migration toolkit: stage versions, generate incrementals via results, apply with libpq.
Project description
pgpkg
A small, focused PostgreSQL migration toolkit.
You write SQL describing the exact desired state of your database in an
ordered sql/ directory. pgpkg does everything else:
stageversion— concatenatessql/into a versioned base file<prefix>--<version>.sql.makemigration— diffs two staged versions with results to generate an incremental migration<prefix>--A--B.sql.graph— shows the version graph and shortest paths between versions.migrate— connects to a live database (libpq env vars or psql-style flags), reads the currently installed version, and applies the shortest sequence of incrementals (or bootstraps from a base file) to reach the target version.wheel— scaffolds a self-contained wrapper Python project that bakes your migrations into a wheel and exposes a<project>-migratorconsole script.verify— round-trips every edge(a -> b)through two throwaway databases to prove the incremental produces the same schema as loading<prefix>--b.sql.bundle— writes a compressedtar.zstartifact containingmigrations/plussql/pre/andsql/post/for automation or custom packaging flows.
Prerequisites
- Python ≥ 3.11 and uv
pgpkg[diff]— themakemigrationcommand requires the optional results dependency:uv tool install --with 'pgpkg[diff]' pgpkg- Docker —
makemigrationandverifyspin up throwaway PostgreSQL containers viatestcontainers; Docker must be running - libpq —
migrateconnects to a live database using standard libpq environment variables (PGHOST,PGPORT,PGDATABASE,PGUSER,PGPASSWORD) or explicit-h/-p/-d/-U/--dsnflags
Quickstart
uv tool install --with 'pgpkg[diff]' pgpkg
mkdir -p sql/pre sql/post
echo "CREATE TABLE foo (id int PRIMARY KEY);" > sql/010_schema.sql
# declare your project once
cat >> pyproject.toml <<'TOML'
[tool.pgpkg]
project_name = "myext"
TOML
# stage a version
pgpkg stageversion 0.1.0
# iterate sql/ and generate an incremental
pgpkg stageversion 0.2.0
pgpkg makemigration --from 0.1.0 --to 0.2.0
# apply to a live database (libpq env vars honored)
pgpkg migrate -d mydb -h localhost --to 0.2.0
Tracking and packaging
By default, pgpkg records applied versions in pgpkg.migrations. You can
relocate that table with:
[tool.pgpkg]
project_name = "myext"
[tool.pgpkg.tracking]
schema = "ops"
table = "schema_versions"
If your application already owns the authoritative version table, set
[tool.pgpkg].version_source = "module:attribute" and provide an object with
read_live_version(...) and record_applied(...) methods. The generic
pgpkg wheel scaffold is intentionally limited to the default tracking path;
custom version sources should use a project-specific wrapper that calls
pgpkg.api.migrate_from_artifact(..., version_source=...).
See the full manual at https://bitner.github.io/pgpkg/ and the source docs in docs/.
Development
uv sync --extra dev --extra diff
uv run pre-commit install
uv run pre-commit run --all-files
uv run ty check src tests
uv run pytest -q
uv build --out-dir dist
uv run mkdocs build --strict
See CONTRIBUTING.md for the release workflow and trusted-publishing setup. For initial repository and publishing bootstrap, use your local project bootstrap checklist.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pgpkg-0.1.0.tar.gz.
File metadata
- Download URL: pgpkg-0.1.0.tar.gz
- Upload date:
- Size: 43.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fecfea66c84c5976eb4058f3325e4d601a4a47378b1499f56ba413b7222b5838
|
|
| MD5 |
82519ed2c6600215bc760f6245e43641
|
|
| BLAKE2b-256 |
0312bd74a956815835a0a1d318f54deab5ebfc8d807178e99421f6232d806111
|
Provenance
The following attestation bundles were made for pgpkg-0.1.0.tar.gz:
Publisher:
publish-pypi.yml on bitner/pgpkg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pgpkg-0.1.0.tar.gz -
Subject digest:
fecfea66c84c5976eb4058f3325e4d601a4a47378b1499f56ba413b7222b5838 - Sigstore transparency entry: 1442638468
- Sigstore integration time:
-
Permalink:
bitner/pgpkg@e6a501358d46fdf356476f3a778ed0842f5835b8 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/bitner
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@e6a501358d46fdf356476f3a778ed0842f5835b8 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pgpkg-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pgpkg-0.1.0-py3-none-any.whl
- Upload date:
- Size: 30.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d68d2b2287bf68ee3c47012678eac4247bad79fcefbb9fc53cff1480d4f9d73
|
|
| MD5 |
4bd211b30d4a0481aa9f7c5ed53e978f
|
|
| BLAKE2b-256 |
aa8f7153e33850f68867b340c93cde3b17d3784dbf28880169383cc4b01cff95
|
Provenance
The following attestation bundles were made for pgpkg-0.1.0-py3-none-any.whl:
Publisher:
publish-pypi.yml on bitner/pgpkg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pgpkg-0.1.0-py3-none-any.whl -
Subject digest:
1d68d2b2287bf68ee3c47012678eac4247bad79fcefbb9fc53cff1480d4f9d73 - Sigstore transparency entry: 1442638647
- Sigstore integration time:
-
Permalink:
bitner/pgpkg@e6a501358d46fdf356476f3a778ed0842f5835b8 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/bitner
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@e6a501358d46fdf356476f3a778ed0842f5835b8 -
Trigger Event:
release
-
Statement type: