---
title: "A clean Postgres branch for every CI run | Xata"
description: "Create a Postgres branch at the start of the pipeline, migrate and test against it, delete it at the end. No shared test database and no cleanup scripts."
source: "https://xata.io/solutions/ci-cd"
---

# A clean database for every test run

Create a Postgres branch when the pipeline starts, run migrations and tests against it, and delete it when the job ends. Each run tests against an untouched copy of your data.

[Read the docs](/docs/automations/ga-pr)[Get started](https://console.xata.io/signup)

### A fresh copy every run

Each run tests against an untouched copy of your data.

### Parallel jobs

Each job gets its own branch. Nothing is shared.

### Deleted at the end

The last step removes the branch.

The problem

## Flaky tests often start in the database

Tests fail for reasons that have nothing to do with the code under test.

- A shared test database turns CI into a queue
- Parallel jobs trip over each other’s rows
- Cleanup scripts miss a table
- A migration from one branch leaks into another branch’s run
- Mocking the database stops testing what production runs

How it works

## Create, test, delete. Every run.

1. 01

   ### Create

   The first step creates a branch from your baseline: production, staging, or a masked copy. It copies no data, so it is ready in about three seconds.

2. 02

   ### Migrate and test

   Apply pending migrations, seed if you need to, run the suite. Integration tests hit real Postgres with the real data shape.

3. 03

   ### Delete

   The last step deletes the branch. Nothing carries over to the next run.

The whole pipeline

### Four commands, any CI

The same commands run anywhere a shell does.

Set it up

## Works with the CI you already run

GitHub Actions

### Copy the workflows

- GitHub Actions

1. 01Create a branch for the job
2. 02Block the merge if the migration fails
3. 03Sweep up branches left behind by cancelled jobs

- [Branch per pull request](/docs/automations/ga-pr)
- [Merge readiness check](/docs/automations/ga-check)
- [Cleanup workflow](/docs/automations/ga-cleanup)

Any other CI

### GitLab, CircleCI, Buildkite, Jenkins

1. 01Install the CLI
2. 02Set XATA_API_KEY to a key scoped to the one project
3. 03Store it in your CI secret manager

Install

- [CLI docs](/docs/cli)

Agent-driven

### Agents run the suite before you review

- Claude Code
- Cursor
- Codex

1. 01Create a branch for the task
2. 02Migrate, test and report
3. 03Delete it before a human sees the change

- [Xata for coding agents](/docs/ai-agents/overview)

Coding agents

## Your agent can run the whole workflow

Connect the Xata MCP server or install the CLI, and your coding agent can create branches, apply migrations and read schema without touching production.

- Claude Code
- Cursor
- Codex
- VS Code (Copilot)
- Windsurf
- Cline
- OpenCode
- Zed

### [Xata for coding agents](/docs/ai-agents/overview)

[CLI, MCP and agent skills in one place.](/docs/ai-agents/overview)

[Read the guide](/docs/ai-agents/overview)

### [MCP server](/docs/platform/mcp)

[Setup for every supported client.](/docs/platform/mcp)

[Connect MCP](/docs/platform/mcp)

Keep it cheap and safe

## No bill for cancelled jobs, no exposed data

### [Cancelled jobs do not leave a bill](/scale-to-zero)

[If a job is cancelled before its delete step, the leftover branch can scale to zero until the cleanup workflow removes it.](/scale-to-zero)

[Learn more](/scale-to-zero)

### [Test against real data without exposing it](/postgres-data-masking)

[If production holds personal data, branch CI runs from a masked copy. The distributions stay realistic and the values do not.](/postgres-data-masking)

[Learn more](/postgres-data-masking)

## More branching workflows

### [One branch per pull request](/solutions/pull-request)

[Test every migration before it merges.](/solutions/pull-request)

[Learn more](/solutions/pull-request)

### [One branch per preview](/solutions/preview)

[A database for every Vercel and Netlify preview.](/solutions/preview)

[Learn more](/solutions/preview)

### [Database Branching](/postgres-branching)

[How Xata branches Postgres.](/postgres-branching)

[Learn more](/postgres-branching)

## Give every test run its own database

Stop sharing a test database. Branch it per run and delete it when the job ends.

[Get started](https://console.xata.io/signup)[Talk to an engineer](https://cal.com/team/xata/xata-sales)
