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.