Problem
Schema changes during development can corrupt or pollute your main database. You need isolated environments to test migrations without affecting production data.Solution
Neon’s branching feature creates instant copy-on-write database branches from your main database. Each branch is isolated, cheap (only stores diffs), and can be created/deleted in seconds.Implementation
Setup
Install and authenticate the Neon CLI:.neon file in your project root with your project ID (find it in your Neon dashboard):
.neon
Helper Scripts
Add these scripts to yourpackage.json:
package.json
Branch Script
scripts/neon-branch.js
Usage
Development Workflow
Commands Reference
| Command | Description |
|---|---|
npm run db:branch:start [name] | Create and switch to branch |
npm run db:branch:stop [name] | Delete branch, switch to main |
npm run db:branch:use [name] | Switch to branch |
npm run db:branch:create [name] | Create branch only |
npm run db:branch:delete [name] | Delete branch only |
npm run db:branch:list | List all branches |
dev-local if not specified.
Considerations
Benefits:- Instant branch creation (copy-on-write, no data duplication)
- Test destructive migrations safely
- Multiple developers can have separate branches
- Branches inherit data from parent at creation time
- Requires Neon PostgreSQL (not compatible with other providers)
- Branch data becomes stale relative to main over time
- Free tier has limited branch count
- Docker-based local PostgreSQL for full isolation
- Schema-only branches (no data) for simpler cases
- Supabase branching for Supabase users