checkout
The checkout command switches your working database to a different branch’s state.
pgbranch checkout <branch>Arguments
Section titled “Arguments”| Argument | Description |
|---|---|
branch | Name of the branch to switch to (required) |
Examples
Section titled “Examples”Switch to Main Branch
Section titled “Switch to Main Branch”pgbranch checkout mainOutput:
Saving current branch state...Switching to branch 'main'...Switched to branch 'main'Switch to Feature Branch
Section titled “Switch to Feature Branch”pgbranch checkout feature-authHow It Works
Section titled “How It Works”When you checkout a branch, pgbranch performs these steps:
- Saves current state - Updates the current branch’s template database with any changes
- Terminates connections - Disconnects all active database connections
- Drops working database - Removes the current working database
- Restores from template - Creates new working database from target branch’s snapshot
Current state Target branchmyapp_dev ─────────┐ pgbranch_main │ │ ▼ │ Save to │ current branch │ │ │ ▼ │ DROP DATABASE │ myapp_dev │ │ │ └────────────┤ ▼ CREATE DATABASE myapp_dev TEMPLATE pgbranch_main │ ▼ myapp_dev now matches 'main'Important Considerations
Section titled “Important Considerations”Connection Handling
Section titled “Connection Handling”- All active connections are terminated
- Running queries will be cancelled
- Open transactions will be rolled back
- Applications should handle reconnection gracefully
Auto-Save Behavior
Section titled “Auto-Save Behavior”Before switching, pgbranch automatically saves the current database state to the current branch. This means:
- Changes made since the last branch operation are preserved
- You can switch branches freely without losing work
- The saved state becomes the new snapshot for that branch
Error Cases
Section titled “Error Cases”Branch Doesn’t Exist
Section titled “Branch Doesn’t Exist”pgbranch checkout nonexistentError: branch 'nonexistent' does not existAlready On Branch
Section titled “Already On Branch”pgbranch checkout main # when already on mainError: already on branch 'main'Database Connection Issues
Section titled “Database Connection Issues”If PostgreSQL is not accessible:
Error: could not connect to database: connection refusedUse Cases
Section titled “Use Cases”Quick Context Switching
Section titled “Quick Context Switching”# Working on feature, need to check something on mainpgbranch checkout main
# Check the data/schema you neededpsql myapp_dev -c "SELECT * FROM users LIMIT 5"
# Back to feature workpgbranch checkout feature-authTesting Different States
Section titled “Testing Different States”# Test with production-like datapgbranch checkout prod-snapshot
# Test with minimal datapgbranch checkout minimal-seed
# Test with edge casespgbranch checkout edge-case-dataRolling Back Changes
Section titled “Rolling Back Changes”# Before risky operationpgbranch branch pre-experiment
# Try something dangerousnpm run migrate:dangerous
# Didn't work? Roll backpgbranch checkout pre-experimentAutomatic Checkout with Git
Section titled “Automatic Checkout with Git”You can automate checkout when switching git branches:
pgbranch hook installThis installs a post-checkout git hook that runs pgbranch checkout <git-branch> automatically.
Performance
Section titled “Performance”Checkout typically takes 1-5 seconds depending on:
- Database size
- Disk speed
- Number of active connections to terminate
For faster checkouts:
- Keep databases reasonably sized during development
- Close idle database connections
- Use SSD storage