Quickstart
This guide will walk you through setting up pgbranch for a project and creating your first database branches.
Prerequisites
Section titled “Prerequisites”- pgbranch installed (Installation guide)
- A PostgreSQL database for your project
- PostgreSQL running and accessible
Step 1: Initialize pgbranch
Section titled “Step 1: Initialize pgbranch”Navigate to your project directory and initialize pgbranch:
cd your-projectpgbranch init -d myapp_devThis creates a .pgbranch directory in your project root containing:
- Configuration file
- Branch metadata
- Encryption keys for remote credentials
Connection Options
Section titled “Connection Options”You can specify PostgreSQL connection details during init:
# Full connection specificationpgbranch init -d myapp_dev -H localhost -p 5432 -U postgres
# With password promptpgbranch init -d myapp_dev -W| Flag | Description | Default |
|---|---|---|
-d, --database | Database name | (required) |
-H, --host | PostgreSQL host | localhost |
-p, --port | PostgreSQL port | 5432 |
-U, --user | PostgreSQL user | postgres |
-W, --password | Prompt for password | - |
Step 2: Create Your First Branch
Section titled “Step 2: Create Your First Branch”With your database in a clean state (e.g., after running migrations), create a branch:
pgbranch branch mainThis creates a snapshot of your current database state. You can verify it was created:
pgbranch branchOutput:
* mainThe * indicates this is your current branch.
Step 3: Make Changes and Create Another Branch
Section titled “Step 3: Make Changes and Create Another Branch”Now let’s simulate working on a feature:
# Run some migrations or make database changes# For example, add a new table in your app
# Save this state as a new branchpgbranch branch feature-authList your branches:
pgbranch branchOutput:
main* feature-authStep 4: Switch Between Branches
Section titled “Step 4: Switch Between Branches”Need to go back to the main branch? Easy:
pgbranch checkout mainYour database is now back to the exact state it was when you created the main branch.
Step 5: Check Status
Section titled “Step 5: Check Status”View your current state anytime:
pgbranch statusOutput:
Database: myapp_dev (localhost:5432)Current branch: mainTotal branches: 2Complete Workflow Example
Section titled “Complete Workflow Example”Here’s a typical development workflow:
# Start fresh - create a main branch from clean databasepgbranch init -d myapp_devpgbranch branch main
# Start working on a featuregit checkout -b feature/user-profiles# Run migrations, add test data...pgbranch branch feature-profiles
# Urgent bug fix needed on maingit checkout mainpgbranch checkout main# Fix the bug, your database is now in the main state
# Back to feature workgit checkout feature/user-profilespgbranch checkout feature-profiles# Your feature database state is restored!Automatic Branch Switching
Section titled “Automatic Branch Switching”Tired of running pgbranch checkout manually? Install the git hook:
pgbranch hook installNow pgbranch will automatically switch database branches when you switch git branches (if a matching branch exists).