Why preview environments that share a staging database break each other, how database-per-PR, schema-per-PR and copy-on-write clones compare, how to seed and clean up preview databases, and a Go example where every preview environment gets its own migrated, seeded Postgres database.

A preview environment that shares the staging database is only half isolated. The first pull request that adds a migration changes the schema for staging and every other preview, and a test that deletes rows deletes them for everyone.
Giving each pull request its own database fixes that, provided that creating, seeding and dropping it happens without anyone remembering to do it. This guide compares the ways to set that up.
Create a database when the pull request's preview environment is created, apply the migrations from that branch, load seed data, and drop the database when the pull request is closed or merged. The options differ mostly in how the database gets created and where its data comes from.
Migrations collide first: pull request A adds a column and its preview applies the migration to the shared database, so staging and pull request B now run against a schema their code has never seen, and if A is closed instead of merged, the column stays behind with no migration file in main to explain it. Destructive migrations make it worse, since a preview that drops a column breaks every other environment reading it.
Shared data causes the next round of failures, where a reviewer testing a refund flow in one preview changes balances that another reviewer is looking at, and automated tests that create and delete records start failing for reasons unrelated to the code under review.
A schema per pull request is the cheapest option, since every preview lives in one database and switches with search_path, as covered in the Postgres schemas docs. The cost shows up in shared state: extensions are installed per database, roles are shared, and migration tools that write to public by default need per-preview configuration.
A database per pull request on a shared instance keeps the server cost the same and isolates schema, extensions and data. Postgres can create it from a template with CREATE DATABASE ... TEMPLATE, which copies an already migrated database instead of replaying every migration. The remaining shared resource is the server itself, so a preview running a heavy import slows the others and all of them count against one max_connections limit.
Production data makes previews realistic and also copies customer emails, addresses and payment details into short-lived environments that more people can reach, often without the access logging production has. Most teams seed previews with fixtures, or copy from a staging database whose data was generated rather than collected.
When a copy is the right call, copy-on-write clones make it fast. Aurora cloning creates a new cluster that shares storage pages with the source until either side writes, so creation is quick and storage grows only with changes. A Cloud SQL clone creates a full new instance, which is simpler to reason about and takes longer to become available. Restoring from a pg_dump works everywhere and gets slower with every gigabyte.
Run the seed after the migrations, make it idempotent so a redeploy of the same pull request does not fail or duplicate rows, and guard it by environment so it can never run against staging or production. INSERT ... ON CONFLICT DO NOTHING with fixed keys covers the first two, and checking the environment name or type in code covers the third. Keep the fixtures small and targeted at what reviewers need to click through, such as one customer with an active subscription and one with a failed payment.
Drop the database in a CI job triggered by the pull request's closed event, which fires for both merged and abandoned pull requests. Add a scheduled sweep that drops preview databases whose pull request is no longer open, since a failed teardown job or a deleted branch leaves databases behind, and on a shared instance those keep using disk and connections until someone notices.
Encore brings infrastructure from code, so a pull request's preview environment gets exactly the databases that branch declares, each one created fresh for that pull request with the branch's migrations applied on deploy. Once the app is linked to GitHub, every pull request gets a preview environment hosted by Encore, named after the pull request, such as pr:72 with the API at https://pr72-$APP_ID.encr.app.

