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.
- 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 policy —
auto|always|neverwith smart detection ofCONCURRENTLYstatements - Override system — Explicit, auditable overrides via SQL comments with required reasons
- CI-ready —
checkcommand as a CI gate, plus a GitHub Action wrapper - Programmatic API — Use the core library directly in your application
# 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| Package | Version | Description |
|---|---|---|
pg-safe-migrate |
CLI tool | |
pg-safe-migrate-core |
Core library (programmatic API) |
Get up and running quickly with a production-ready starter:
| Template | Stack | Description |
|---|---|---|
| express-postgres-starter | Express + TypeScript | REST API with safe migrations baked in |
| nextjs-postgres-starter | Next.js + TypeScript | Full-stack app with migration workflow |
- Getting Started
- Configuration
- Safety Rules
- Overrides
- Zero-Downtime Patterns
- GitHub Action
- Ecosystem & Integrations
- Adoption Roadmap
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_URLReturns exit code 1 if any safety rules are violated or drift is detected.
| Feature | pg-safe-migrate | node-pg-migrate | graphile-migrate | dbmate |
|---|---|---|---|---|
| Built-in safety linter | ✅ 10 rules | ❌ | ❌ | ❌ |
| Drift detection | ✅ SHA-256 | ❌ | ❌ | |
| Advisory locks | ✅ | ❌ | ✅ | ❌ |
| CONCURRENTLY detection | ✅ auto | Manual | ❌ | ❌ |
| Override system | ✅ auditable | N/A | N/A | N/A |
| CI gate command | ✅ check |
❌ | ❌ | ❌ |
| GitHub Action | ✅ | ❌ | ❌ | ❌ |
| Down migrations | ✅ optional | ✅ | ❌ | ✅ |
| TypeScript | ✅ native | ✅ | ✅ | Go |
See CONTRIBUTING.md for development setup and guidelines.
MIT