Skip to content

CLI Reference

This page provides a complete reference of all pgbranch commands and their options.

These options are available for all commands:

OptionDescription
--help, -hShow help for command
--version, -vShow version
CommandDescription
initInitialize pgbranch for a database
branchList or create branches
checkoutSwitch to a different branch
deleteDelete a branch
statusShow current branch and status
logShow branch history
diffCompare schemas between branches
mergeMerge schema changes
hookManage git hooks
pruneRemove stale branches
remoteManage remote storage
pushUpload branch to remote
pullDownload branch from remote
keysManage encryption keys
updateCheck for updates

Initialize pgbranch for a database in the current directory.

Terminal window
pgbranch init -d <database> [flags]
FlagShortDescriptionDefault
--database-dDatabase name(required)
--host-HPostgreSQL hostlocalhost
--port-pPostgreSQL port5432
--user-UPostgreSQL userpostgres
--password-WPrompt for password-
Terminal window
pgbranch init -d myapp_dev
pgbranch init -d myapp_dev -H localhost -p 5432 -U postgres -W

List all branches or create a new branch.

Terminal window
pgbranch branch [name]
ArgumentDescription
nameName for new branch (optional)
Terminal window
pgbranch branch # List branches
pgbranch branch main # Create 'main' branch
pgbranch branch feature-api # Create 'feature-api' branch

Switch to a different branch.

Terminal window
pgbranch checkout <branch>
ArgumentDescription
branchBranch to switch to (required)
Terminal window
pgbranch checkout main
pgbranch checkout feature-auth

Delete a branch and its snapshot.

Terminal window
pgbranch delete <branch> [flags]

Alias: rm

ArgumentDescription
branchBranch to delete (required)
FlagShortDescriptionDefault
--force-fForce delete current branchfalse
Terminal window
pgbranch delete old-feature
pgbranch rm old-feature
pgbranch delete current-branch --force

Show current branch and repository status.

Terminal window
pgbranch status
Terminal window
pgbranch status
# Output:
# Database: myapp_dev (localhost:5432)
# Current branch: main
# Total branches: 5

Show branch history with details.

Terminal window
pgbranch log
Terminal window
pgbranch log
# Output:
# * main (current)
# Created: 2024-01-15 10:30:00
# Parent: -

Compare schemas between branches.

Terminal window
pgbranch diff <branch1> [branch2] [flags]
ArgumentDescription
branch1First branch (required)
branch2Second branch (default: working DB)
FlagDescription
--statShow summary statistics only
--sqlShow SQL statements
Terminal window
pgbranch diff main feature-auth
pgbranch diff main # Compare with working DB
pgbranch diff main feature-auth --stat
pgbranch diff main feature-auth --sql

Merge schema changes between branches.

Terminal window
pgbranch merge <source> <target> [flags]
ArgumentDescription
sourceBranch with changes (required)
targetBranch to apply changes to (required)
FlagShortDescriptionDefault
--dry-run-Preview without applyingfalse
--migration-file-Generate migration filefalse
--migration-dir-Migration file directorymigrations
--force-fSkip confirmationfalse
Terminal window
pgbranch merge feature-auth main
pgbranch merge feature-auth main --dry-run
pgbranch merge feature-auth main --migration-file
pgbranch merge feature-auth main --force

Manage git hooks for automatic branch switching.

Terminal window
pgbranch hook <subcommand>
SubcommandDescription
installInstall git post-checkout hook
uninstallRemove git hook
Terminal window
pgbranch hook install
pgbranch hook uninstall

Remove branches not accessed recently.

Terminal window
pgbranch prune [flags]
FlagShortDescriptionDefault
--days-dDays threshold for staleness7
--force-ySkip confirmationfalse
Terminal window
pgbranch prune # Interactive, 7 days
pgbranch prune -d 14 # 14 days threshold
pgbranch prune -d 7 -y # Auto-prune without confirmation

Manage remote storage backends.

Terminal window
pgbranch remote <subcommand> [args] [flags]
SubcommandDescription
add <name> <url>Add a new remote
remove <name>Remove a remote (alias: rm)
listList configured remotes (alias: ls)
ls-remoteList branches on remote
set-default <name>Set default remote
delete <branch>Delete branch from remote
FlagDescription
--no-credentialsSkip credential prompts
FlagShortDescription
--remote-rSpecify remote name
Terminal window
pgbranch remote add origin s3://bucket/prefix
pgbranch remote add local /path/to/dir --no-credentials
pgbranch remote list
pgbranch remote remove origin
pgbranch remote set-default backup
pgbranch remote ls-remote
pgbranch remote ls-remote -r origin
pgbranch remote delete old-branch -r origin

Upload a branch to remote storage.

Terminal window
pgbranch push <branch> [flags]
ArgumentDescription
branchBranch to push (required)
FlagShortDescriptionDefault
--remote-rRemote namedefault
--force-fOverwrite if existsfalse
--description-dAdd description-
Terminal window
pgbranch push main
pgbranch push main -r backup
pgbranch push main --force
pgbranch push main -d "Schema v2.1"

Download a branch from remote storage.

Terminal window
pgbranch pull <branch> [flags]
ArgumentDescription
branchBranch to pull (required)
FlagShortDescriptionDefault
--remote-rRemote namedefault
--as-Local branch namesame
--force-fOverwrite localfalse
Terminal window
pgbranch pull main
pgbranch pull main -r team
pgbranch pull main --as main-backup
pgbranch pull main --force

Manage encryption keys for credentials.

Terminal window
pgbranch keys <subcommand> [flags]
SubcommandDescription
generateGenerate new encryption key
statusShow key status
FlagShortDescription
--force-fRegenerate existing key
Terminal window
pgbranch keys status
pgbranch keys generate
pgbranch keys generate --force

Check for pgbranch updates.

Terminal window
pgbranch update
Terminal window
pgbranch update
# Output:
# Current version: v1.2.0
# Latest version: v1.3.0
# Run: go install github.com/le-vlad/pgbranch/cmd/pgbranch@latest

CodeMeaning
0Success
1General error
2Invalid arguments
3Database connection error
4Remote storage error

pgbranch respects these environment variables:

VariableDescription
PGHOSTPostgreSQL host
PGPORTPostgreSQL port
PGUSERPostgreSQL user
PGPASSWORDPostgreSQL password
PGDATABASEPostgreSQL database
AWS_ACCESS_KEY_IDAWS access key
AWS_SECRET_ACCESS_KEYAWS secret key
AWS_REGIONAWS region
AWS_ENDPOINT_URLCustom S3 endpoint