Skip to content

Quickstart

This guide will walk you through setting up pgbranch for a project and creating your first database branches.

  • pgbranch installed (Installation guide)
  • A PostgreSQL database for your project
  • PostgreSQL running and accessible

Navigate to your project directory and initialize pgbranch:

Terminal window
cd your-project
pgbranch init -d myapp_dev

This creates a .pgbranch directory in your project root containing:

  • Configuration file
  • Branch metadata
  • Encryption keys for remote credentials

You can specify PostgreSQL connection details during init:

Terminal window
# Full connection specification
pgbranch init -d myapp_dev -H localhost -p 5432 -U postgres
# With password prompt
pgbranch init -d myapp_dev -W
FlagDescriptionDefault
-d, --databaseDatabase name(required)
-H, --hostPostgreSQL hostlocalhost
-p, --portPostgreSQL port5432
-U, --userPostgreSQL userpostgres
-W, --passwordPrompt for password-

With your database in a clean state (e.g., after running migrations), create a branch:

Terminal window
pgbranch branch main

This creates a snapshot of your current database state. You can verify it was created:

Terminal window
pgbranch branch

Output:

* main

The * 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:

Terminal window
# Run some migrations or make database changes
# For example, add a new table in your app
# Save this state as a new branch
pgbranch branch feature-auth

List your branches:

Terminal window
pgbranch branch

Output:

main
* feature-auth

Need to go back to the main branch? Easy:

Terminal window
pgbranch checkout main

Your database is now back to the exact state it was when you created the main branch.

View your current state anytime:

Terminal window
pgbranch status

Output:

Database: myapp_dev (localhost:5432)
Current branch: main
Total branches: 2

Here’s a typical development workflow:

Terminal window
# Start fresh - create a main branch from clean database
pgbranch init -d myapp_dev
pgbranch branch main
# Start working on a feature
git checkout -b feature/user-profiles
# Run migrations, add test data...
pgbranch branch feature-profiles
# Urgent bug fix needed on main
git checkout main
pgbranch checkout main
# Fix the bug, your database is now in the main state
# Back to feature work
git checkout feature/user-profiles
pgbranch checkout feature-profiles
# Your feature database state is restored!

Tired of running pgbranch checkout manually? Install the git hook:

Terminal window
pgbranch hook install

Now pgbranch will automatically switch database branches when you switch git branches (if a matching branch exists).

Learn more about git hooks