pg-safe-migrate

March 3, 2026 · View on GitHub

npm version npm core License: MIT TypeScript Node.js

Safety-first PostgreSQL migration engine for Node.js

pg-safe-migrate helps teams ship schema changes confidently by enforcing safety rules, detecting drift, and providing clear guardrails — all from a simple CLI or programmatic API.

Features

  • Safety linter — 10 built-in rules block risky schema operations by default
  • Drift detection — SHA-256 checksums ensure applied migrations are immutable
  • Advisory locks — Guarantees single-runner execution via PostgreSQL advisory locks
  • Transaction policyauto | always | never with smart detection of CONCURRENTLY statements
  • Override system — Explicit, auditable overrides via SQL comments with required reasons
  • CI-readycheck command as a CI gate, plus a GitHub Action wrapper
  • Programmatic API — Use the core library directly in your application

Quick Start

# Install
npm install -g pg-safe-migrate

# Initialize in your project
pg-safe-migrate init

# Create a migration
pg-safe-migrate create add_users_table

# Edit the generated file in ./migrations/
# Then apply:
export DATABASE_URL=postgresql://localhost/mydb
pg-safe-migrate up

# Check status
pg-safe-migrate status

Packages

PackageVersionDescription
pg-safe-migratenpmCLI tool
pg-safe-migrate-corenpmCore library (programmatic API)

Starter Templates

Get up and running quickly with a production-ready starter:

TemplateStackDescription
express-postgres-starterExpress + TypeScriptREST API with safe migrations baked in
nextjs-postgres-starterNext.js + TypeScriptFull-stack app with migration workflow

Documentation

CI Gate Example

Add a safety check to your GitHub Actions workflow:

- uses: defnotwig/pg-safe-migrate/action@v1
  with:
    command: check
    database_url: ${{ secrets.DATABASE_URL }}

Or run it manually:

pg-safe-migrate check --database $DATABASE_URL

Returns exit code 1 if any safety rules are violated or drift is detected.

Why pg-safe-migrate?

Featurepg-safe-migratenode-pg-migrategraphile-migratedbmate
Built-in safety linter✅ 10 rules
Drift detection✅ SHA-256⚠️ hash-based
Advisory locks
CONCURRENTLY detection✅ autoManual
Override system✅ auditableN/AN/AN/A
CI gate commandcheck
GitHub Action
Down migrations✅ optional
TypeScript✅ nativeGo

Contributing

See CONTRIBUTING.md for development setup and guidelines.

Community

Acknowledgments

Special thanks to all contributors who help improve pg-safe-migrate through code, documentation, and feedback.

License

MIT