pgmold
PostgreSQL schema-as-code management tool. Define schemas in native PostgreSQL DDL, diff against live databases, plan migrations, and apply them safely.
Features
- Schema-as-Code: Define PostgreSQL schemas in native SQL DDL files
- Introspection: Read schema from live PostgreSQL databases
- Diffing: Compare schemas and generate migration plans
- Safety: Lint rules prevent destructive operations without explicit flags
- Drift Detection: Monitor for schema drift in CI/CD
- Transactional Apply: All migrations run in a single transaction
Installation
Usage
# Compare SQL schema to live database
# Generate migration plan
# Apply migrations (with safety checks)
# Apply with destructive operations allowed
# Dry run (preview SQL without executing)
# Lint schema
# Monitor for drift
Schema Definition (PostgreSQL DDL)
('admin', 'user', 'guest');
(
id BIGINT NOT NULL,
email VARCHAR(255) NOT NULL,
role user_role NOT NULL DEFAULT 'guest',
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
PRIMARY KEY (id)
);
(email);
(
id BIGINT NOT NULL,
user_id BIGINT NOT NULL,
title TEXT NOT NULL,
content TEXT,
PRIMARY KEY (id),
CONSTRAINT posts_user_id_fkey FOREIGN KEY (user_id)
REFERENCES users (id) ON DELETE CASCADE
);
(user_id);
Safety Rules
By default, pgmold blocks destructive operations:
DROP TABLErequires--allow-destructiveDROP COLUMNrequires--allow-destructiveDROP ENUMrequires--allow-destructive- Type narrowing produces warnings
SET NOT NULLproduces warnings (may fail on existing NULLs)
Set PGMOLD_PROD=1 to enable production mode, which blocks table drops entirely.
Development
# Build
# Test
# Run integration tests (requires Docker)
License
MIT