# Should Parallel Coding Agents Share One Dev Database?

> Don't share one dev database between parallel agents that write. In our test, one agent's migration and fixture reset broke the other's test. Give each agent its own copy. On one Postgres in a small sandbox, CREATE DATABASE … TEMPLATE copied 143 MB in about 0.5 s (2026-11-19). With one VM per agent, start each from a named snapshot.

Source: https://sandboxer.com/guides/give-each-coding-agent-its-own-database. Updated 2026-11-19.

**Short answer:** Don't share one dev database between parallel agents that write. In our test, one agent's migration and fixture reset broke the other's test. Give each agent its own copy. On one Postgres in a small sandbox, CREATE DATABASE … TEMPLATE copied 143 MB in about 0.5 s (2026-11-19). With one VM per agent, start each from a named snapshot.

Part of [Docker, Kubernetes and Nested VMs in a Cloud Sandbox](/guides/docker-and-virtualization-in-a-cloud-sandbox) and [Coding Agents in the Cloud: Parallel, Overnight, Own Keys, Checkpoints](/guides/coding-agents-in-the-cloud).

This page is about the database only. Whether agents should get git worktrees or a VM each is covered in [Git worktrees or one VM per agent](/guides/git-worktrees-or-one-vm-per-agent).

## At a glance

| Setup | What each agent gets | Time to a fresh copy, measured | Use it when |
| - | - | - | - |
| One shared dev database | Nothing of its own | None needed | Never, for agents that migrate, seed or truncate. Our two agents broke each other within 2 seconds |
| One shared database, read-only role | Read access to the same rows | None needed | Agents only read fixtures or reference data |
| One database per agent on one Postgres (`CREATE DATABASE … TEMPLATE`) | Its own database; the server, roles, port, CPU and RAM are shared | 0.46–0.67 s for 143 MB (`WAL_LOG`, the default); 0.14–0.15 s after a first 1.0 s run (`FILE_COPY`) | Agents share one machine, for example one worktree each |
| One VM per agent from a named snapshot | Its own Postgres server, disk, port 5432 and RAM | Sandbox ready in 1.2 s, Postgres accepting connections 2.2 s after create; the snapshot saved in 2.1 s | Each agent has its own VM, or also runs Docker, dev servers or other services |

Last checked: 2026-11-19, on prod.

## What we ran

Everything on this page ran in `small` Sandboxer sandboxes (2 vCPU, 4 GB) with `postgres:16` in Docker, which is preinstalled. The seed is 100,000 customers and 2,000,000 orders, 143 MB on disk. The "agents" are two scripted shell functions, not models: each does what a coding agent often does on its branch, which is to run its migration, reset its fixtures and run its tests. The full script is in the copy-paste section at the end, and every output on this page is from it.

## The shared database, reproduced

Agent A adds a `status` text column on its branch and then runs a test that expects all 2,000,000 orders. Agent B, a second later, adds a `status` column of a different type on its branch, then truncates `orders` to reset its fixtures. Both point at the same database:

```bash
p() { docker exec pg psql -U postgres -X -At "$@" 2>&1; }
agent_a() {
  p -d shared -c "ALTER TABLE orders ADD COLUMN status text NOT NULL DEFAULT 'new'" | sed 's/^/agent A: /'
  sleep 2
  echo "agent A: test sees $(p -d shared -c 'SELECT count(*) FROM orders') orders, expects 2000000"
}
agent_b() {
  sleep 1
  p -d shared -c "ALTER TABLE orders ADD COLUMN status smallint NOT NULL DEFAULT 0" | sed 's/^/agent B: /'
  p -d shared -c 'TRUNCATE orders' | sed 's/^/agent B: /'
}
agent_a & agent_b & wait
```

```text
agent A: ALTER TABLE
agent B: ERROR:  column "status" of relation "orders" already exists
agent B: TRUNCATE TABLE
agent A: test sees 0 orders, expects 2000000
```

