Logo
Productspgbranch
pgbranch

pgbranch

Branch your Postgres database like you branch code

Introduction to pgbranch

pgbranch is a tool designed to bring the power of version control to PostgreSQL databases. Just as developers use Git to manage code branches, pgbranch allows users to manage database branches, making it easy to switch between different database states without losing data or encountering inconsistencies.

The main problem that pgbranch solves is the difficulty of managing database migrations when switching between git branches. When working on a feature branch, database changes such as migrations, seed data, and test records can become incompatible with the main branch. Switching back to main often results in a broken database state, requiring manual intervention to reset the database. pgbranch eliminates this issue by creating instant snapshots of the database state, allowing users to switch between branches seamlessly and reliably.

Takeaways

  • Create and restore database snapshots instantly using PostgreSQL's native template database feature.
  • Compare and merge schema changes between branches.
  • Share database snapshots with your team using remote storage options like AWS S3, MinIO, and Cloudflare R2.
  • Integrate with Git to automatically switch database branches when switching git branches.
  • Encrypt remote storage credentials for secure configuration management.

How pgbranch Works

pgbranch leverages PostgreSQL's template database functionality to create lightweight, fast snapshots of the database state. When you run pgbranch branch <name>, it creates a new branch that is a copy of the current database state. These branches are not full copies but rather references to the original, which makes them very efficient.

To switch between branches, you use the pgbranch checkout <name> command, which drops the current database and restores it from the selected snapshot. This process is fast and ensures that your database is always in a known, consistent state.

Core Benefits and Applications

BenefitDescription
Fast SwitchingSwitch between database states instantly without manual rollbacks
Schema ManagementCompare and merge schema differences between branches
Remote CollaborationShare database snapshots across teams using cloud storage
Git IntegrationAutomatically sync database branches with git branches
Secure ConfigurationEncrypt remote storage credentials for safe version control

pgbranch is particularly useful for development workflows where multiple feature branches require isolated database states. It helps prevent common issues like broken databases, inconsistent migrations, and lost work due to context switching.