A simple Docker setup for running a PostgreSQL database.
-
Update the files in the
secrets-exampledirectory with your desired credentials. -
Rename the
secrets-exampledirectory tosecrets. -
Create the Docker network:
docker network create postgres-network- Start the PostgreSQL container:
docker compose up -dIf the port is already in use, change the
POSTGRES_HOST_PORTvariable in the.envfile.
- To stop the container:
docker compose downThe database is reachable from:
localhost:<POSTGRES_HOST_PORT>on the local machinepostgres:5432from other containers in the Docker networkpostgres-network
The pgdata volume persists the database, so data is not lost when stopped.
To stop and delete the volume (this will erase ALL database data):
docker compose down -vThe current port binding restricts access to the local machine.
To allow other machines on the network to connect, change it to:
ports:
- "${POSTGRES_HOST_PORT}:5432"