PostgreSQL Package Manager CLI
pgpm is a powerful and opinionated command-line interface (CLI) for managing PostgreSQL database schemas, migrations, and modules, akin to a package manager for your database. It is designed for modular PostgreSQL development, enabling the creation and management of reusable database packages with dependency resolution and semantic versioning. The tool features a deterministic migration engine, offering version-controlled, plan-driven deployments with rollback capabilities and idempotent execution. It supports recursive module resolution across workspaces and provides turnkey module-first workspaces ready for CI/CD, Docker, and end-to-end testing with TypeScript tooling. The current stable version is 4.16.3, and it appears to be actively maintained with a focus on robust and reproducible database development workflows.
Common errors
-
Error: Database connection refused.
cause The `pgpm` CLI tool cannot establish a connection to the PostgreSQL database, likely due to incorrect connection parameters, the database not running, or network issues.fixVerify that your PostgreSQL database is running and accessible. Check your `PGHOST`, `PGPORT`, `PGUSER`, `PGPASSWORD`, and `PGDATABASE` environment variables. If using Docker, ensure `pgpm docker start` was successful and `eval "$(pgpm env)"` was run. -
Error: Migration checksum mismatch detected for migration '001_initial_schema.sql'.
cause The content of a previously applied migration file has changed, or the corresponding schema object in the database no longer matches the expected state recorded by `pgpm`.fixDo NOT modify migration files after they have been deployed to a shared environment. If this occurred in development, revert the changes, or if necessary, reset your local database state and re-deploy. For production, a new 'reverting' migration or a data patch might be needed, with careful planning. -
Error: Module '@pgpm/my-module' not found or dependency not resolved.
cause The `pgpm` CLI could not locate a referenced database module or resolve its dependencies, often due to an incorrect module name, missing installation, or an issue with the workspace configuration.fixEnsure the module name is correct. If it's a public module, run `pgpm install @pgpm/my-module`. If it's a local module within your workspace, verify its `package.json` and directory structure. Check your `pgpm.json` configuration for correct module paths. -
Error: There are pending migrations that have not been applied.
cause The database schema is not up-to-date with the latest migration files in your project. This means new migration files exist in your codebase that haven't been run against the connected database.fixRun `pgpm deploy` to apply the pending migrations to your database. If you expect the database to be up-to-date, run `pgpm verify` to identify which migrations are pending or if there's a discrepancy.
Warnings
- breaking Major versions of `pgpm` may introduce changes to migration file formats, project structure, or command-line arguments. Always review release notes carefully before upgrading to avoid unexpected behavior or requiring manual migration adjustments.
- gotcha When running `pgpm` commands that interact with a database (e.g., `deploy`, `verify`), ensure your `PG*` environment variables (like `PGHOST`, `PGPORT`, `PGUSER`, `PGPASSWORD`, `PGDATABASE`) are correctly set. `pgpm` relies on these for connection, and incorrect settings will lead to connection errors.
- breaking The package resolution logic for `pgpm`'s database modules changed in prior versions, moving towards a more `npm`-like recursive dependency model. This might affect how modules are located and deployed if not updated.
- gotcha Direct modification of `pgpm`-managed database objects outside of `pgpm` migrations can lead to 'migration checksum mismatch' errors, as `pgpm`'s deterministic engine expects controlled schema changes. This often happens if you manually alter the database schema or run raw SQL outside the `pgpm` workflow.
Install
-
npm install pgpm -
yarn add pgpm -
pnpm add pgpm
Imports
- PgpmConfiguration
import type { PgpmConfiguration } from 'pgpm' - deploy
import { deploy } from 'pgpm/commands' - initWorkspace
import { initWorkspace } from 'pgpm/workspace'
Quickstart
# Install pgpm globally
npm install -g pgpm
# Start a local PostgreSQL instance using Docker and export environment variables
pgpm docker start
eval "$(pgpm env)"
# Create a new pgpm workspace
pgpm init workspace my-app
cd my-app
# Create your first database module within the workspace
pgpm init packages/my-module
cd packages/my-module
# Add a sample migration (e.g., create a users table)
# Create src/migrations/001_create_users_table.sql with content:
/*
create table users (
id serial primary key,
name text not null
);
*/
# Return to the workspace root
cd ../..
# Deploy all changes and modules to the database, creating the database if it doesn't exist
pgpm deploy --createdb --database mydatabase
console.log("pgpm workspace and initial module deployed successfully!")