Postgres Preview Database for Every Pull Request

Postgres preview database branch created for each pull request

A Postgres preview database for every pull request is easy to set up with Neon branches, and a new demo repo shows what it costs and what it catches. The recipe: on a new pull request, GitHub Actions cuts a Neon branch from main called preview/pr-<number>. It runs migrations, seeds and tests against that branch, then deletes it when the pull request closes.

Under the hood, a Neon branch copies its parent lazily, writing data only when it changes. The preview gets the real schema and data immediately and stores only what the pull request changes. The test used a 309 MB database with 1,002,000 rows, and the numbers below come from real runs on GitHub-hosted runners.

The workflow in three parts

Three workflows do the job. preview-db.yml runs when a pull request is opened, reopened or updated. It makes the branch using neondatabase/create-branch-action (or picks up an existing one), runs migrations, seed and tests, then posts a schema diff and the host and branch name as comments.

A second workflow runs on close. GitHub sends that event for merged and abandoned pull requests alike, so neondatabase/delete-branch-action handles both. A third workflow sweeps every day for any preview/pr-<n> branch when the matching pull request has closed or the branch has outlived a chosen age.

Each branch also gets an expiry 14 days out, so Neon clears it out even when every workflow breaks. The platform caps expiry at 30 days.

You need a migration command, a Neon project with main as default, a NEON_API_KEY secret and a NEON_PROJECT_ID variable. A project-scoped key keeps the token away from other projects. The branch carries over the schema_migrations table from main, so just the pull request’s new migrations execute.

How fast it is

Over 10 runs, the first query on a new branch worked between 1,326 and 1,791 ms after the create request, with a median of 1,438 ms. Every branch held all 1,002,000 rows. Removal took between 1.6 and 2.1 seconds, and a lookup afterwards returned 404 each time.

Across the demo repo’s pull requests, the Neon-based job finished in 16 to 27 seconds. The common alternative, an empty Postgres 18 service container, needed 18 to 31 seconds, partly because pulling and starting the container cost 8 to 12 seconds.

The bug an empty database missed

The third pull request added a unique index on (user_id, lower(url)). Against an empty container the index finished in 2 ms and the suite went green. Against the Neon branch it errored with 23505, since the production-like data held 2,000 bookmarks whose URLs differed only by capital letters in the host.

Merged as written, that migration would have broken the deploy. The corrected migration removed the newer bookmark of each duplicate pair first, and the branch then went green. A container loaded with the same duplicates would also have caught it, but someone must create and keep up that dataset. A branch already has one.

Timing differs too. Another index took 2 ms on an empty table and 2,252 ms on a million rows. Real lock time in production also depends on hardware and traffic, but only the larger figure is in the right range.

Storage and cost

Postgres itself reported sizes of 309 MB, 350 MB and 377 MB for three branches. Neon doesn’t keep separate copies. Branches read the parent’s pages and store just the ones they modify. Neon’s consumption data put the three branches at about 99 MB together, a little under the 109 MB of new indexes measured inside Postgres. The test did not explain the gap.

Priced at $0.35 per GB-month, 99 MB comes to about 3.5 cents monthly. Compute is the bigger bill. The whole test, covering 47 branches across roughly two hours, consumed 1,577 CU-seconds and 13 extra branch-hours, about 13 cents. The rule of thumb is that a preview costs about what its pull request writes. An index on a big table adds roughly the index size. A table rewrite, such as a column type change, should add about the table size, though the test did not try one.

Limits and cleanup

Even on a paid plan, compute runs out before branches do. With 25 branches open, 20 previews responded and 4 failed with an error about too many concurrently active endpoints.

The daily sweep deleted the orphans planted in the test and left the open pull request’s branch alone. It only touches branches named exactly preview/pr-<n>, never the default or protected branches. One warning: its age rule also deletes the branch of an open pull request that outlives the limit, even if a migration is running. The next push recreates it from current main with a new host. Set the maximum age above how long pull requests usually stay open.

Before you copy it

Keep real personal data out of previews: start from an anonymized branch you have verified, or from one holding the schema only. The demo data was synthetic. Pull requests from forks get no secrets in GitHub Actions, so the workflow skips them. Test the sweep with its dry-run option first, and pick a maximum branch age that fits your review cycle.

Source: devops-daily.com

Written by

Saksham Bhargava

Saksham Bhargava runs Alteroid and has been writing about consumer technology for over five years, covering iPhone, Android, Windows, AI tools and gaming. With an MCA and six years of hands-on web development experience, he tests the features he writes about on his own devices and focuses on clear, practical guides that solve real problems. Away from the screen, he enjoys travelling to places most tourists skip.