Skip to content
Latchkey

How to Provide a Postgres Fixture With Compose in CI

A throwaway Postgres in Compose gives tests a real database with a known schema, seeded once and torn down after the job.

Define a postgres service with a pg_isready healthcheck and mount an init SQL script so the schema is ready the moment the service reports healthy.

Steps

  • Set POSTGRES_PASSWORD and a healthcheck using pg_isready.
  • Mount *.sql into /docker-entrypoint-initdb.d to seed on first boot.
  • Gate tests on --wait or depends_on: service_healthy.

Compose file

docker-compose.yml
services:
  postgres:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: test
      POSTGRES_DB: app
    volumes:
      - ./ci/seed.sql:/docker-entrypoint-initdb.d/seed.sql:ro
    ports: ["5432:5432"]
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres -d app"]
      interval: 5s
      retries: 10

Gotchas

  • Init scripts run only when the data directory is empty, so a reused named volume skips them; use a fresh volume or down -v between runs.
  • Match the client library major version to the server to avoid protocol surprises.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →