如何使用Docker-Compose安装不同版本的PostgreSQL数据库服务器?分步指南
#postgres #database #ubuntu #docker

在此博客中,我将演示如何使用Docker Compose在同一台计算机上安装第12、13、14和15版的PostgreSQL DB服务器。为了插图,我将使用Ubuntu 20.04

docker组成的编排环境运行多个登山码的应用程序。它使得可以将多个容器聚集在一起以制定应用程序。 Docker组合使用YAML文件来定义以后用一个命令启动和运行应用程序的服务。

Step #1-安装docker -compose

通过访问https://github.com/docker/compose/releases/
检查Docker撰写的最新版本

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

例如:

Docker Compose version v2.14.2

Step #2-准备docker -compose.yaml文件,描述我们要运行的服务

cat docker-compose.yaml

version: '3.8'
services:
  db12:
    image: postgres:12
    restart: always
    environment:
      - POSTGRES_USER=myuser
      - POSTGRES_PASSWORD=mypwd
    ports:
      - '5442:5432'
    volumes:
      - db12:/var/lib/postgresql/data
  db13:
    image: postgres:13
    restart: always
    environment:
      - POSTGRES_USER=myuser
      - POSTGRES_PASSWORD=mypwd
    ports:
      - '5443:5432'
    volumes:
      - db13:/var/lib/postgresql/data
  db14:
    image: postgres:14
    restart: always
    environment:
      - POSTGRES_USER=myuser
      - POSTGRES_PASSWORD=mypwd
    ports:
      - '5444:5432'
    volumes:
      - db14:/var/lib/postgresql/data
  db15:
    image: postgres:15
    restart: always
    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 

Step #3-构建docker -compose

docker-compose up -d

例如:

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$

Step #4-检查与创建的PostgreSQL DB服务器的连接。

连接到PostgreSQL DB服务器,版本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=#

连接到PostgreSQL DB服务器,版本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=#

Conclusion

在此博客中,我演示了如何使用Docker Compose在同一台计算机上安装第12、13、14和15版的PostgreSQL DB服务器。