Skip to main content

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:

  1. stageversion — concatenates sql/ into a versioned base file <prefix>--<version>.sql.
  2. makemigration — diffs two staged versions with results to generate an incremental migration <prefix>--A--B.sql, including body-only function/procedure changes that require CREATE OR REPLACE.
  3. graph — shows the version graph and shortest paths between versions.
  4. 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.
  5. wheel — scaffolds a self-contained wrapper Python project that bakes your migrations into a wheel and exposes a <project>-migrator console script.
  6. verify — round-trips every edge (a -> b) through two throwaway databases to prove the incremental produces the same schema as loading <prefix>--b.sql.
  7. bundle — writes a compressed tar.zst artifact containing migrations/ plus sql/pre/ and sql/post/ for automation or custom packaging flows.

Prerequisites

  • Python ≥ 3.11 and uv
  • pgpkg[diff] — the makemigration command requires the optional results dependency: uv tool install --with 'pgpkg[diff]' pgpkg
  • Docker — makemigration and verify spin up throwaway PostgreSQL containers via testcontainers; Docker must be running
  • libpq — migrate connects to a live database using standard libpq environment variables (PGHOST, PGPORT, PGDATABASE, PGUSER, PGPASSWORD) or explicit -h/-p/-d/-U/--dsn flags

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

Metadata

Release files for pgpkg 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pgpkg 0.1.1
File Size Uploaded Provenance
pgpkg-0.1.1.tar.gz 45.7 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pgpkg 0.1.1
File Interpreter ABI Platform
pgpkg-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 77.6 kB

Release files / pgpkg-0.1.1.tar.gz

Download URL pgpkg-0.1.1.tar.gz
Size 45.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a3abf53a6b7e8c88774e0280a199bf752a4f0d17cb848c6d1119984c871b7ff9
BLAKE2b-256 checksum
How to use checksums
824cc2557e77821fb7c53b327975d544ee7873042caaad8cf6a6f0416fdbd4cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 12, 2026.

Transparency log

Release files / pgpkg-0.1.1-py3-none-any.whl

Download URL pgpkg-0.1.1-py3-none-any.whl
Size 31.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b8d2e6fc7a5118abc9529b37dba63ae4adc83e120650063e8563ef89f7c4e011
BLAKE2b-256 checksum
How to use checksums
40ce2046b80e9a6e80088479f594b28c861d6ee68a515c109ff99343f4ec6d92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 12, 2026.

Transparency log

Security for pgpkg 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pgpkg-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 12, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page