EXAMPLE 02
  • services
  • databases

One shared Postgres

One development database for all your sandboxes and their instances.


The problem

Your API and your web app both need the development database, and so does every extra instance you spin up for an agent. Giving each its own Postgres means as many containers, migrations and seeds as instances, and data that differs between them.

A service with scope = "global" is created once per config root and shared by every sandbox that lists it.

Let your agent set this up

Config

devsandboxes.toml
[services.postgres]
image = "postgres:16"
scope = "global"                       # one container for the whole config root
env = { POSTGRES_PASSWORD = "postgres" }
ports = ["127.0.0.1:5432:5432"]        # optional: reach it from the host too

[template.base]
image = "node:22"
services = ["postgres"]
# The hostname is the service's name.
containerEnv = { DATABASE_URL = "postgres://postgres:postgres@postgres:5432/postgres" }

[sandbox.api]
extends = "base"
folder = "../api"

[sandbox.web]
extends = "base"
folder = "../web"

Try it

TERMINAL
devsandbox run api                     # starts postgres, then api
devsandbox run web                     # joins the running postgres
devsandbox run api --name fix-login    # its own worktree, same database
devsandbox service ls                  # one global postgres
  • --name: the instance name.

Check an instance resolves the service, and connect from the host:

TERMINAL
devsandbox exec fix-login getent hosts postgres
psql postgres://postgres:postgres@127.0.0.1:5432/postgres

How it works

  • Every instance that lists the service joins its network and reaches it by name (services).
  • stop and rm leave it running for the others; devsandbox gc removes it once no running instance uses it.
  • devsandbox service rebuild postgres applies config changes and rewires running sandboxes in place (drift).
  • The data lives in the service container: service rebuild and gc start it empty, so keep a way to re-seed.

Agent instructions

Prompt for your coding agentPaste into Claude Code, Codex or any agent with a shell.

Give my devsandbox sandboxes one shared Postgres database that every sandbox and every instance connects to.

  1. Read the devsandbox-cli and config-toml-spec skills first (load them if they are installed, otherwise read them from https://github.com/Nemikolh/devsandboxes/tree/main/skills). Follow them over your own assumptions: unknown config keys are hard errors.
  2. Find my devsandbox config dir: the folder holding devsandboxes.toml, passed to devsandbox as -C <config dir>. If you cannot tell which one, ask me.
  3. Add [services.postgres] with image = "postgres:<postgres version>" (the version the repo targets), scope = "global" and env = { POSTGRES_PASSWORD = "<password>" }. Add ports = ["127.0.0.1:5432:5432"] only if I want to reach it from the host and that port is free.
  4. Add "postgres" to services of a template every sandbox extends, with containerEnv pointing the app at host postgres under the variable name the repo already reads, e.g. DATABASE_URL = "postgres://postgres:<password>@postgres:5432/postgres".
  5. Validate with devsandbox -C <config dir> ls and fix every error it reports before going on.
  6. Start the sandboxes with devsandbox -C <config dir> run <sandbox> and check devsandbox -C <config dir> service ls shows a single global postgres.
  7. Check devsandbox -C <config dir> exec <instance> getent hosts postgres resolves, then run the repo's migrations in that instance.
  8. Ask me before anything destructive: devsandbox rm, gc, rebuild --force, service rebuild, deleting files or branches.
fill in before sending Agent skills (opens in a new tab)

Source: site/src/content/examples/shared-postgres.mdxEdit this page on GitHub(opens in a new tab)

MENU /
View on GitHub
EXAMPLES
All examplesEvery recipe on one page.Parallel coding agentsSeveral coding agents on one repo, fully isolated from one another.One shared PostgresOne development database for all your sandboxes and their instances.Private services per instanceEvery instance gets its own Postgres and Redis, so agents never touch each other's data.Shared package cachesNew sandboxes stop downloading the same packages again.ssh-agent and git pushAgents push over ssh with your host's keys, which never enter the container.Port forwarding on demandOpen a sandbox's dev server or database from your host, without restarting anything.Attach VS CodeOpen any instance in VS Code with one key.devcontainer FeaturesAdd Node, Rust or the GitHub CLI to any image, in TOML.Babysit pull requestsAn agent keeps your open PRs merged with their base, and pings you when it can't.Port a devcontainer.jsonTurn an existing devcontainer.json into a devsandbox config.