Skip to content
Runbook

Install PostgreSQL on Docker

Open source SQL relational database, robust and standards-compliant.

Databasesqlrdbmsrelationalaciddatabase

Installation

Container (official image) · Docker
docker run -d --name postgres -e POSTGRES_PASSWORD=secret -p 5432:5432 -v pgdata:/var/lib/postgresql/data postgres:17

Replace 'secret' with a strong password. The named volume pgdata persists the data.

Verify the installation

Verify
docker exec postgres psql -U postgres -c 'SELECT version();'

Important files

TypePathDescription
data/var/lib/postgresql/dataData directory INSIDE the container, to be mounted on a volume for persistence.

Default ports

5432

Command-line tools

  • psqlInteractive SQL client, accessible via docker exec.
  • pg_dumpLogical backup, via docker exec.
  • pg_restoreRestore from a custom-format backup, via docker exec.

Uninstall

Remove the container
docker rm -f postgres

Does NOT delete the pgdata volume.

Also remove the data
docker volume rm pgdata

Irreversible: destroys all cluster data.

Good to know

  • POSTGRES_PASSWORD is MANDATORY on first startup, otherwise the container refuses to initialize.
  • Without a volume, the data is lost when the container is removed: always use a named volume or a bind mount in production.
  • Useful variables: POSTGRES_USER (superuser, default postgres) and POSTGRES_DB (database created at startup).

Starting PostgreSQL with Docker

The official postgres image (Docker Official Image) provides a ready-to-use server. The command above launches a detached container, publishes port 5432, and stores the data in a named volume pgdata.

The POSTGRES_PASSWORD variable is mandatory on the very first startup: it sets the password for the postgres superuser. Without it, cluster initialization fails (unless you explicitly allow insecure authentication, which is strongly discouraged).

Data persistence

The internal data directory is /var/lib/postgresql/data. As long as it is not mounted on a volume or a bind mount, the data disappears when the container is removed. The named volume pgdata in the example ensures that your databases survive container recreations.

Customization and connection

On first launch, you can set POSTGRES_USER (superuser name, postgres by default) and POSTGRES_DB (database created automatically). To open a SQL shell: docker exec -it postgres psql -U postgres. From the host, connect to localhost:5432 with your usual client.