Encryption
pgbranch uses encryption to protect sensitive credentials stored in configuration files.
Overview
Section titled “Overview”When you configure remote storage (S3, R2, etc.), the credentials are encrypted before being stored in .pgbranch/config.json. This allows you to:
- Safely commit config files to version control
- Share project configuration without exposing secrets
- Keep credentials secure on disk
Encryption Key
Section titled “Encryption Key”Key Location
Section titled “Key Location”The encryption key is stored at:
~/.pgbranch_keyThis is a user-level key, separate from project files.
Key Generation
Section titled “Key Generation”A key is automatically generated when you:
- Run
pgbranch initfor the first time - Add your first remote with credentials
You can also manually generate a key:
pgbranch keys generateOutput:
Encryption key generated at ~/.pgbranch_keyKey Status
Section titled “Key Status”Check if you have an encryption key:
pgbranch keys statusOutput:
Encryption key: ~/.pgbranch_keyStatus: ActiveCreated: 2024-01-15 10:30:00What Gets Encrypted
Section titled “What Gets Encrypted”Encrypted
Section titled “Encrypted”- AWS Access Key ID
- AWS Secret Access Key
- R2/MinIO credentials
- Any authentication tokens
Not Encrypted
Section titled “Not Encrypted”- Remote URLs
- Remote names
- Configuration settings
- Branch metadata
Configuration File Example
Section titled “Configuration File Example”After adding a remote, your config looks like:
{ "database": "myapp_dev", "host": "localhost", "port": 5432, "user": "postgres", "remotes": { "origin": { "name": "origin", "type": "s3", "url": "s3://my-bucket/pgbranch", "options": { "region": "us-east-1", "accessKeyId": "ENC:v1:base64encodedciphertext...", "secretAccessKey": "ENC:v1:base64encodedciphertext..." } } }}The ENC:v1: prefix indicates encrypted values.
Key Management
Section titled “Key Management”Regenerating Keys
Section titled “Regenerating Keys”If your key is compromised:
pgbranch keys generate --forceBacking Up Keys
Section titled “Backing Up Keys”Back up your encryption key securely:
# Copy to secure locationcp ~/.pgbranch_key /secure/backup/location/
# Or view the key to store elsewherecat ~/.pgbranch_keySharing Keys (Team Use)
Section titled “Sharing Keys (Team Use)”For teams sharing the same remote configuration:
Option 1: Share the key
Securely distribute ~/.pgbranch_key to team members:
# Send via secure channel (not email/Slack)# Each team member places it at ~/.pgbranch_keyOption 2: Use separate credentials
Each team member adds the remote with their own credentials:
pgbranch remote add origin s3://bucket/prefix# Each person enters their own AWS credentialsOption 3: Use environment variables
Skip credential storage entirely:
pgbranch remote add origin s3://bucket/prefix --no-credentialsThen set environment variables:
export AWS_ACCESS_KEY_ID=xxxexport AWS_SECRET_ACCESS_KEY=xxxEncryption Algorithm
Section titled “Encryption Algorithm”pgbranch uses:
- Algorithm: AES-256-GCM
- Key derivation: 256-bit random key
- Format:
ENC:v1:<base64-encoded-ciphertext>
The encryption is designed for local credential protection, not for securing data in transit (HTTPS handles that).
Security Best Practices
Section titled “Security Best Practices”- Keep
~/.pgbranch_keysecure and backed up - Use different AWS IAM users per developer
- Rotate credentials periodically
- Use environment variables in CI/CD
- Commit
~/.pgbranch_keyto version control - Share the encryption key via insecure channels
- Use the same credentials for production and development
.gitignore Recommendations
Section titled “.gitignore Recommendations”Add to your .gitignore:
# pgbranch sensitive files.pgbranch/config.json # If it contains DB passwords
# Note: config.json with only remote credentials# (encrypted) is safe to commitFor the encryption key (which is outside your project):
# The key is at ~/.pgbranch_key# It's already outside your repo, so no .gitignore neededTroubleshooting
Section titled “Troubleshooting””Could not decrypt credentials”
Section titled “”Could not decrypt credentials””Error: could not decrypt credentials: invalid keyCauses:
- Wrong or missing encryption key
- Key was regenerated after credentials were encrypted
Solution:
# Check key statuspgbranch keys status
# Re-add the remotepgbranch remote remove originpgbranch remote add origin s3://bucket/prefix“Encryption key not found”
Section titled ““Encryption key not found””Error: encryption key not found at ~/.pgbranch_keySolution:
pgbranch keys generateThen re-add your remotes.
Moving to a New Machine
Section titled “Moving to a New Machine”-
Copy the encryption key:
Terminal window # On old machinecat ~/.pgbranch_key# On new machineecho "your-key-content" > ~/.pgbranch_keychmod 600 ~/.pgbranch_key -
Clone your project (with .pgbranch/config.json)
-
Initialize pgbranch:
Terminal window pgbranch init -d myapp_dev -
Test remote access:
Terminal window pgbranch remote ls-remote
CI/CD Integration
Section titled “CI/CD Integration”In CI/CD, avoid using encrypted credentials. Instead:
GitHub Actions
Section titled “GitHub Actions”env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
steps: - name: Setup pgbranch remote run: | pgbranch init -d test_db pgbranch remote add origin s3://bucket/prefix --no-credentials pgbranch pull seed-dataGitLab CI
Section titled “GitLab CI”variables: AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
test: script: - pgbranch init -d test_db - pgbranch remote add origin s3://bucket/prefix --no-credentials - pgbranch pull seed-dataRelated Topics
Section titled “Related Topics”- Remote Setup - Configure remote storage
- Configuration - Config file reference