This directory holds one-shot SQL scripts that run the very first time the local Postgres container starts. They provision databases and users that are required by other services in the compose stack (SonarQube today, potentially others later) but live alongside the main application database on the same Postgres instance.
For local development, running three or four separate Postgres containers (one for the app, one for SonarQube, one for tests, etc.) would:
- waste memory (each Postgres reserves ~200 MB of shared buffers)
- complicate port management (each needs its own host port)
- make backups, cleanup (
docker compose down -v), and inspection harder
Instead, the stack runs one Postgres 17 container (db service in
docker-compose.yml) and every tool that needs a database gets its own
logical database and user on that shared instance. The scripts here are what
keep that setup one-shot:
When
docker compose up -d dbruns on a fresh volume, Postgres auto-executes every*.sqlfile in/docker-entrypoint-initdb.d/in alphabetical order. We mount this directory there, so the scripts run exactly once — right after the main database is created.
Production does not use this. On GKE, SonarCloud (SaaS) replaces the local SonarQube, and the application uses Cloud SQL via the Cloud SQL Auth Proxy. These init scripts are a local-dev convenience only.
The scripts do NOT run if the Postgres data volume already exists. This is by design — running them twice would fail with "database already exists". If you need to re-run them after the first start:
# Option A: destroy the volume and re-init everything
docker compose down -v
docker compose up -d db
# Option B: run the SQL manually on the existing instance
docker exec -i postgres-demo psql -U demo -d customer-service < infra/postgres/init-sonar.sql| File | Role | Runs when |
|---|---|---|
init-sonar.sql |
Creates the sonar database (UTF8-encoded) and the sonar role owning it, used by the optional local SonarQube container. Grants full privileges so SonarQube can manage its own schema on first boot. Safe to remove if you don't run SonarQube locally. |
First container start on fresh vol |
README.md |
This file. | (not executed) |
db:
volumes:
- ./infra/postgres/init-sonar.sql:/docker-entrypoint-initdb.d/init-sonar.sql:roRead-only mount — Postgres will never rewrite these files. If you add a new
init script, mount it similarly and keep alphabetical order in mind: files
run in the order ls -1 returns.
- Create
init-<thing>.sqlin this directory with idempotent-where-possible SQL (useCREATE USER IF NOT EXISTS/CREATE DATABASE IF NOT EXISTSwhen the tooling supports it). - Add a read-only mount to
docker-compose.ymlunder thedbservice. - Document its purpose in the table above.
- If you want it to also apply to existing volumes, run it manually via
psql(see Option B above).
- Filename prefix
init-<tool>.sqlkeeps the directory tidy as it grows. - Scripts should NOT assume a specific user is connected — explicitly set
ownership via
OWNER sonar/OWNER keycloak/ etc. - Avoid
DROPstatements. This directory is for provisioning; teardown happens viadocker compose down -v.