Skip to content
Aditya Bhalsod
← DevOps
DevOpsBeginner

How the Postgres Image Runs Your init.sql Only on First Start

A beginner look at postgres-and-pgadmin, covering the init directory mount, the ten extensions in init.sql and why editing that file later changes nothing.

  • #docker
  • #docker-compose
  • #postgres
  • #postgis
Contents
  1. The mount that does the work
  2. What happens on the first start
  3. What init.sql switches on
  4. Why editing the file later does nothing
  5. Before you use this anywhere real
  6. When this fits

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

YAML
  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:ro

There 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

SQL
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:

Bash
docker compose exec db psql -U admin -d postgres_db -f /docker-entrypoint-initdb.d/init.sql

Or 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 admin and secret, and pgAdmin's login is admin@admin.com with secret. All are plain text in a file that is meant to be committed. Move them to an .env file or a secrets mechanism.
  • Ports. 5432:5432 and 80:80 publish on every interface of the host. Prefix them with 127.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/postgis and dpage/pgadmin4 have no tag, so each pull takes whatever latest is at that moment. A Postgres data directory belongs to one major version, so if latest ever moves to a newer major, an existing db-data volume 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.

Keep reading

More from DevOps and beyond