How To Set Up and Run a PostgreSQL Database Using Docker

PostgreSQL is an open-source relational database management system. PostgreSQL is officially supported on all major operating systems such as Windows, Ubuntu, MacOs, CentOS etc. PostgreSQL can also be run as a containerized application on Docker. Docker is a powerful containerization tool that allows you to package and run applications and their dependencies in an isolated environment. 

Why use PostgreSQL on Docker Containers?

Using PostgreSQL on Docker containers can bring us a lot of benefits. To start with, docker containers provide us with isolation which is quite useful when we want our PostgreSQL database to be isolated from other applications or even other versions of PostgreSQL running on the same machine. Suppose we have our application and PostgreSQL database running on different containers and our application fails, we can spin up a new container for our application and our data would be safe.

Using PostgreSQL on Docker containers makes portability very simple, we can move our PostgreSQL database from one system to another easily as Docker containers can run on any system that supports Docker. Docker containers also help us achieve better resource utilization and scalability as we can spin up multiple PostgreSQL containers with very less resource utilization in comparison to VMs.

Getting started with PostgreSQL on Docker: Installation

To set up PostgreSQL on Docker containers, you first need to have Docker Desktop installed on your system. You can download docker desktop from this link.

Pull the PostgreSQL Image

Docker allows you to run applications in isolated containers, and these containers are built from images. PostgreSQL’s community maintains Debian and Alpine based PostgreSQL images. If we simply pull the Postgres image we will get a Debian based image which would run on the bookworm version of Debian. We can however specify the bullseye version of Debian as well.

Alpine based PostgreSQL images are smaller in size and offer only required packages, due to which they contain lesser functionalities. We’ll discuss more about Alpine and Debian variants of PostgreSQL images in our upcoming blog, for this blog we will be using Postgres image which is built on the bookworm version of Debian. 

To set up PostgreSQL on Docker you must pull the official PostgreSQL image from Docker Hub. 

docker pull postgres
Using default tag: latest
latest: Pulling from library/postgres
1f7ce2fa46ab: Pull complete
a797f18545f0: Pull complete
969823a73455: Pull complete
3cf63429c214: Pull complete
0829252d0d9a: Pull complete
b66a9305e34c: Pull complete
799c670608dc: Pull complete
870cc573d452: Pull complete
fb2a58049417: Pull complete
79e525202743: Pull complete
a3104899b78b: Pull complete
43ef4fb89a17: Pull complete
bbf10b4ac81d: Pull complete
Digest: sha256:71da05df8c4f1e1bac9b92ebfba2a0eeb183f6ac6a972fd5e55e8146e29efe9c
Status: Downloaded newer image for postgres:latest
docker.io/library/postgres:latest

This command downloads the latest PostgreSQL image to your local machine.

You can pull a PostgreSQL image with a different version by specifying the version like in the following command.

docker pull postgres:15

Run the PostgreSQL Container

Now that you have pulled the PostgreSQL image, run the container using the following command. You may replace POSTGRES_USER and POSTGRES_PASSWORD with your own desires username and password.

docker run --name postgresql -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres
8d49126184fb5ace174b447816e71fab57711eada4bec8368f729df5818bfef7

In the above mentioned commands, this is what each flag does.

  • –name postgresql: Sets the name of the container.
  • -e POSTGRES_USER=postgres: Sets the user for PostgreSQL.
  • -e POSTGRES_PASSWORD=postgres: Sets the password for the PostgreSQL user.
  • -p 5432:5432: Maps port 5432 on your machine to port 5432 in the container.
  • -d postgres: Runs the container in the background.

You can see if your container is running, by using the following command

docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d49126184fb postgres “docker-entrypoint.s…” About a minute ago Up About a minute 0.0.0.0:5432->5432/tcp postgresql

You may stop or start your PostgreSQL container as per your needs using docker start and docker stop commands

Stop Docker Container:

docker stop postgresql

postgresql

Start Docker container:

docker start postgresql

postgresql

Connect to PostgreSQL

You can connect to PostgreSQL using PSQL by getting into the container and then connecting with PSQL. You can get inside the container by running the following command:

docker exec -it postgresql /bin/bash

You can connect to PostgreSQL database by running the following PSQL command inside the container.

root@8d49126184fb:/# psql -h localhost -U postgres
psql (16.1 (Debian 16.1-1.pgdg120+1))
Type "help" for help.

postgres=#

Conclusion

We saw how easily we can use Docker to set up PostgreSQL in isolated containers and connect to our database. This provides us with an efficient and reliable way to run PostgreSQL without dependencies on the host system.

Leave A Comment