PostgreSQL is the backbone of transactional data storage in many systems. As platform engineers, we often work with different PostgreSQL versions to test new features, ensure backward compatibility, conduct benchmarking and load tests, manage major/minor version upgrades, and enhance monitoring capabilities. For development purposes, our preferred method is to use official Docker images, which allow us to quickly set up and run PostgreSQL in containers.
With Docker, you can launch any version of PostgreSQL within minutes. Below is a command to launch PostgreSQL 16 on a Debian-based OS. The PostgreSQL community also supports other operating systems.
docker run -d --name pg16 \
--env POSTGRES_PASSWORD=postgres \
-p 5432:5432 \
postgres:16
To connect to the PostgreSQL database locally, use the following command:
psql -h localhost -p 5432 -U postgres -d postgres
To exec into the container and connect to the database, use the following commands:
docker exec -it pg16 bash
psql -U postgres
These Docker images are immutable, so there are specific ways to modify the postgresql.conf file. For parameters that require a restart to take effect, you can use the following approach. This command assumes the default configuration path for the Debian PostgreSQL image:
# to copy the config file to local file system
docker cp pg16:/var/lib/postgresql/data/postgresql.conf /tmp/
# modify and push the config file back to docker image
docker cp /tmp/postgresql.conf pg16:/var/lib/postgresql/data/
To apply the changes, restart the container with the following command:
docker restart pg16
To inspect the container configurations, use:
docker inspect pg16
To read PostgreSQL error logs, use:
docker logs pg16
For more details refer the official site. Docker Hub PostgreSQL