Build the mental model
A Docker image gives Windows, macOS, and Linux learners a consistent practice environment. A named volume keeps data when the container is replaced, and passwords must not be committed into project source. Production requires more than this local setup: backups, encryption, high availability, and patching all matter.
Connect it to a real scenario
Run the `postgres:18` image and create `tutorial_platform` on first startup. The `pgdata` volume stores database files outside the container lifecycle. Use `pg_isready` to verify that the server is accepting connections.
Try the working example
docker volume create pgdata
docker run --name pg-tutorial \
-e POSTGRES_PASSWORD=learnpg \
-e POSTGRES_DB=tutorial_platform \
-p 127.0.0.1:5432:5432 \
-v pgdata:/var/lib/postgresql/data \
-d postgres:18
docker exec pg-tutorial pg_isready -U postgresPostgreSQL reports `accepting connections` and is ready to use.5-minute try-it
Stop and start the container, then verify that the named volume preserves your database data.
One important caution
Never reuse the sample password in production. Also protect shell history, avoid committing `.env`, and do not expose the port publicly.
PostgreSQL — Getting Started — PostgreSQL Global Development Group