Database Branching
Database branching allows you to create isolated copies of your database for development, testing, or preview environments. Each branch is a separate PostgreSQL database that can be used independently.
Overview
Section titled “Overview”Use database branches to:
- Test migrations before applying to production
- Create isolated environments for PR previews
- Safely experiment with schema changes
- Run integration tests with real data structures
Quick Start with CLI
Section titled “Quick Start with CLI”The easiest way to work with branches is using the Fluxbase CLI. The server handles all the database operations - you just run commands.
Common Workflow
Section titled “Common Workflow”# Create a branch for your featurefluxbase branch create my-feature
# Work with the branch (it's automatically used by the CLI)fluxbase branch get my-feature
# When done, delete the branchfluxbase branch delete my-featureEssential Commands
Section titled “Essential Commands”# Create a branch (copies schema from main by default)fluxbase branch create my-feature
# Create a branch with full data copyfluxbase branch create my-feature --clone-mode full_clone
# List all branchesfluxbase branch list
# Show branch detailsfluxbase branch get my-feature
# Reset branch to parent state (useful for testing migrations)fluxbase branch reset my-feature
# Delete a branchfluxbase branch delete my-featureCreating Nested Branches
Section titled “Creating Nested Branches”Create a branch from another branch:
# Create feature-b from feature-a (instead of main)fluxbase branch create feature-b --from feature-aThis creates a chain: main → feature-a → feature-b
Server Configuration
Section titled “Server Configuration”This section is for server operators only. Users of the CLI or API don’t need to configure anything - the server handles branching automatically.
Prerequisites
Section titled “Prerequisites”The PostgreSQL user must have CREATE DATABASE privilege. Grant it if needed:
-- Connect to postgres database as superuserGRANT CREATE ON DATABASE postgres TO your_fluxbase_user;Minimal Configuration
Section titled “Minimal Configuration”Enable branching in your fluxbase.yaml:
branching: enabled: trueThat’s it! The server will use its existing database credentials to create and manage branches.
Optional Settings
Section titled “Optional Settings”branching: enabled: true max_total_branches: 50 # Maximum total branches across all users max_branches_per_user: 5 # Maximum branches per user (default: 5) default_data_clone_mode: schema_only auto_delete_after: 24h database_prefix: branch_| Option | Default | Description |
|---|---|---|
enabled | false | Enable database branching |
max_total_branches | 50 | Maximum total branches across all users |
default_data_clone_mode | schema_only | Default cloning mode (schema_only, full_clone, seed_data) |
auto_delete_after | 0 | Auto-delete preview branches after this duration (0 = disabled) |
database_prefix | branch_ | Prefix for branch database names |
Note: When auto_delete_after is set (e.g., 24h, 7d), a background cleanup scheduler runs automatically to delete expired branches.
Data Clone Modes
Section titled “Data Clone Modes”| Mode | Description |
|---|---|
schema_only | Copy schema only, no data (fast) |
full_clone | Copy schema and all data (slower, useful for testing with real data) |
seed_data | Copy schema with seed data (coming soon) |
How It Works
Section titled “How It Works”When you create a branch, the server:
- Uses its database credentials to connect to the
postgresdatabase - Executes
CREATE DATABASE branch_my_feature(or similar) - Copies the schema (and optionally data) from the parent branch
- Tracks the branch metadata in the
branching.branchestable
The server never needs separate admin credentials - it uses the same PostgreSQL user it already has.
Using TypeScript SDK
Section titled “Using TypeScript SDK”import { createClient } from '@fluxbase/sdk'
const client = createClient('http://localhost:8080', 'your-key')
// Create a branchconst { data: branch } = await client.branching.create('my-feature', { dataCloneMode: 'schema_only', expiresIn: '7d'})
// Wait for it to be readyawait client.branching.waitForReady('my-feature')
// Delete when doneawait client.branching.delete('my-feature')See the TypeScript SDK Branching Guide for complete documentation.
Using the REST API
Section titled “Using the REST API”For advanced users and custom integrations:
# Create a branchcurl -X POST http://localhost:8080/api/v1/admin/branches \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{"name": "my-feature"}'
# Access branch datacurl http://localhost:8080/api/v1/tables/users \ -H "Authorization: Bearer $TOKEN" \ -H "X-Fluxbase-Branch: my-feature"Branch Types
Section titled “Branch Types”| Type | Description | Auto-Delete |
|---|---|---|
main | Primary database | Never |
preview | Temporary environments | After auto_delete_after |
persistent | Long-lived branches | Never |
Connecting to Branches
Section titled “Connecting to Branches”Via HTTP Header
Section titled “Via HTTP Header”Include the X-Fluxbase-Branch header in your requests:
curl http://localhost:8080/api/v1/tables/users \ -H "X-Fluxbase-Branch: my-feature"Via Query Parameter
Section titled “Via Query Parameter”Append ?branch= to the URL:
curl "http://localhost:8080/api/v1/tables/users?branch=my-feature"Direct Database Connection
Section titled “Direct Database Connection”Get the branch connection URL for direct PostgreSQL access:
fluxbase branch get my-feature --output json | jq -r .connection_urlBranch Lifecycle
Section titled “Branch Lifecycle”┌─────────────────────────────────────────────────────────────┐│ MAIN BRANCH ││ (Always exists) │└───────────────────────────┬─────────────────────────────────┘ │ ▼┌───────────────────────────────────────────────────────────────┐│ Creating → Ready → Migrating → Ready → Deleting ││ (new) (use) (update) (use) (cleanup) │└───────────────────────────────────────────────────────────────┘States
Section titled “States”| State | Description |
|---|---|
creating | Database is being created |
ready | Branch is available for use |
migrating | Migrations are running |
error | An error occurred |
deleting | Branch is being deleted |
deleted | Branch has been deleted |
Access Control
Section titled “Access Control”Branch access is controlled by:
- Creator - Automatically has admin access
- Explicit Grants - Can grant read/write/admin access to others
- Service Keys - Have full access to all branches
- Dashboard Admins - Have full access to all branches
Access Levels
Section titled “Access Levels”| Level | Permissions |
|---|---|
read | View branch, query data |
write | Read + modify data |
admin | Write + delete/reset branch |
Next Steps
Section titled “Next Steps”- Branching Workflows - Development workflow examples
- GitHub Integration - Automatic PR branches
- TypeScript SDK Branching - SDK documentation
- Security Best Practices - Security considerations