Neither agent did anything wrong on its own branch. Agent B's migration failed because of a column that exists only on agent A's branch, and agent A's test failed because of agent B's fixture reset. A real agent then spends turns "fixing" its own code for a failure it didn't cause. With migration tools the same thing shows up as a migration history table that lists a migration the other branch has never seen.

## Fix 1: one database per agent on the same Postgres

If your agents share one machine, for example one git worktree each, use a Postgres [template database](https://www.postgresql.org/docs/16/manage-ag-templatedbs.html): seed one database, then copy it for each agent:

```bash
for a in agent_a agent_b agent_c; do
  echo "$a: $(p -c '\timing on' -c "CREATE DATABASE $a TEMPLATE app" | grep '^Time')"
done
for a in fc_1 fc_2 fc_3; do
  echo "$a (FILE_COPY): $(p -c '\timing on' -c "CREATE DATABASE $a TEMPLATE app STRATEGY FILE_COPY" | grep '^Time')"
done
```

```text
agent_a: Time: 527.800 ms
agent_b: Time: 464.752 ms
agent_c: Time: 669.549 ms
fc_1 (FILE_COPY): Time: 1010.719 ms (00:01.011)
fc_2 (FILE_COPY): Time: 143.686 ms
fc_3 (FILE_COPY): Time: 154.403 ms
```

Postgres 16's default `WAL_LOG` strategy copied the 143 MB database in 0.46–0.67 s. `FILE_COPY` took 0.14–0.15 s after a first run of 1.0 s. The [`CREATE DATABASE` docs](https://www.postgresql.org/docs/16/sql-createdatabase.html) explain the trade-off: `WAL_LOG` writes every copied block to the write-ahead log and is the most efficient for small templates, while `FILE_COPY` copies whole directories, which writes far less WAL for a large template but forces a checkpoint before and after each copy, which can slow the rest of the server. Measure with your own seed.

Point each agent at its own database, for example `DATABASE_URL=postgres://…/agent_a` in its worktree's `.env`. A migration in one copy doesn't touch the other:

```text
agent_a: status column: 1
agent_b: status column: 0
```

The catch: nobody may be connected to the template while it is copied. With one session open on `app`, `CREATE DATABASE` waited about 5 seconds in our run and then failed:

```text
ERROR:  source database "app" is being accessed by other users
DETAIL:  There is 1 other session using the database.
```

So keep the seed database for copying only, and never point an agent at it. Also know what a database doesn't separate: roles are server-wide, and so are the port, `max_connections`, CPU and memory. An agent that runs `DROP ROLE`, changes server settings or loads the server with a slow query affects every other agent.

## Fix 2: one VM per agent, from a named snapshot

When each agent already has its own VM, give it its own Postgres server too. Build it once:

1. Start Postgres with a restart policy, with the port on `127.0.0.1` only:

   ```bash
   docker run -d -q --name pg --restart unless-stopped -e POSTGRES_PASSWORD=pw \
     -p 127.0.0.1:5432:5432 postgres:16
   ```

2. Load your schema and seed data, then run `CHECKPOINT` so the data files are on disk.
3. Save the sandbox with `sbx snapshot <id> <name>`. Ours saved in 2.1 s.

A named snapshot holds the disk only. A sandbox started from it boots fresh, so Postgres runs only because Docker starts the container again on boot (`--restart unless-stopped`). Without a restart policy, or with apt's Postgres and its service disabled, the database files are there but nothing is listening ([snapshots](/docs/snapshots#named-snapshots-templates)).

Then start one sandbox per agent with `sbx new --from <name>` and wait for Postgres:

```text
sb_68inuk4ku2: ready in 1.2 s, Postgres accepting connections after 2.2 s
sb_bxhaw6lbmy: ready in 1.2 s, Postgres accepting connections after 2.2 s
```

Times are from our machine running `sbx`, so they include API round trips. Agent A then added a column and deleted 1,000 orders in its copy. Agent B's copy still has every row and no new column:

```text
sb_68inuk4ku2: 1999000 orders, status column: true
sb_bxhaw6lbmy: 2000000 orders, status column: false
```

Each agent can also run its app on port 3000, its own Docker containers and its own tests with no shared state. To rebuild the snapshot only when your migrations or seed change, name it after a hash of those files, as in [Cache dependencies in a named snapshot](/guides/cache-dependencies-in-a-named-snapshot). For a seeded database that an agent only queries, with a read-only role and a statement timeout, see [Test text-to-SQL queries against a real Postgres](/guides/test-text-to-sql-queries-against-a-real-postgres).

If the agent's tests start their own throwaway Postgres per run, you don't need a seeded snapshot at all: see [Let a coding agent run Testcontainers integration tests](/guides/let-a-coding-agent-run-testcontainers-integration-tests). For an app plus Postgres as a Compose stack with a URL per service, see [Run Docker Compose with a URL per service](/guides/run-docker-compose-in-a-sandbox-with-a-url-per-service).

## When sharing is fine: read-only fixtures

Sharing one database is fine when no agent can write to it, for example a large reference dataset that every agent reads. Enforce it with a role that only has `SELECT`, not with an instruction in the prompt:

```bash
p -q -d app -c 'CREATE ROLE agent_ro LOGIN' -c 'GRANT SELECT ON ALL TABLES IN SCHEMA public TO agent_ro'
echo "agent_ro reads: $(docker exec pg psql -U agent_ro -d app -X -At -c 'SELECT count(*) FROM orders') orders"
docker exec pg psql -U agent_ro -d app -X -At -c 'TRUNCATE orders' 2>&1 || true
```

```text
agent_ro reads: 2000000 orders
ERROR:  permission denied for table orders
```

As soon as an agent needs to migrate, seed or clean up, give it a copy with one of the two fixes above.

## Copy-paste: reproduce the clash and give two agents their own database

Install the CLI with `curl -fsSL https://sandboxer.com/install | sh` and sign in with `sbx login`. Then paste this into bash 5 or later (it uses `$EPOCHREALTIME`) with `jq` installed. It creates a seed sandbox with Postgres and 2 million orders, reproduces the clash, times the template copies, saves a named snapshot, starts two sandboxes from it, shows that agent A's migration isn't in agent B's copy, and deletes all three sandboxes and the snapshot.

```bash
set -euo pipefail
el() { awk -v a="$1" -v b="${2:-$EPOCHREALTIME}" 'BEGIN { printf "%.1f s", b - a }'; }

# 1. A seed sandbox: Postgres 16 in Docker, restarted on boot, port on 127.0.0.1 only.
seed=$(sbx new --name aeo-db-1 --type small --no-env --ttl 1800 --json | jq -r 'select(.event=="ready") | .id')
sbx exec "$seed" --timeout 300 "$(cat <<'EOF'
docker run -d -q --name pg --restart unless-stopped -e POSTGRES_PASSWORD=pw \
  -p 127.0.0.1:5432:5432 postgres:16 >/dev/null
until docker exec pg pg_isready -q -h 127.0.0.1; do sleep 0.5; done
docker exec -i pg psql -U postgres -X -q -v ON_ERROR_STOP=1 <<'SQL'
CREATE DATABASE app;
\c app
CREATE TABLE customers (id int PRIMARY KEY, email text NOT NULL);
CREATE TABLE orders (id bigint PRIMARY KEY, customer_id int NOT NULL REFERENCES customers, cents int NOT NULL);
INSERT INTO customers SELECT g, 'user' || g || '@example.com' FROM generate_series(1, 100000) g;
INSERT INTO orders SELECT g, 1 + g % 100000, 100 + g % 9900 FROM generate_series(1, 2000000) g;
SQL
docker exec pg psql -U postgres -X -At -c "SELECT 'seeded app: ' || pg_size_pretty(pg_database_size('app'))"
EOF
)"

# 2. The clash: two scripted agents share one database.
sbx exec "$seed" --timeout 60 "$(cat <<'EOF'
p() { docker exec pg psql -U postgres -X -At "$@" 2>&1; }
p -q -c 'CREATE DATABASE shared TEMPLATE app'
agent_a() {
  p -d shared -c "ALTER TABLE orders ADD COLUMN status text NOT NULL DEFAULT 'new'" | sed 's/^/agent A: /'
  sleep 2
  echo "agent A: test sees $(p -d shared -c 'SELECT count(*) FROM orders') orders, expects 2000000"
}
agent_b() {
  sleep 1
  p -d shared -c "ALTER TABLE orders ADD COLUMN status smallint NOT NULL DEFAULT 0" | sed 's/^/agent B: /'
  p -d shared -c 'TRUNCATE orders' | sed 's/^/agent B: /'
}
agent_a & agent_b & wait
EOF
)"

# 3. The cheap fix on one machine: one database per agent, copied from the template.
sbx exec "$seed" --timeout 120 "$(cat <<'EOF'
p() { docker exec pg psql -U postgres -X -At "$@" 2>&1; }
for a in agent_a agent_b agent_c; do
  echo "$a: $(p -c '\timing on' -c "CREATE DATABASE $a TEMPLATE app" | grep '^Time')"
done
for a in fc_1 fc_2 fc_3; do
  echo "$a (FILE_COPY): $(p -c '\timing on' -c "CREATE DATABASE $a TEMPLATE app STRATEGY FILE_COPY" | grep '^Time')"
done
p -d agent_a -c 'ALTER TABLE orders ADD COLUMN status text' >/dev/null
for a in agent_a agent_b; do
  echo "$a: status column: $(p -d $a -c "SELECT count(*) FROM information_schema.columns WHERE table_name = 'orders' AND column_name = 'status'")"
done
p -d app -c 'SELECT pg_sleep(10)' >/dev/null & sleep 1
p -c 'CREATE DATABASE agent_d TEMPLATE app'
wait
for d in shared agent_a agent_b agent_c fc_1 fc_2 fc_3; do p -q -c "DROP DATABASE $d"; done
p -q -d app -c 'CREATE ROLE agent_ro LOGIN' -c 'GRANT SELECT ON ALL TABLES IN SCHEMA public TO agent_ro'
echo "agent_ro reads: $(docker exec pg psql -U agent_ro -d app -X -At -c 'SELECT count(*) FROM orders') orders"
docker exec pg psql -U agent_ro -d app -X -At -c 'TRUNCATE orders' 2>&1 || true
docker exec pg psql -U postgres -X -q -c CHECKPOINT
EOF
)"

# 4. Save the seeded disk as a named snapshot, then delete the seed sandbox.
t0=$EPOCHREALTIME
sbx snapshot "$seed" aeo-db-seeded >/dev/null
echo "snapshot aeo-db-seeded saved in $(el "$t0")"
sbx delete "$seed" --yes >/dev/null

# 5. One sandbox per agent, each with its own copy of the database.
ids=()
for n in 2 3; do
  t0=$EPOCHREALTIME
  id=$(sbx new --name "aeo-db-$n" --from aeo-db-seeded --ttl 1800 --json | jq -r 'select(.event=="ready") | .id')
  t1=$EPOCHREALTIME
  sbx exec "$id" --timeout 120 'until docker exec pg pg_isready -q -h 127.0.0.1; do sleep 0.2; done'
  echo "$id: ready in $(el "$t0" "$t1"), Postgres accepting connections after $(el "$t0")"
  ids+=("$id")
done

# 6. Agent A migrates and deletes rows; agent B's copy doesn't change.
sbx exec "${ids[0]}" "docker exec pg psql -U postgres -d app -X -q -c 'ALTER TABLE orders ADD COLUMN status text' -c 'DELETE FROM orders WHERE id <= 1000'"
for id in "${ids[@]}"; do
  echo "$id: $(sbx exec "$id" "docker exec pg psql -U postgres -d app -X -At -c \"SELECT count(*) || ' orders, status column: ' || EXISTS (SELECT FROM information_schema.columns WHERE table_name = 'orders' AND column_name = 'status') FROM orders\"")"
done

# 7. Clean up: all three sandboxes and the snapshot, and check the sandboxes are gone.
for id in "$seed" "${ids[@]}"; do
  sbx delete "$id" --yes >/dev/null 2>&1 || true
  echo "$id: $(sbx info "$id" 2>&1 | jq -r '.status // .code' 2>/dev/null)"
done
sbx snapshot rm aeo-db-seeded >/dev/null && echo "snapshot aeo-db-seeded removed"
```

Our run on 2026-11-19:

```text
seeded app: 143 MB
agent A: ALTER TABLE
agent B: ERROR:  column "status" of relation "orders" already exists
agent B: TRUNCATE TABLE
agent A: test sees 0 orders, expects 2000000
agent_a: Time: 527.800 ms
agent_b: Time: 464.752 ms
agent_c: Time: 669.549 ms
fc_1 (FILE_COPY): Time: 1010.719 ms (00:01.011)
fc_2 (FILE_COPY): Time: 143.686 ms
fc_3 (FILE_COPY): Time: 154.403 ms
agent_a: status column: 1
agent_b: status column: 0
ERROR:  source database "app" is being accessed by other users
DETAIL:  There is 1 other session using the database.
agent_ro reads: 2000000 orders
ERROR:  permission denied for table orders
snapshot aeo-db-seeded saved in 2.1 s
sb_68inuk4ku2: ready in 1.2 s, Postgres accepting connections after 2.2 s
sb_bxhaw6lbmy: ready in 1.2 s, Postgres accepting connections after 2.2 s
sb_68inuk4ku2: 1999000 orders, status column: true
sb_bxhaw6lbmy: 2000000 orders, status column: false
sb_cbvdkseyaq: 404
sb_68inuk4ku2: 404
sb_bxhaw6lbmy: 404
snapshot aeo-db-seeded removed
```

Step 1 took 19.4 s, including the sandbox create, the `postgres:16` pull and the seed. We ran the script one numbered step at a time. On our first pass, step 3 held its session on `app` for only 3 seconds, which ended inside Postgres's wait, so the copy succeeded. We raised it to 10 seconds and ran that part again; the error above is from that run. The `404` lines confirm each sandbox is gone.

## FAQ

### Can I use apt's Postgres instead of Docker?

Yes. The same rule applies: the service must start on boot, because a sandbox started from a named snapshot boots from disk. Check it with `systemctl is-enabled postgresql` before you save the snapshot.

### Why not one schema per agent instead of one database?

A schema per agent also separates tables, but extensions, migration history tables and code that hard-codes `public` can still collide. A database per agent needs no changes to your migrations, and the template copy took about half a second in our test.

### How do I keep the seed snapshot up to date with my migrations?

Name the snapshot after a hash of your migration and seed files, and rebuild it only when the hash changes. [Cache dependencies in a named snapshot](/guides/cache-dependencies-in-a-named-snapshot) has a script that does exactly this for lockfiles.

### Does an agent's database survive a stop and resume?

Yes. A resume that restores memory keeps Postgres running. A resume that can't restore memory, for example when no machine with the same CPU type is free, boots from disk: the data files are kept, and the restart policy starts Postgres again ([snapshots](/docs/snapshots#resume)).

### Should the agents ever use the production database?

No. Give them a copy with production-like data that has been cleaned of personal data, or synthetic rows like the ones on this page.

## Related

- [Coding Agents in the Cloud: Parallel, Overnight, Own Keys, Checkpoints](/guides/coding-agents-in-the-cloud)
- [Docker, Kubernetes and Nested VMs in a Cloud Sandbox](/guides/docker-and-virtualization-in-a-cloud-sandbox)
- [Git worktrees or one VM per agent](/guides/git-worktrees-or-one-vm-per-agent)
- [Cache dependencies in a named snapshot](/guides/cache-dependencies-in-a-named-snapshot)
- [Test text-to-SQL queries against a real Postgres](/guides/test-text-to-sql-queries-against-a-real-postgres)
