Skip to the workspace
Pebellslearn by building
Installation, Setup and Configuring  ›  Lesson

Running PostgreSQL in Docker

Using Docker 12 minRun PostgreSQL in Docker with a pinned major-version tag, a required password and a named volume so the data survives the container.

postgresql dba / Running PostgreSQL in Docker

Explore each scene at your own pace.

01 · The rule

The rule

Running PostgreSQL in Docker

A new developer joins Pedal and needs a local database that matches production. Installing packages on every laptop drifts over time. One Docker command gives everyone the same version — as long as you know the one mistake that silently deletes the data.

The rule

Pin the tag, set a password, put the data in a volume.

docker run -d --name pedal-db \
  -e POSTGRES_PASSWORD=change-me \
  -e POSTGRES_DB=pedal \
  -p 5432:5432 \
  -v pedal-data:/var/lib/postgresql/data \
  postgres:17
  • Pin the tag. postgres:17 stays on major version 17 and picks up minor fixes when you pull. postgres:latest would one day jump to a new major version, and a data directory from the old one will refuse to start.
  • Set a password. The official image will not start without POSTGRES_PASSWORD (unless you deliberately choose an insecure trust mode). POSTGRES_USER and POSTGRES_DB are optional and default to postgres.
  • Put the data in a volume. A container's own filesystem disappears with the container. -v pedal-data:/var/lib/postgresql/data stores the cluster in a named volume that survives docker rm. (Images for version 18 and later expect the volume at /var/lib/postgresql instead; check the image notes when you change major version.)

-p 5432:5432 publishes the port so tools on your machine can connect to localhost:5432.

Two more behaviours of the official image:

  • Files in /docker-entrypoint-initdb.d/ (.sql or .sh) run only when the data directory is empty, the first time. Mount your schema there to create it automatically.
  • docker exec -it pedal-db psql -U postgres -d pedal opens psql inside the container.

In production, many teams skip running PostgreSQL themselves and use a managed cloud service (Amazon RDS, Google Cloud SQL, Azure Database for PostgreSQL). The provider handles installation, patches and backups; you still own roles, settings and queries, and you trade some control, such as superuser access, for that convenience.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.