CLI Reference
Quick Reference
Section titled “Quick Reference”| Command | Purpose |
|---|---|
pgmt init | Initialize new project |
pgmt apply | Apply schema to dev database |
pgmt diff | Preview what apply would do |
pgmt migrate new | Generate migration |
pgmt migrate update | Regenerate migration after changes |
pgmt migrate apply | Apply migrations to target database |
pgmt migrate status | Show migration status |
pgmt migrate validate | Validate migrations match schema |
pgmt migrate diff | Detect drift in target database |
pgmt baseline create | Create baseline snapshot |
pgmt baseline list | List baselines |
pgmt baseline clean | Remove old baselines |
pgmt debug dependencies | Analyze object dependencies |
Global Options
Section titled “Global Options”Available for all commands:
-h, --help # Show help-V, --version # Show version--config-file <FILE> # Config file (default: pgmt.yaml)-v, --verbose # Verbose output-q, --quiet # Suppress non-essential output--debug # Debug outputDatabase overrides:
--dev-url <URL> # Development database--shadow-url <URL> # Shadow database--target-url <URL> # Target/production databaseDirectory overrides:
--schema-dir <PATH> # Schema directory--migrations-dir <PATH> # Migrations directory--baselines-dir <PATH> # Baselines directorypgmt init
Section titled “pgmt init”Initialize a new pgmt project. Can be re-run to update an existing configuration.
pgmt init [OPTIONS]Options:
--dev-url <URL> # Database URL (required)--create-baseline # Create baseline from existing database--no-baseline # Skip baseline creation--no-import # Skip import (empty project)--defaults # Use defaults for all prompts--schema-dir <DIR> # Schema directory name (default: "schema")--migrations-dir <DIR> # Migrations directory name (default: "migrations")--baselines-dir <DIR> # Baselines directory name (default: "schema_baselines")--auto-shadow # Use auto shadow database--shadow-pg-version <VER> # PostgreSQL version for auto shadow (e.g., "14", "15", "16")--roles-file <PATH> # Path to roles file (default: auto-detect roles.sql)--fresh # Force fresh init (overwrite existing config)Re-initialization:
When run in a directory with an existing pgmt.yaml, you’ll be prompted to:
- Update - Modify existing config (shows current values as defaults)
- Fresh - Start over with new configuration
- Cancel - Keep current configuration
Use --fresh to skip this prompt and always overwrite.
Examples:
# Interactive (recommended)pgmt init --dev-url postgres://localhost/myapp_dev
# Import existing database with baselinepgmt init --dev-url postgres://localhost/existing_db --create-baseline
# Empty projectpgmt init --defaults --no-import
# Custom directory structurepgmt init --dev-url postgres://localhost/myapp_dev \ --schema-dir db/schema \ --migrations-dir db/migrations \ --baselines-dir db/baselines
# Specify PostgreSQL version for shadow databasepgmt init --dev-url postgres://localhost/myapp_dev --auto-shadow --shadow-pg-version 14
# Re-initialize with fresh configpgmt init --dev-url postgres://localhost/newdb --freshpgmt apply
Section titled “pgmt apply”Apply schema files to the development database.
pgmt apply [OPTIONS]Options:
--watch # Watch for file changes--dry-run # Preview changes without applying--auto-safe # Auto-apply safe changes, prompt for destructive--safe-only # Apply only safe changes--confirm-all # Prompt for each change--force-all # Apply all without promptsExamples:
pgmt apply # Interactivepgmt apply --watch # Watch mode (auto-safe by default)pgmt apply --dry-run # Preview onlypgmt apply --safe-only # Skip destructive changespgmt diff
Section titled “pgmt diff”Compare schema files against the development database. Shows what pgmt apply would do.
pgmt diff [OPTIONS]Options:
--format <FORMAT> # detailed | summary | sql | json--output-sql <FILE> # Save SQL to fileExamples:
pgmt diff # Detailed comparisonpgmt diff --format summary # Quick overviewpgmt diff --format sql # SQL to syncExit codes: 0 = no differences, 1 = differences found
pgmt migrate new
Section titled “pgmt migrate new”Generate a new migration based on schema changes.
pgmt migrate new [DESCRIPTION] [OPTIONS]Options:
--create-baseline # Create baseline alongside migrationExamples:
pgmt migrate new "add users table"pgmt migrate new "v2.0 release" --create-baselinepgmt migrate new # Interactive (prompts for description)pgmt migrate update
Section titled “pgmt migrate update”Regenerate a migration after schema changes or when your branch is behind.
pgmt migrate update [VERSION] [OPTIONS]Arguments:
VERSION # Migration version (e.g., V1234567890 or partial 123456) # If omitted, updates the latest migrationOptions:
--dry-run # Preview without updating--backup # Create .bak file before updatingExamples:
pgmt migrate update V1734567890 # Update specific migrationpgmt migrate update --dry-run # Previewpgmt migrate update --backup # Update with backupNote: This regenerates the migration from scratch. Manual edits will be lost.
pgmt migrate apply
Section titled “pgmt migrate apply”Apply migrations to a target database.
pgmt migrate apply [OPTIONS]Options:
--target-url <URL> # Target database (required unless configured)Examples:
pgmt migrate apply --target-url postgres://prod/myapppgmt migrate apply # Uses target_url from pgmt.yamlpgmt migrate status
Section titled “pgmt migrate status”Show migration status for a target database.
pgmt migrate status [OPTIONS]Options:
--target-url <URL> # Target databaseExample output:
Applied: V1734500000__create_users.sql (2024-12-18 10:00) V1734510000__add_posts.sql (2024-12-18 11:00)
Pending: V1734520000__add_comments.sqlpgmt migrate validate
Section titled “pgmt migrate validate”Validate that migrations produce the expected schema. Use in CI to catch missing migrations.
pgmt migrate validate [OPTIONS]Options:
--format <FORMAT> # human | json--verbose # Detailed outputExamples:
pgmt migrate validatepgmt migrate validate --format jsonExit codes: 0 = valid, 1 = mismatch
pgmt migrate diff
Section titled “pgmt migrate diff”Detect drift between schema files and target database.
pgmt migrate diff [OPTIONS]Options:
--format <FORMAT> # detailed | summary | sql | json--output-sql <FILE> # Save remediation SQL to file--target-url <URL> # Target databaseExamples:
pgmt migrate diff --target-url postgres://prod/myapppgmt migrate diff --format sql --output-sql fix-drift.sqlExit codes: 0 = no drift, 1 = drift detected
pgmt baseline create
Section titled “pgmt baseline create”Create a baseline snapshot from current schema files.
pgmt baseline create [OPTIONS]Options:
--description <DESC> # Custom descriptionExamples:
pgmt baseline createpgmt baseline create --description "Production v2.0"pgmt baseline list
Section titled “pgmt baseline list”List existing baselines.
pgmt baseline list [OPTIONS]Options:
--format <FORMAT> # table | jsonpgmt baseline clean
Section titled “pgmt baseline clean”Remove old baselines.
pgmt baseline clean [OPTIONS]Options:
--keep <N> # Keep N most recent (default: 5)--older-than <DAYS> # Remove baselines older than N days--dry-run # Preview what would be deletedExamples:
pgmt baseline clean --keep 5pgmt baseline clean --older-than 30pgmt baseline clean --keep 3 --dry-runpgmt debug dependencies
Section titled “pgmt debug dependencies”Analyze object dependencies from both PostgreSQL introspection and -- require: headers. Useful for troubleshooting dependency ordering issues.
pgmt debug dependencies [OPTIONS]Options:
--format <FORMAT> # json | text (default: json)--object <NAME> # Filter to specific objectExamples:
pgmt debug dependencies # JSON outputpgmt debug dependencies --format text # Human-readablepgmt debug dependencies --object public.users # Filter to one objectOutput includes:
- All tracked objects and their types
- File-to-object mappings (which file defines which objects)
- File dependencies from
-- require:headers - Dependency graph showing what each object depends on
pgmt config
Section titled “pgmt config”Manage configuration.
pgmt config get <KEY> # Get valuepgmt config set <KEY> <VALUE> # Set valuepgmt config list # List all valuespgmt config validate # Validate config fileExamples:
pgmt config get databases.devpgmt config set databases.dev postgres://localhost/myapppgmt config list --format jsonEnvironment Variables
Section titled “Environment Variables”PGMT_CONFIG_FILE # Override config file locationPGMT_DEV_URL # Override dev database URLPGMT_SHADOW_URL # Override shadow database URLPGMT_TARGET_URL # Override target database URLExit Codes
Section titled “Exit Codes”0 - Success1 - General error2 - Configuration error3 - Database connection error4 - Migration error5 - Validation error64 - Usage error (invalid arguments)