---
title: "Postgres per lane"
url: https://devlanes.run/examples/infra-postgres-per-lane/
description: "Give each lane its own Postgres container while Redis stays shared, and delete a lane's container together with its data."
---

# Postgres per lane

A TypeScript server built with [Hono](https://hono.dev) that writes rows to its lane's own Postgres container and counts visits in a Redis that every lane shares. A branch that bumps the image tag in `docker-compose.yml` runs that version without touching the other lanes, and a destructive migration only breaks that branch's data; a shared Postgres, as in [infra-postgres-shared](/examples/infra-postgres-shared/), costs less but runs one version for everyone.

## Requirements

- devlanes, with `devlanes setup` run once
- Node 22 or later
- Docker with Compose 2.24.4 or newer, which `per_lane` needs for the `!override` and `!reset` tags in its override file. `devlanes doctor` prints both versions.

## Run it

```sh
cd infra-postgres-per-lane
devlanes start
```

`devlanes start --no-ui` prints the start as text:

```text
example-postgres-per-lane@0 (main): 1/1 ready
ready api http://127.0.0.1:20510 https://example-postgres-per-lane.localhost
```

Open https://example-postgres-per-lane.localhost. The page says `This is lane 0 of example-postgres-per-lane.` and shows the lane's Postgres container, port and rows, and the shared Redis with this lane's database index and visit count; "Add a row" inserts into Postgres and "Add a visit" increments the counter in Redis.

## During a start

1. Infra. Redis starts once in the shared Compose project `example-postgres-per-lane` (`infra.project_name`). Postgres starts in the lane's own project, `example-postgres-per-lane-lane<N>`, with an override file devlanes writes to `compose/` in the lane data folder. The override drops `container_name`, publishes `5432` on a port from the lane's block and adds `dev.devlanes.*` labels. The `pgdata` volume belongs to the lane's project, so each lane gets its own.
2. `infra.setup.schema` runs once per lane. It feeds `infra/schema.sql` to `psql` through `docker compose -p "$DEVLANES_COMPOSE_LANE_PROJECT" exec -T postgres ...`; plain `docker compose exec` would target the shared project, where Postgres does not run.
3. The `install` task runs `npm install` on the lane's first start.
4. The `api` service runs `npm run dev` (`tsx watch src/index.ts`) with `DATABASE_URL` pointing at `${{ infra.postgres.port }}`, which differs per lane, and `REDIS_URL` ending in `/${{ lane.redis_db }}`, the lane index. The ready check waits for `/healthz`.

```sh
docker ps --filter name=postgres-per-lane --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
```

```text
NAMES                                        STATUS                    PORTS
example-postgres-per-lane-lane0-postgres-1   Up 9 seconds (healthy)    127.0.0.1:20511->5432/tcp
example-postgres-per-lane-redis-1            Up 12 seconds (healthy)   127.0.0.1:52667->6379/tcp
```

`devlanes env show api` prints the lane data folder as `DEVLANES_LANE_DATA` (`~/.devlanes/data/example-postgres-per-lane/<lane id>`). The override is in it:

```sh
cat "$(devlanes env show api | grep ^DEVLANES_LANE_DATA= | cut -d= -f2)/compose/devlanes.compose-override.yaml"
```

```text
# Generated by devlanes for lane 0 of example-postgres-per-lane; rewritten on every start.
services:
  "postgres":
    container_name: !reset null
    ports: !override
      - "127.0.0.1:20511:5432/tcp"
    labels:
      "dev.devlanes.kind": "compose"
      "dev.devlanes.lane": "0"
      "dev.devlanes.project": "example-postgres-per-lane"
      "dev.devlanes.scope": "lane"
      "dev.devlanes.service": "postgres"
```

## Try next

- Start a second lane: `git worktree add ../examples-checkout -b feat/checkout`, then `cd ../examples-checkout/infra-postgres-per-lane && devlanes start`. The new worktree is lane 1. In this folder, `devlanes lane new --name example-postgres-per-lane-review --start --no-ui` gives the same lane 1 without a second worktree. Lane 1 gets its own Postgres container and port, while both lanes use the same Redis:

  ```text
  NAMES                                        STATUS                    PORTS
  example-postgres-per-lane-lane1-postgres-1   Up 5 seconds (healthy)    127.0.0.1:20551->5432/tcp
  example-postgres-per-lane-lane0-postgres-1   Up 30 seconds (healthy)   127.0.0.1:20511->5432/tcp
  example-postgres-per-lane-redis-1            Up 33 seconds (healthy)   127.0.0.1:52667->6379/tcp
  ```

  https://example-postgres-per-lane1.localhost says `This is lane 1 of example-postgres-per-lane.` and shows `example-postgres-per-lane-lane1-postgres-1` with no rows and Redis database 1, so its visit count starts at 0 whatever lane 0 counted.

- Freeze lane 1 with `devlanes debug freeze example-postgres-per-lane@1`. Its processes are suspended and its Postgres is paused; the shared Redis keeps running:

  ```text
  frozen example-postgres-per-lane@1
    a request, a connection or a command thaws it, or run devlanes debug thaw example-postgres-per-lane@1
  ```

  ```text
  NAMES                                        STATUS
  example-postgres-per-lane-lane1-postgres-1   Up 18 seconds (Paused)
  example-postgres-per-lane-lane0-postgres-1   Up 43 seconds (healthy)
  example-postgres-per-lane-redis-1            Up 46 seconds (healthy)
  ```

- Thaw lane 1 with a request: `curl -fsS https://example-postgres-per-lane1.localhost/healthz` (add `--ssl-revoke-best-effort` on Windows) prints `ok` and the container is no longer paused.
- Clean lane 1 with `devlanes lane clean 1 --yes`. Teardown runs `FLUSHDB` on Redis database 1, then the lane's Compose project goes with its container and volume. Redis and lane 0 stay:

  ```text
  cleaned example-postgres-per-lane@1: stopped 1 unit, teardown redis
  ```

  ```sh
  docker volume ls --filter name=postgres-per-lane
  ```

  ```text
  DRIVER    VOLUME NAME
  local     example-postgres-per-lane-lane0_pgdata
  ```

- Clean up everything: `devlanes lane clean 0 --yes` removes lane 0's Postgres and volume. The shared Redis outlives every lane, so remove it with `docker compose -p example-postgres-per-lane down -v`.

## Files

- [`devlanes.yaml`](https://github.com/devlanes-run/examples/blob/d2a99f076179db99581cef85453a58b788f3970b/infra-postgres-per-lane/devlanes.yaml): `infra.per_lane`, the Redis index in `lanes.vars`, the setup and teardown entries, the URLs in `global_env`, the `install` task and the `api` service.
- [`docker-compose.yml`](https://github.com/devlanes-run/examples/blob/d2a99f076179db99581cef85453a58b788f3970b/infra-postgres-per-lane/docker-compose.yml): Postgres and Redis with pinned tags, ephemeral loopback ports and healthchecks.
- [`infra/schema.sql`](https://github.com/devlanes-run/examples/blob/d2a99f076179db99581cef85453a58b788f3970b/infra-postgres-per-lane/infra/schema.sql): the `notes` table the setup entry creates.
- [`src/db.ts`](https://github.com/devlanes-run/examples/blob/d2a99f076179db99581cef85453a58b788f3970b/infra-postgres-per-lane/src/db.ts) and [`src/redis.ts`](https://github.com/devlanes-run/examples/blob/d2a99f076179db99581cef85453a58b788f3970b/infra-postgres-per-lane/src/redis.ts): the queries behind the page and the buttons.
- [`src/routes/`](https://github.com/devlanes-run/examples/blob/d2a99f076179db99581cef85453a58b788f3970b/infra-postgres-per-lane/src/routes): the page, `/api/state` and the form handlers.

## Docs

- [Infra: per-lane Compose services](/docs/guides/infra/#per-lane-compose-services)
- [Infra recipes: Postgres per lane, Redis index per lane](/docs/guides/infra/#recipes)
- [Lane data folder](/docs/guides/ports-and-env/#lane-data-folder)
- [Idle lanes](/docs/guides/lifecycle/#idle-lanes)
