Contents
postgres-and-pgadmin is three files: a Compose file, a readme and a ten-line init.sql. It starts PostgreSQL from the PostGIS image next to pgAdmin, with a set of extensions already enabled.
Most of the Compose file is ordinary. One line deserves a proper look: the one that mounts init.sql. It works exactly once, and afterwards it looks as if it has stopped working.
The mount that does the work
db:
image: postgis/postgis
environment:
POSTGRES_DB: postgres_db
POSTGRES_USER: admin
POSTGRES_PASSWORD: secret
PGDATA: /var/lib/postgresql/data
volumes:
- db-data:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql:roThere are two mounts. db-data is a named volume, and PGDATA points at the same path, so the database cluster lives on the volume and survives docker compose down. The second line bind-mounts one file from the host into /docker-entrypoint-initdb.d, a directory the Postgres image reads at startup. The :ro suffix makes the file read-only inside the container.
What happens on the first start
When the data directory is empty, the image creates a new cluster using POSTGRES_USER, POSTGRES_PASSWORD and POSTGRES_DB. Then it runs everything it finds in the init directory. SQL files run against the database named in POSTGRES_DB, which here is postgres_db. If a statement fails, initialization stops and the container exits, instead of coming up half configured.
What init.sql switches on
CREATE EXTENSION IF NOT EXISTS postgis;
CREATE EXTENSION IF NOT EXISTS postgis_topology;
CREATE EXTENSION IF NOT EXISTS fuzzystrmatch;
CREATE EXTENSION IF NOT EXISTS postgis_tiger_geocoder;
CREATE EXTENSION IF NOT EXISTS hstore;
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS unaccent;
CREATE EXTENSION IF NOT EXISTS citext;
CREATE EXTENSION IF NOT EXISTS btree_gist;
CREATE EXTENSION IF NOT EXISTS intarray;The first four are the spatial set. fuzzystrmatch sits before postgis_tiger_geocoder because the geocoder depends on it. The other six are general purpose: key-value columns (hstore), fuzzy text matching (pg_trgm), accent stripping (unaccent), case-insensitive text (citext), and two index helpers (btree_gist, intarray).
IF NOT EXISTS on every line means a repeat run is harmless.
Why editing the file later does nothing
The readme says the extensions are created only on first database initialization. After the first run, db-data holds a cluster, so the entrypoint leaves it alone. Change init.sql, run docker compose up -d again, and nothing happens.
There are two ways forward. Because every statement is idempotent, you can run the mounted file yourself:
docker compose exec db psql -U admin -d postgres_db -f /docker-entrypoint-initdb.d/init.sqlOr start clean with docker compose down -v. That deletes both db-data and pgadmin-data, so you lose your data and pgAdmin's own settings.
The script also runs against postgres_db only. If you create a second database later, check which extensions it has.
Before you use this anywhere real
- Credentials. The database uses
adminandsecret, and pgAdmin's login isadmin@admin.comwithsecret. All are plain text in a file that is meant to be committed. Move them to an.envfile or a secrets mechanism. - Ports.
5432:5432and80:80publish on every interface of the host. Prefix them with127.0.0.1:to keep both on your own machine. Port 80 will also clash with any web server already running locally. - Image tags.
postgis/postgisanddpage/pgadmin4have no tag, so each pull takes whateverlatestis at that moment. A Postgres data directory belongs to one major version, so iflatestever moves to a newer major, an existingdb-datavolume may refuse to start. Pin both.
When this fits
This works well for a throwaway local database where you want spatial and search extensions ready after one command, plus a UI to look around in.
It stops fitting when the schema matters. An init script runs once per volume, so tables and changes belong in a migration tool, with this script limited to extensions.
Found this useful? Share it with your team.