Docker & Setup
Running PostgreSQL locally
Same as ClickHouse, everything here runs in Docker first. The image/container/volume model is identical across both modules — worth being precise about once.
From image to running server
A Docker image is a read-only template. A container is a running process created from that image, with its own writable layer on top. A volume is storage that lives outside the container's lifecycle, so data survives when the container is removed and recreated.
The image is pulled once and cached locally. Every
docker compose up after that reuses it — only the container is
created and destroyed. docker compose down removes the container
but leaves postgres_data untouched; only down -v deletes the
volume, and with it, every database in the cluster.
The compose file
This repo's postgres/compose.yaml is intentionally minimal: one
service, one port, one named volume. (The three-node production cluster
is compose_prod.yaml, covered in
The Production Cluster.)
services:
postgres:
image: postgres:18.6-trixie
ports:
- "5432:5432" # HOST : CONTAINER
environment:
POSTGRES_DB: learning
POSTGRES_USER: admin
POSTGRES_PASSWORD: admin123 # lab password
volumes:
- postgres_data:/var/lib/postgresql
volumes:
postgres_data:Postgres 18 images moved the data directory to
/var/lib/postgresql/18/docker (one subdirectory per major version, to
make pg_upgrade between versions easier), so the volume is mounted on
the parent, /var/lib/postgresql. The /var/lib/postgresql/data mount
that every Postgres 17 example uses doesn't work with 18.
Unlike ClickHouse's dual HTTP/native-protocol ports, Postgres exposes
a single port: 5432, the Postgres wire protocol. Every client —
psql, drivers, connection poolers — speaks it.
Credentials
The environment block sets the database name, user and password that
the image creates on first start, and only on first start.
The official Postgres image only runs its initialization when the data
directory is empty. Change the password in compose.yaml after the
volume already has data and nothing happens: the admin role and
learning database already exist from the first run. Wipe the volume
(docker compose down -v) to re-init, or change it with ALTER ROLE.
Everyday commands
# start (detached)
docker compose up -d
# stop the container, keep the volume
docker compose stop
# remove the container, keep the volume
docker compose down
# remove container AND delete all data
docker compose down -v
# open a SQL shell inside the container
docker compose exec postgres psql -U admin -d learning
# tail logs
docker compose logs -f postgresVerifying it works
Once the container is up, connect and run a few sanity commands. The
first two are plain SQL; the last two are psql meta-commands
(client-side shortcuts, not SQL — they start with a backslash and
aren't sent to the server as queries):
SELECT version();
SELECT now();
\l -- list databases
\dt -- list tables in the current schemaWith the server running and reachable, the next lesson, Architecture, covers what's actually happening on the other end of that connection.