Skip to content

Deploy PostgreSQL With Docker

homepage-banner

PostgreSQL is a stable, powerful, and flexible, free and open-source object-relational database management system favored by developers. Docker is a tool that uses containers to make it easier to create, deploy, and run applications.

Deploying PostgreSQL in the Docker environment saves time, boosts performance, and reduces the size of the application. In this post, we will learn how to deploy a PostgreSQL database in Docker in two ways:

  • Deploying a PostgreSQL with Docker Compose
  • Deploying a PostgreSQL with Docker

Requirements

  • A server running Ubuntu 20.04.
  • A root password set up on your server.

Install Docker and Docker Compose

Before starting, install Docker and Docker Compose to your system. First, install all required dependencies with the following command:

apt-get install apt-transport-https ca-certificates curl software-properties-common -y

Next, add the GPG key with the following command:

curl -fsSL <https://download.docker.com/linux/ubuntu/gpg> | apt-key add -

Next, add the Docker repository to the APT with the following command:

add-apt-repository "deb [arch=amd64] <https://download.docker.com/linux/ubuntu> focal stable"

Next, install Docker and Docker Compose using the following command:

apt-get install docker-ce docker-compose -y

Once Docker is installed, verify the Docker version with the following command:

docker --version

You should see the following output:

Docker version 20.10.6, build 370c289

Deploy a PostgreSQL with Docker Compose

In this section, we will create a docker-compose.yml and launch the PostgreSQL container using Docker Compose.

First, create a docker-compose.yml inside the /opt directory with the following command:

nano /opt/docker-compose.yml

Add the following lines:

version: '3.3'
services:
  postgres:
    image: 'postgres:latest'
    restart: always
    environment:
      POSTGRES_PASSWORD: password
    ports:
      - "5432:5432"

Save and close the file when you are finished.

The above file will download the PostgreSQL latest image from the Docker Hub, start the container and expose the port 5432.

Now, change the directory to /opt and launch the PostgreSQL container with the following command:

cd /opt
docker-compose up -d

Output:

Pulling postgres (postgres:latest)...
latest: Pulling from library/postgres
f7ec5a41d630: Pull complete
d073cd070242: Pull complete
03790957a916: Pull complete
b3776ac15dab: Pull complete
7144fd00aec4: Pull complete
54f6491bd120: Pull complete
247ab23c6036: Pull complete
57800498c536: Pull complete
bcb15a4d14f4: Pull complete
cfc751ecbc6e: Pull complete
bbf042afd4a4: Pull complete
453056a20de6: Pull complete
d5b1a75378ef: Pull complete
7841e2074775: Pull complete
Digest: sha256:61d5d8ef6cb4e2035f053f26b6b455c201a809354084cc8426b6904b8dd35602
Status: Downloaded newer image for postgres:latest
Creating opt_postgres_1 ... done

You can now verify the running container with the following command:

docker ps

Output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f8a1ae552cff postgres:latest "docker-entrypoint.s…" 6 seconds ago Up 4 seconds 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp opt_postgres_1

You can also check the PostgreSQL container log with the following command:

docker-compose logs -f

Output:

postgres_1 | 2021-04-29 11:57:41.004 UTC [1] LOG: starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres_1 | 2021-04-29 11:57:41.005 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2021-04-29 11:57:41.005 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2021-04-29 11:57:41.008 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2021-04-29 11:57:41.015 UTC [65] LOG: database system was shut down at 2021-04-29 11:57:40 UTC
postgres_1 | 2021-04-29 11:57:41.021 UTC [1] LOG: database system is ready to accept connections

Deploy a PostgreSQL with Docker

You can also deploy a PostgreSQL container without writing a docker-compose.yml file. You just need to search the latest PostgreSQL image, download it and launch the PostgreSQL container.

First, search the PostgreSQL image in the Docker Registry with the following command:

docker search postgres

You should see the list of all Postgres images in the following output:

