Using the postgres docker image, is the quickest way to get started with any of your postgres development environments, or if you need to test any features of the postgres database. Official postgres database images are available for download and use from docker hub. You can get images of postgres 11,12,13,14 and 15 from docker hub. In this blog post we will see how to get started with downloading and running this image on Ubuntu Linux.
The steps outlined below assume that you have one of the newer versions of ubuntu installed on a host and that you are using the linux user named “ubuntu” to run docker.
As root execute the following commands on the ubuntu host to download docker. These steps also setup the ubuntu user to run docker containers.
mkdir -m 0755 -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update -y
apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
systemctl disable --now docker.service docker.socket
su - ubuntu -c "dockerd-rootless-setuptool.sh install"
Now you are ready to download and run the postgres container.
Login as the “ubuntu” user and Create a text file named pg.yaml file, that has the following instructions. The following instructions assume that you have a directory /u01/pg/data which you want to use to store your postgres datafiles.
services:
db:
image: postgres:latest
restart: always
environment:
POSTGRES_PASSWORD: MyPassword
ports:
- "5432:5432"
volumes:
- /u01/pg/data:/var/lib/postgresql/data
Now we can run docker to download and run this image using the following command
docker compose -f /home/ubuntu/tmp/pg.yaml up -d db
This command downloads and installs the latest postgres image (Which at the time of writing is 15.2) You can now connect to the postgres database from the ubuntu host as shown below.
psql -h localhost -p 5432 -d postgres -U postgres
You can login as root to your postgres container using the command below
docker exec -it cc18a27f8850 bash
where cc18a27f8850 is the container id (You can get your container id by running the command, docker container ls -a.
Once you are logged in as root to the postgres container, you can su – postgres and then run commands to stop and start postgres. pg_ctl can be found in /usr/lib/postgresql/15/bin.