Run in your terminal to get started locally.
The catalog service's migration creates the table its preview will start with:
-- catalog/migrations/1_create_products.up.sql
CREATE TABLE products (
sku TEXT PRIMARY KEY,
name TEXT NOT NULL,
price_cents BIGINT NOT NULL CHECK (price_cents >= 0)
);
The fixtures use fixed keys and ON CONFLICT DO NOTHING, so a redeploy of the same pull request leaves existing rows alone:
-- catalog/fixtures.sql
-- Safe to run on every startup: existing rows are left alone.
INSERT INTO products (sku, name, price_cents) VALUES
('TEST-MUG', 'Test mug', 1200),
('TEST-SHIRT', 'Test shirt', 2500),
('TEST-FREE', 'Free sample', 0)
ON CONFLICT (sku) DO NOTHING;
The service embeds the fixtures and runs them at startup only locally and in preview environments, using the environment type from the metadata API:
// catalog/catalog.go
package catalog
import (
"context"
_ "embed"
"encore.dev"
"encore.dev/rlog"
"encore.dev/storage/sqldb"
)
// Every preview environment gets its own copy of this database,
// with the migrations in ./migrations applied on deploy.
var db = sqldb.NewDatabase("catalog", sqldb.DatabaseConfig{
Migrations: "./migrations",
})
//go:embed fixtures.sql
var fixtures string
func init() {
env := encore.Meta().Environment
local := env.Cloud == encore.CloudLocal && env.Type != encore.EnvTest
preview := env.Type == encore.EnvEphemeral
if !local && !preview {
return // never seed staging or production
}
if _, err := db.Exec(context.Background(), fixtures); err != nil {
rlog.Error("seeding fixtures failed", "err", err)
}
}
type Product struct {
SKU string `json:"sku"`
Name string `json:"name"`
PriceCents int64 `json:"priceCents"`
}
type ListResponse struct {
Products []Product `json:"products"`
}
//encore:api public method=GET path=/products
func List(ctx context.Context) (*ListResponse, error) {
rows, err := db.Query(ctx, `SELECT sku, name, price_cents FROM products ORDER BY sku`)
if err != nil {
return nil, err
}
defer rows.Close()
resp := &ListResponse{}
for rows.Next() {
var p Product
if err := rows.Scan(&p.SKU, &p.Name, &p.PriceCents); err != nil {
return nil, err
}
resp.Products = append(resp.Products, p)
}
return resp, rows.Err()
}
EnvEphemeral is the environment type Encore uses for short-lived environments such as previews, and the test run is excluded so encore test keeps its own clean databases. This follows the go:embed approach in the test data docs, which also mention deployment webhooks as a way to seed previews from outside the app. If a migration in the pull request fails, Encore rolls it back and aborts that preview's deploy, so the broken migration never reaches staging.
Instead of fixtures, you can have each preview start from a copy of another environment's database, such as staging, by choosing the source environment under App Settings, then Preview Environments, in the Platform Dashboard. That copy uses Neon database branching, the default database provider for environments hosted by Encore, so the source environment's database has to run on Neon as well, and it is a good fit when staging holds synthetic data you want reviewers to see.
Cron jobs do not run in preview environments, so scheduled work in a pull request is tested by calling its endpoint directly. Opening the pull request is enough for the rest, and running encore gen client --env=pr:72 --lang=typescript my-app gives a frontend developer a typed client for that preview's API.
With the database declared in the same code an agent edits, a coding agent that adds a table, its migration and a few fixture rows in one pull request gets a preview where reviewers can click through that change with data already in place.
Best fit: Teams on AWS or GCP that want every pull request to come with its own migrated, seeded database and a URL reviewers can open, with production in their own cloud account.
No. A shared database means the first pull request with a migration changes the schema for staging and every other preview, and test data written in one preview shows up in all of them. Give each pull request its own database, created from the branch's migrations, and drop it when the pull request closes.
No, not in most cases, since production data brings personal and payment data into short-lived environments that more people can reach, often without the access controls production has. Seed previews with fixtures or with a copy of a staging database whose data is synthetic.
Yes. It works for small apps, and it is cheap, but every preview shares the same database roles, extensions and connection limit, and migration tools that assume the public schema need extra configuration. A separate database per pull request on a shared instance gives cleaner isolation at a similar cost.
Embed an idempotent fixtures.sql file in the service and execute it at startup when encore.Meta().Environment.Type is encore.EnvEphemeral, which is the environment type of preview environments. Encore creates the preview's database and applies the migrations on deploy, so the fixtures run against the branch's schema.
Deploy to your cloud on AWS and Google Cloud
without managing infrastructure
Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.