Shared Postgres with a database per lane
A TypeScript notes API built with Hono and backed by one Postgres container that every lane shares. Each lane gets its own database in it, migrated on every start and seeded through the running API.
Requirements
Section titled “Requirements”- devlanes, with
devlanes setuprun once - Node 22 or later
- Docker with Compose v2
Run it
Section titled “Run it”cd infra-postgres-shareddevlanes startOpen https://example-postgres-shared.localhost. The page says This is lane 0 of example-postgres-shared., names its database example_postgres_shared_main, lists the notes in it and has a form to add one.
During a start
Section titled “During a start”The first start of lane 0, with devlanes start --no-ui --output plain:
ok infraok setupok installok migrateok seedexample-postgres-shared@0 · mainok services 1 of 1 ready ok api https://example-postgres-shared.localhost- Infra: devlanes runs
docker-compose.ymlas the Compose projectexample-postgres-shared(infra.project_name, set from the project name) and waits for the healthcheck. Postgres is published on an ephemeral loopback port, read back as${{ infra.postgres.port }}. - Setup:
infra.setup.databasecreates${{ lane.db_name }}(example_postgres_shared_mainin lane 0,example_postgres_shared_lane1in lane 1) throughdocker compose exec, so the host needs nopsql. It runs once per lane; later starts printskipped setup: already done. lane.createtasks:installrunsnpm installon each start until it succeeds once for the lane.lane.starttasks:migraterunsnpm run db:migrate, which applies the SQL files insrc/migrations/that theschema_migrationstable does not list yet. The first start logsapplied 001_create_notes.sql, later ones0 applied, 1 already there.- Services:
apirunsnpm run dev(tsx watch src/index.ts) withDATABASE_URLfromglobal_envand becomes ready when/healthzanswers. after: servicestasks:seedrunsnpm run db:seed, which posts two notes to${{ services.api.url }}/api/notes, so the notes go through the running API. The plain output above listsok seedwith the other tasks, butdevlanes debug eventsshowstask example-postgres-shared@0 seed startedafterunit.changed example-postgres-shared@0:api ready.
Try next
Section titled “Try next”Start a second lane:
git worktree add ../examples-checkout -b feat/checkout, thencd ../examples-checkout/infra-postgres-shared && devlanes start. In the same folder,devlanes lane new --name example-postgres-shared-review --startgives lane 1 without a worktree. Lane 1 answers at https://example-postgres-shared1.localhost with its own seeded notes, and both databases sit in the one container:Terminal window docker compose -p example-postgres-shared exec postgres psql -U postgres -Atc "SELECT datname FROM pg_database WHERE datname LIKE 'example_postgres_shared_%' ORDER BY datname"example_postgres_shared_lane1example_postgres_shared_mainAdd a note with the form, then run
devlanes run reset-db --yes. It drops the lane’s database, creates it again, migrates and seeds, and the page is back to the two seeded notes.apilogspool: terminating connection due to administrator commandand recovers.Run
devlanes run reset-dbwithout--yesand without a terminal to ask on. It stops withtask reset-db asks: Drop example_postgres_shared_main and seed it again?; rerun with --yes.devlanes stopstopsapionly.devlanes infra statusstill shows postgres ashealthy, and the nextdevlanes startprintsskipped setup: already done.devlanes lane clean example-postgres-shared-review --yesprintscleaned example-postgres-shared@1: stopped 1 unit, teardown database. The query above then lists onlyexample_postgres_shared_main, and lane 0 keeps running. For the worktree’s lane, run it with1.With the lanes stopped,
devlanes infra downstops the container and keeps theexample-postgres-shared_pgdatavolume. The nextdevlanes startbrings it back with the notes still there.To remove everything:
devlanes lane clean 0 --yes, thendocker compose -p example-postgres-shared down -vfor the shared container and its volume, whichlane cleanleaves in place.
devlanes.yaml: the per-lanedb_name, the shared Compose infra with its setup and teardown,DATABASE_URL, the tasks and theapiservice.docker-compose.yml: Postgres 17 on an ephemeral loopback port, with a named volume and a healthcheck.src/app.ts: the routes, with the pages insrc/routes/pages.tsxand the JSON API insrc/routes/api.ts.src/db.tsandsrc/notes.ts: the connection pool and the notes queries.src/scripts/migrate.tsandsrc/migrations/: the migrations themigratetask applies.src/scripts/seed.ts: posts the first notes through the API.