NAME DESCRIPTION STARS OFFICIAL AUTOMATED postgres The PostgreSQL object-relational database sy… 9252 [OK] sameersbn/postgresql 157 [OK] bitnami/postgresql Bitnami PostgreSQL Docker Image 93 [OK] paintedfox/postgresql A docker image for running Postgresql. 77 [OK] centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 45 postgrest/postgrest REST API for any Postgres database 44 arm32v7/postgres The PostgreSQL object-relational database sy… 29 wrouesnel/postgres_exporter Postgres metrics exporter for Prometheus. 27 circleci/postgres The PostgreSQL object-relational database sy… 23 schickling/postgres-backup-s3 Backup PostgresSQL to S3 (supports periodic … 19 [OK] prodrigestivill/postgres-backup-local Backup PostgresSQL to local filesystem with … 19 [OK] centos/postgresql-10-centos7 PostgreSQL is an advanced Object-Relational … 19 debezium/postgres PostgreSQL for use with Debezium change data… 17 [OK] centos/postgresql-94-centos7 PostgreSQL is an advanced Object-Relational … 16 postdock/postgres PostgreSQL server image, can work in master … 14 [OK] clkao/postgres-plv8 Docker image for running PLV8 1.4 on Postgre… 13 [OK] camptocamp/postgres Docker image for PostgreSQL including some e… 8 [OK] centos/postgresql-95-centos7 PostgreSQL is an advanced Object-Relational … 6 jgiannuzzi/postgres-bdr Docker image for PostgreSQL with BDR support 5 [OK] dcm4che/postgres-dcm4chee PostgreSQL for dcm4che-arc 5.x 5 [OK] blacklabelops/postgres Postgres Image for Atlassian Applications 4 [OK] ansibleplaybookbundle/postgresql-apb An APB which deploys RHSCL PostgreSQL 2 [OK] fredboat/postgres PostgreSQL 10.0 used in FredBoat's docker-co… 1 manageiq/postgresql Container with PostgreSQL and built on CentO… 0 [OK] cfcommunity/postgresql <https://github.com/cloudfoundry-community/po…> 0

Now, pull the latest version of the PostgreSQL image with the following command:

docker pull postgres:latest

You should see the following output:

latest: Pulling from library/postgres
f7ec5a41d630: Pull complete
d073cd070242: Pull complete
03790957a916: Pull complete
b3776ac15dab: Pull complete
7144fd00aec4: Pull complete
54f6491bd120: Pull complete
247ab23c6036: Pull complete
57800498c536: Pull complete
bcb15a4d14f4: Pull complete
cfc751ecbc6e: Pull complete
bbf042afd4a4: Pull complete
453056a20de6: Pull complete
d5b1a75378ef: Pull complete
7841e2074775: Pull complete
Digest: sha256:61d5d8ef6cb4e2035f053f26b6b455c201a809354084cc8426b6904b8dd35602
Status: Downloaded newer image for postgres:latest
docker.io/library/postgres:latest

Now, verify the downloaded image with the following command:

docker images

You should see the following output:

REPOSITORY TAG IMAGE ID CREATED SIZE postgres latest 26c8bcd8b719 2 weeks ago 314MB

Now, launch the PostgreSQL container and define the Postgres password with the following command:

docker run --name postgres-container -e POSTGRES_PASSWORD=password -d postgres

Now, verify the Postgres container with the following command:

docker ps

You should see the following output:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b68599dd6840 postgres "docker-entrypoint.s…" 9 seconds ago Up 8 seconds 5432/tcp postgres-container

If you want to connect to the running Postgres container, run the following command:

docker exec -it postgres-container psql -U postgres

You will be redirected to the PostgreSQL container shell:

psql (13.2 (Debian 13.2-1.pgdg100+1))
Type "help" for help.

postgres=#

You can now create a database, table, and add data into the table.

You can exit from the Postgres container with the following command:

postgres=# exit

Conclusion

In the above guide, you learned how to deploy PostgreSQL in Docker in two different ways. Deploying a PostgreSQL in the containerized environment is a time saver and cost-efficient in terms of infrastructure.

Leave a message