plan and apply commands can use an external PostgreSQL database instead of the default embedded PostgreSQL instance for validating desired state schemas. This is useful in environments where embedded PostgreSQL has limitations.
Overview
By default, theplan command (and apply command in File Mode) spins up a temporary embedded PostgreSQL instance to apply and validate your desired state SQL. However, you can optionally provide your own PostgreSQL database using the --plan-* flags or PGSCHEMA_PLAN_* environment variables.
Note: For the apply command, these options only apply when using File Mode (--file). When using Plan Mode (--plan), the plan has already been generated, so plan database options are not applicable.
When to Use External Database
Use an external database for plan generation when:- Your schema uses PostgreSQL extensions (like
hstore,postgis,uuid-ossp, etc.) - The embedded database doesn’t have extensions pre-installed, causing plan generation to fail with “type does not exist” errors (#121) - Your schema has cross-schema foreign key references - The embedded approach only loads one schema at a time, breaking foreign key constraints that reference tables in other schemas (#122)
How It Works
When using an external database:- Temporary Schema Creation: pgschema creates a temporary schema with a unique timestamp (e.g.,
pgschema_tmp_20251030_154501_123456789) - SQL Application: Your desired state SQL is applied to the temporary schema
- Schema Inspection: The temporary schema is inspected to extract the desired state
- Comparison: The desired state is compared with your target database’s current state
- Cleanup: The temporary schema is dropped (best effort) after plan generation
Basic Usage
With Plan Command
With Apply Command (File Mode)
Common Use Cases
Using PostgreSQL Extensions
If your schema uses extensions likehstore, postgis, or uuid-ossp, you need to install them in the plan database first:
schema.sql can now use extension types:
Handling Cross-Schema Foreign Keys
If your schema has foreign keys that reference tables in other schemas, you need to create those schemas in the plan database:schema.sql can now reference tables in other schemas:
Configuration Options
Using Command-Line Flags
Plan database server host. If provided, uses external database instead of embedded PostgreSQL.Environment variable:
PGSCHEMA_PLAN_HOSTPlan database server port.Environment variable:
PGSCHEMA_PLAN_PORTPlan database name. Required when
--plan-host is provided.Environment variable: PGSCHEMA_PLAN_DBPlan database user name. Required when
--plan-host is provided.Environment variable: PGSCHEMA_PLAN_USERPlan database password. Can also be provided via
PGSCHEMA_PLAN_PASSWORD environment variable.Environment variable: PGSCHEMA_PLAN_PASSWORDUsing Environment Variables
Database Permissions
The plan database user needs the following permissions:- Create and drop schemas
- Create tables, indexes, functions, and other schema objects
- Set search_path
See Also
- Plan Command - Main plan command documentation
- Apply Command - Applying migration plans
- Environment Variables - Managing environment configuration

