Schema Diff
The diff command compares the database schema between two branches and displays the differences.
pgbranch diff <branch1> [branch2] [flags]Arguments
Section titled “Arguments”| Argument | Description |
|---|---|
branch1 | First branch to compare (required) |
branch2 | Second branch to compare (optional, defaults to working database) |
| Flag | Description |
|---|---|
--stat | Show summary statistics only |
--sql | Show SQL statements to apply changes |
Examples
Section titled “Examples”Compare Two Branches
Section titled “Compare Two Branches”pgbranch diff main feature-authOutput:
=== Schema Diff: main → feature-auth ===
Table: sessions + Column: id (uuid, NOT NULL, DEFAULT gen_random_uuid()) + Column: user_id (uuid, NOT NULL) + Column: token (text, NOT NULL) + Column: expires_at (timestamp with time zone) + Index: sessions_user_id_idx ON sessions(user_id) + Foreign Key: sessions_user_id_fkey REFERENCES users(id)
~ Table: users + Column: last_login (timestamp with time zone) + Column: login_count (integer, DEFAULT 0)
Enum: auth_provider Values: local, google, githubCompare Branch Against Working Database
Section titled “Compare Branch Against Working Database”pgbranch diff mainThis compares the main branch snapshot against your current working database state.
Summary Statistics Only
Section titled “Summary Statistics Only”pgbranch diff main feature-auth --statOutput:
Schema Diff Summary: main → feature-auth
Tables: + 1 added ~ 1 modified - 0 removed
Columns: + 5 added ~ 0 modified - 0 removed
Indexes: + 1 added
Constraints: + 1 foreign key added
Enums: + 1 addedGenerate Migration SQL
Section titled “Generate Migration SQL”pgbranch diff main feature-auth --sqlOutput:
-- Migration: main → feature-auth-- Generated by pgbranch
-- Create enum typeCREATE TYPE auth_provider AS ENUM ('local', 'google', 'github');
-- Create table: sessionsCREATE TABLE sessions ( id uuid NOT NULL DEFAULT gen_random_uuid(), user_id uuid NOT NULL, token text NOT NULL, expires_at timestamp with time zone, PRIMARY KEY (id));
-- Add columns to usersALTER TABLE users ADD COLUMN last_login timestamp with time zone;ALTER TABLE users ADD COLUMN login_count integer DEFAULT 0;
-- Create indexesCREATE INDEX sessions_user_id_idx ON sessions(user_id);
-- Add foreign keysALTER TABLE sessions ADD CONSTRAINT sessions_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id);What Gets Compared
Section titled “What Gets Compared”The diff command detects changes in:
Tables
Section titled “Tables”- New tables added
- Tables removed
- Table renames (detected as remove + add)
Columns
Section titled “Columns”- New columns
- Removed columns
- Type changes
- Default value changes
- Nullability changes
- Column renames (detected as remove + add)
Indexes
Section titled “Indexes”- New indexes
- Removed indexes
- Index definition changes
Constraints
Section titled “Constraints”- Primary keys
- Foreign keys
- Unique constraints
- Check constraints
- New enum types
- Removed enum types
- Added enum values
- Removed enum values
Functions
Section titled “Functions”- New functions
- Removed functions
- Function signature changes
- Function body changes
Output Format
Section titled “Output Format”Color Coding
Section titled “Color Coding”When displayed in a terminal:
| Symbol | Color | Meaning |
|---|---|---|
+ | Green | Added |
- | Red | Removed |
~ | Yellow | Modified |
Destructive Change Warnings
Section titled “Destructive Change Warnings”Potentially destructive changes are highlighted:
=== Schema Diff: feature-auth → main ===
⚠️ DESTRUCTIVE CHANGES DETECTED:
Table: sessions (will be dropped) ⚠️ This will delete all data in the table
~ Table: users - Column: last_login (will be dropped) - Column: login_count (will be dropped)Use Cases
Section titled “Use Cases”Review Before Merge
Section titled “Review Before Merge”Before merging schema changes:
# See what will changepgbranch diff main feature-auth
# If looks good, proceed with mergepgbranch merge feature-auth mainGenerate Migration Files
Section titled “Generate Migration Files”Create migration files from diff output:
pgbranch diff main feature-auth --sql > migrations/002_add_auth.sqlAudit Schema Evolution
Section titled “Audit Schema Evolution”Track how your schema evolved:
# Compare original to currentpgbranch diff v1-schema current-schema --statDebug Schema Issues
Section titled “Debug Schema Issues”When something isn’t working:
# Compare working database against known-good branchpgbranch diff known-goodLimitations
Section titled “Limitations”No Data Comparison
Section titled “No Data Comparison”The diff command only compares schema, not data. To compare data:
# Use PostgreSQL tools directlypg_dump --schema-only db1 > schema1.sqlpg_dump --schema-only db2 > schema2.sqldiff schema1.sql schema2.sqlRename Detection
Section titled “Rename Detection”Renames are detected as separate add/remove operations. pgbranch cannot automatically detect that a column was renamed vs. removed and a new one added.
Extension-Specific Objects
Section titled “Extension-Specific Objects”Some PostgreSQL extension-specific objects may not be fully compared.