Skip to main content
This guide will walk you through the core pgschema workflow: dumping a schema, making edits, planning changes, and applying migrations. By the end, you’ll understand how to manage Postgres schemas declaratively.

Prerequisites

Before starting, ensure you have:
  • pgschema installed (see installation guide)
  • Access to a PostgreSQL database (14+)
  • Database credentials with appropriate permissions

Step 1: Dump Your Current Schema

First, let’s capture the current state of your database schema:
This creates a schema.sql file containing your complete schema definition. Let’s look at what it contains:
schema.sql
The output is clean and developer-friendly, unlike the verbose output from pg_dump.

Step 2: Make Schema Changes

Now, let’s modify the schema.sql file to add new features. Edit the file to include:
schema.sql (modified)

Step 3: Generate Plan

This will save the migration plan to plan.json and print the human-readable version to stdout:
plan.txt

Step 4: Apply Changes

When you’re ready to apply the changes:

Alternative: Direct Apply

You can also skip the separate planning step and apply changes directly from the schema file:
This approach automatically generates the plan internally and applies the changes in one command.
The two-phase workflow (plan then apply) is recommended for production environments where you want to review and save the plan before execution. Direct apply is convenient for development environments.
pgschema will:
  1. Show you the migration plan
  2. Ask for confirmation (unless using --auto-approve)
  3. Apply the changes
  4. Report success or any errors