DEV Community

Dmitry Romanoff
Dmitry Romanoff

Posted on • Edited on

How to install PostgreSQL database servers of different versions using docker-compose? A step-by-step guide

In this blog I'll demonstrate how to install PostgreSQL DB servers of version 12, 13, 14 and 15 on the same machine using docker compose. For illustration I will use Ubuntu 20.04

Docker compose orchestrates an environment to run multi-container Docker applications. It enables to bring multiple containers together to make an application. Docker compose makes use of a yaml file to define services that are later used to start and run the application with a single command.

Step #1 - Install docker-compose

Check the latest release of the docker compose by visiting https://github.com/docker/compose/releases/

sudo curl -L "https://github.com/docker/compose/releases/download/v2.14.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose              

sudo chmod +x /usr/local/bin/docker-compose

docker-compose --version
Enter fullscreen mode Exit fullscreen mode

For example:

Docker Compose version v2.14.2
Enter fullscreen mode Exit fullscreen mode

Step #2 - Prepare docker-compose.yaml file, describing the services that we want to run

cat docker-compose.yaml

version: '3.8'
services:
  db12:
    image: postgres:12
    restart: always
    command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c max_connections=200
    environment:
      - POSTGRES_USER=myuser
      - POSTGRES_PASSWORD=mypwd
    ports:
      - '5442:5432'
    volumes:
      - db12:/var/lib/postgresql/data
  db13:
    image: postgres:13
    restart: always
    command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c max_connections=200
    environment:
      - POSTGRES_USER=myuser
      - POSTGRES_PASSWORD=mypwd
    ports:
      - '5443:5432'
    volumes:
      - db13:/var/lib/postgresql/data
  db14:
    image: postgres:14
    restart: always
    command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c max_connections=200
    environment:
      - POSTGRES_USER=myuser
      - POSTGRES_PASSWORD=mypwd
    ports:
      - '5444:5432'
    volumes:
      - db14:/var/lib/postgresql/data
  db15:
    image: postgres:15
    restart: always
    command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all -c max_connections=200
    environment:
      - POSTGRES_USER=myuser
      - POSTGRES_PASSWORD=mypwd
    ports:
      - '5445:5432'
    volumes:
      - db15:/var/lib/postgresql/data      
volumes:
  db12:
    driver: local
  db13:
    driver: local
  db14:
    driver: local
  db15:
    driver: local 
Enter fullscreen mode Exit fullscreen mode

Step #3 - Build docker-compose

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

For example:

docker-compose up -d
[+] Running 4/4
 ⠿ Container create_postgres_db_servers_docker_compose-db15-1  Started                                                  1.3s
 ⠿ Container create_postgres_db_servers_docker_compose-db12-1  Started                                                  1.4s
 ⠿ Container create_postgres_db_servers_docker_compose-db13-1  Started                                                  0.7s
 ⠿ Container create_postgres_db_servers_docker_compose-db14-1  Started                                                  1.4s
dmi@dmi-VirtualBox:~/create_postgres_db_servers_docker_compose$

Enter fullscreen mode Exit fullscreen mode

Step #4 - Check connection to the created PostgreSQL DB servers.

Connection to the PostgreSQL DB server, version 12.13:

psql -h localhost -p 5442 -U myuser -d postgres -W
Password:
psql (15.1 (Ubuntu 15.1-1.pgdg20.04+1), server 12.13 (Debian 12.13-1.pgdg110+1))
Type "help" for help.

postgres=# select version();
                                                            version
-------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 12.13 (Debian 12.13-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
(1 row)

postgres=#
Enter fullscreen mode Exit fullscreen mode

Connection to the PostgreSQL DB server, version 15.1:

psql -h localhost -p 5445 -U myuser -d postgres -W
Password:
psql (15.1 (Ubuntu 15.1-1.pgdg20.04+1))
Type "help" for help.

postgres=# select version();
                                                           version
-----------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 15.1 (Debian 15.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
(1 row)

postgres=#
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this blog I've demonstrated how to install PostgreSQL DB servers of version 12, 13, 14 and 15 on the same machine using docker compose.

Top comments (0)