Skip to content

Schema Diff

The diff command compares the database schema between two branches and displays the differences.

Terminal window
pgbranch diff <branch1> [branch2] [flags]
ArgumentDescription
branch1First branch to compare (required)
branch2Second branch to compare (optional, defaults to working database)
FlagDescription
--statShow summary statistics only
--sqlShow SQL statements to apply changes
Terminal window
pgbranch diff main feature-auth

Output:

=== 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, github
Terminal window
pgbranch diff main

This compares the main branch snapshot against your current working database state.

Terminal window
pgbranch diff main feature-auth --stat

Output:

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 added
Terminal window
pgbranch diff main feature-auth --sql

Output:

-- Migration: main → feature-auth
-- Generated by pgbranch
-- Create enum type
CREATE TYPE auth_provider AS ENUM ('local', 'google', 'github');
-- Create table: sessions
CREATE 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 users
ALTER TABLE users ADD COLUMN last_login timestamp with time zone;
ALTER TABLE users ADD COLUMN login_count integer DEFAULT 0;
-- Create indexes
CREATE INDEX sessions_user_id_idx ON sessions(user_id);
-- Add foreign keys
ALTER TABLE sessions
ADD CONSTRAINT sessions_user_id_fkey
FOREIGN KEY (user_id) REFERENCES users(id);

The diff command detects changes in:

  • New tables added
  • Tables removed
  • Table renames (detected as remove + add)
  • New columns
  • Removed columns
  • Type changes
  • Default value changes
  • Nullability changes
  • Column renames (detected as remove + add)
  • New indexes
  • Removed indexes
  • Index definition changes
  • Primary keys
  • Foreign keys
  • Unique constraints
  • Check constraints
  • New enum types
  • Removed enum types
  • Added enum values
  • Removed enum values
  • New functions
  • Removed functions
  • Function signature changes
  • Function body changes

When displayed in a terminal:

SymbolColorMeaning
+GreenAdded
-RedRemoved
~YellowModified

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)

Before merging schema changes:

Terminal window
# See what will change
pgbranch diff main feature-auth
# If looks good, proceed with merge
pgbranch merge feature-auth main

Create migration files from diff output:

Terminal window
pgbranch diff main feature-auth --sql > migrations/002_add_auth.sql

Track how your schema evolved:

Terminal window
# Compare original to current
pgbranch diff v1-schema current-schema --stat

When something isn’t working:

Terminal window
# Compare working database against known-good branch
pgbranch diff known-good

The diff command only compares schema, not data. To compare data:

Terminal window
# Use PostgreSQL tools directly
pg_dump --schema-only db1 > schema1.sql
pg_dump --schema-only db2 > schema2.sql
diff schema1.sql schema2.sql

Renames are detected as separate add/remove operations. pgbranch cannot automatically detect that a column was renamed vs. removed and a new one added.

Some PostgreSQL extension-specific objects may not be fully compared.

  • merge - Apply schema changes between branches
  • branch - Create new branches
  • checkout - Switch between branches