branch
The branch command lists existing branches or creates a new branch from the current database state.
# List all branchespgbranch branch
# Create a new branchpgbranch branch <name>Arguments
Section titled “Arguments”| Argument | Description |
|---|---|
name | Name for the new branch (optional) |
Examples
Section titled “Examples”List All Branches
Section titled “List All Branches”pgbranch branchOutput:
main* feature-auth feature-paymentsThe * indicates the current branch.
Create a New Branch
Section titled “Create a New Branch”pgbranch branch feature-notificationsOutput:
Branch 'feature-notifications' createdCreate Initial Branch
Section titled “Create Initial Branch”After initializing pgbranch, create your first branch:
pgbranch init -d myapp_devpgbranch branch mainHow It Works
Section titled “How It Works”When you create a branch, pgbranch:
- Terminates active connections to the working database
- Creates a template database as a snapshot
- Records metadata (creation time, parent branch)
The template database is named: pgbranch_<branch_name>_<project_hash>
Working database: myapp_dev │ ▼ CREATE DATABASE pgbranch_feature_auth_abc123 TEMPLATE myapp_dev │ ▼Snapshot stored as template databaseBranch Naming
Section titled “Branch Naming”Branch names should:
- Contain only alphanumeric characters, hyphens, and underscores
- Not start with a number
- Be reasonably short (PostgreSQL has a 63-character limit on identifiers)
Good branch names:
mainfeature-authbugfix-login-issuev2-schemaBranching Strategy
Section titled “Branching Strategy”Mirror Git Branches
Section titled “Mirror Git Branches”A common strategy is to mirror your git branch names:
git checkout -b feature/user-profilespgbranch branch feature-user-profilesSnapshot Points
Section titled “Snapshot Points”Create branches at significant points:
# After fresh migrationpgbranch branch clean-schema
# After seeding test datapgbranch branch with-seed-data
# Before risky migrationpgbranch branch pre-migrationViewing Branch Details
Section titled “Viewing Branch Details”To see more information about branches, use the log command:
pgbranch logOutput:
* main (current) Created: 2024-01-15 10:30:00 Parent: -
feature-auth Created: 2024-01-15 14:22:00 Parent: mainStorage Considerations
Section titled “Storage Considerations”Each branch creates a complete database copy. Monitor disk usage:
# Check PostgreSQL database sizespsql -c "SELECT pg_database.datname, pg_size_pretty(pg_database_size(pg_database.datname)) FROM pg_database WHERE datname LIKE 'pgbranch_%';"Use pgbranch prune to clean up old branches:
pgbranch prune -d 7 # Remove branches not accessed in 7 daysCommon Workflows
Section titled “Common Workflows”Feature Development
Section titled “Feature Development”# Start from mainpgbranch checkout main
# Create feature branchpgbranch branch feature-new-api
# Work on feature...# Run migrations, add test data
# Switch back to main for a hotfixpgbranch checkout mainpgbranch branch hotfix-critical-bugTesting Migrations
Section titled “Testing Migrations”# Save current statepgbranch branch before-migration
# Run migrationnpm run migrate
# If migration fails, restorepgbranch checkout before-migration