Skip to main content
The apply command applies database schema changes to a target database schema. You can either provide a desired state file to generate and apply a plan, or execute a pre-generated plan file directly.

Overview

The apply command supports two execution modes:

File Mode (Generate and Apply)

  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. Compare it with the current database state of the target schema
  4. Generate a migration plan with proper dependency ordering
  5. Display the plan for review
  6. Apply the changes (with optional confirmation and safety checks)
By default, File Mode uses an embedded PostgreSQL instance to validate the desired state. Cross-schema FKs to ignored tables are stubbed from the target automatically; for extensions or other cases see External Plan Database.

Plan Mode (Execute Pre-generated Plan)

  1. Load a pre-generated plan from JSON file
  2. Validate plan version compatibility and schema fingerprints
  3. Display the plan for review
  4. Apply the changes (with optional confirmation and safety checks)

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 apply changes to

Plan Database Options

When using File Mode (--file), the apply command generates a plan internally using a temporary PostgreSQL instance. Cross-schema FKs to ignored tables work with the default embedded instance; extensions and other edge cases may need an external database. See External Plan Database for complete documentation. Note: These options only apply when using --file mode. When using --plan mode, the plan has already been generated.

Apply Options

string
Path to desired state SQL schema file (mutually exclusive with —plan)Used in File Mode to generate and apply a plan from the desired state.
string
Path to pre-generated plan JSON file (mutually exclusive with —file)Used in Plan Mode to execute a plan that was previously generated with pgschema plan --output-json.
boolean
default:"false"
Apply changes without prompting for approvalUseful for automated deployments and CI/CD pipelines.
boolean
default:"false"
Disable colored output in the plan displayUseful for scripts, CI/CD environments, or terminals that don’t support colors.
string
Maximum time to wait for database locks (e.g., ’30s’, ‘5m’, ‘1h’)If not specified, uses PostgreSQL’s default behavior (wait indefinitely). See PostgreSQL lock_timeout documentation.When set, a failed attempt to acquire a lock is automatically retried with exponential backoff instead of failing immediately. CREATE INDEX CONCURRENTLY is never retried.
string
default:"pgschema"
Application name for database connection (visible in pg_stat_activity) (env: PGAPPNAME)See PostgreSQL application_name documentation.

Ignoring Objects

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

Examples

File Mode (Generate and Apply)

This will:
  1. Generate a migration plan by comparing the desired state with current database
  2. Display the plan with colored output
  3. Prompt: “Do you want to apply these changes? (yes/no):”
  4. Wait for confirmation before proceeding
  5. Apply changes using transactions where possible
Example output:

Plan Mode (Execute Pre-generated Plan)

Benefits of plan mode:
  • Separation of concerns: Generate plans in one environment, apply in another
  • Review process: Plans can be reviewed before deployment
  • Repeatability: Same plan can be applied to multiple environments
  • Version control: Plans can be stored and versioned
  • Rollback preparation: Generate rollback plans before applying changes

Auto-approve for CI/CD

With Lock Timeout

Custom Application Name

Safety Features

Schema Fingerprint Validation

When using plan mode, pgschema validates that the database schema hasn’t changed since the plan was generated:
If schema changes are detected:

Version Compatibility

Plans include version information to ensure compatibility:
  • pgschema version: Must match the version used to generate the plan
  • Plan format version: Must be supported by the current pgschema version

Transaction Handling

pgschema automatically determines whether changes can run in a transaction:
  • Transactional mode (default): All changes run in a single transaction with automatic rollback on failure
  • Non-transactional mode: Some operations (like CREATE INDEX CONCURRENTLY) run outside transactions

No-op Detection

If no changes are needed, pgschema skips execution: