Skip to main content
The plan command generates a migration plan to apply a desired schema state to a target database schema. It compares the desired state (from a file) with the current state of a specific schema and shows what changes would be applied.

Overview

The plan command follows infrastructure-as-code principles similar to Terraform:
  1. Read the desired state from a SQL file (with include directive support)
  2. Apply the desired state SQL to a temporary PostgreSQL instance (embedded by default, or external via --plan-* flags)
  3. Connect to the target database and analyze current state of the specified schema
  4. Compare the two states
  5. Generate a detailed migration plan with proper dependency ordering
  6. Display the plan without making any changes
By default, pgschema uses an embedded PostgreSQL instance to validate your desired state SQL. Cross-schema foreign keys to unmanaged tables (for example Supabase auth.users) work with the default embedded instance when you ignore those schemas. For PostgreSQL extensions, or when referenced tables are not on the target, see External Plan Database.

Basic Usage

Connection Options

string
default:"localhost"
Database server host (env: PGHOST)
integer
default:"5432"
Database server port (env: PGPORT)
string
required
Database name (required) (env: PGDATABASE)
string
required
Database user name (required) (env: PGUSER)
string
Database password (optional, can also use PGPASSWORD env var or .pgpass file)You can provide the password in multiple ways (in order of precedence):
Password Resolution Order:
  1. Command line --password flag (highest priority)
  2. PGPASSWORD environment variable
  3. .pgpass file in user’s home directory
  4. PostgreSQL will prompt for password if none found
See dotenv (.env) for detailed configuration options.
string
default:"prefer"
SSL mode for database connection (env: PGSSLMODE)Valid values: disable, allow, prefer, require, verify-ca, verify-fullFor verify-ca and verify-full modes, you can configure certificate paths using standard PostgreSQL environment variables (PGSSLROOTCERT, PGSSLCERT, PGSSLKEY).
string
default:"public"
Schema name to target for comparison

Plan Database Options

By default, the plan command uses an embedded PostgreSQL instance to validate your desired state SQL. Cross-schema FKs to ignored tables are stubbed from the target automatically; extensions and other edge cases may need an external database. See External Plan Database for complete documentation.

Plan Options

string
required
Path to desired state SQL schema file
string
Output human-readable format to stdout or file pathExamples:
  • --output-human stdout - Display to terminal
  • --output-human plan.txt - Save to file
string
Output JSON format to stdout or file pathThis JSON format is the same format accepted by the apply command for executing migration plans.Examples:
  • --output-json stdout - Display to terminal
  • --output-json plan.json - Save to file
string
Output SQL format to stdout or file pathExamples:
  • --output-sql stdout - Display to terminal
  • --output-sql migration.sql - Save to file
boolean
default:"false"
Disable colored output for human format when writing to stdoutThis is useful for:
  • Scripts and automation that need to parse output
  • CI/CD environments that don’t support color codes
  • Redirecting output to files where color codes are unwanted
Note: This flag only affects human format output to stdout. File output and JSON/SQL formats are never colored.

Ignoring Objects

You can exclude specific database objects from migration planning using a .pgschemaignore file. See Ignore (.pgschemaignore) for complete documentation.

Examples

Default Human-Readable Output

JSON Output for Automation

SQL Migration Script

Plan for Specific Schema

Use Cases

Pre-deployment Validation

CI/CD Integration

Change Tracking

Multiple Output Formats

You can generate multiple output formats simultaneously:
Note: Only one output format can use stdout. If no output flags are specified, the command defaults to human-readable output to stdout with colors enabled.

Comparison Direction

The plan command is unidirectional: it always plans changes from the current state (database) to the desired state (file).
This ensures:
  • Consistent infrastructure-as-code workflow
  • Clear source of truth (the file)
  • Predictable change direction

Include Directive Support

The plan command supports include directives in schema files, allowing you to organize your schema across multiple files:
The include processor will resolve all includes relative to the directory containing the main schema file.