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)
- Read the desired state from a SQL file (with include directive support)
- Compare it with the current database state of the target schema
- Generate a migration plan with proper dependency ordering
- Display the plan for review
- Apply the changes (with optional confirmation and safety checks)
Plan Mode (Execute Pre-generated Plan)
- Load a pre-generated plan from JSON file
- Validate plan version compatibility and schema fingerprints
- Display the plan for review
- Apply the changes (with optional confirmation and safety checks)
Basic Usage
Connection Options
Database server host (env: PGHOST)
Database server port (env: PGPORT)
Database name (required) (env: PGDATABASE)
Database user name (required) (env: PGUSER)
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:
- Command line
--password
flag (highest priority) PGPASSWORD
environment variable.pgpass
file in user’s home directory- PostgreSQL will prompt for password if none found
Schema name to apply changes to
Apply Options
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.
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
.Apply changes without prompting for approvalUseful for automated deployments and CI/CD pipelines.
Disable colored output in the plan displayUseful for scripts, CI/CD environments, or terminals that don’t support colors.
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.
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)
- Generate a migration plan by comparing the desired state with current database
- Display the plan with colored output
- Prompt: “Do you want to apply these changes? (yes/no):”
- Wait for confirmation before proceeding
- Apply changes using transactions where possible
Plan Mode (Execute Pre-generated Plan)
- 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: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