PostgreSQL 基於Docker的多實例安裝

參考:https://hub.docker.com/_/postgres/sql

https://docs.docker.com/engine/installation/debian/docker

 

利用Docker進行多實例的安裝,相對於源碼安裝更簡單,更方便,隔離性也更好 。數據庫

安裝Dockervim

  1. 更新安裝源
sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

sudo touch /etc/apt/sources.list.d/docker.list

sudo vim /etc/apt/sources.list.d/docker.list

根據系統寫入:bash

deb https://apt.dockerproject.org/repo debian-jessie main網絡

  1. 安裝
sudo apt-get update

sudo apt-get install docker-engine
  1. 開啓服務
sudo service docker start
  1. 驗證
sudo docker run hello-world

該命令會下載一個測試鏡像,並在容器內運行,打印出相關信息,當看到相似以下信息是表示運行正常。

Hello from Docker.

This message shows that your installation appears to be working correctly.

 

To generate this message, Docker took the following steps:

 1. The Docker client contacted the Docker daemon.

 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.

 3. The Docker daemon created a new container from that image which runs the

    executable that produces the output you are currently reading.

 4. The Docker daemon streamed that output to the Docker client, which sent it

to your terminal.

 

安裝PostgreSQLapp

  1. 下載最新官方PostgeSQL鏡像
sudo docker pull postgres
  1. 啓動 兩個鏡像,分別映射至宿主機5454及5455端口
sudo docker run --name cluster1 -p 5454:5432 -e POSTGRES_PASSWORD=postgres -d postgres

sudo docker run --name cluster2 -p 5455:5432 -e POSTGRES_PASSWORD=postgres -d postgres
  1. 查看運行中的容器及相關信息
cxy@debian:~$ sudo docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES

3bf3b533ba05        postgres            "/docker-entrypoint.s"   26 seconds ago      Up 24 seconds       0.0.0.0:5455->5432/tcp   cluster2

29b9ffeb6ac8        postgres            "/docker-entrypoint.s"   58 seconds ago      Up 56 seconds       0.0.0.0:5454->5432/tcp   cluster1

cxy@debian:~$ sudo docker inspect 3bf3b533ba05 |grep IPAddress

        "SecondaryIPAddresses": null,

        "IPAddress": "172.17.0.3",

                "IPAddress": "172.17.0.3",

cxy@debian:~$ sudo docker inspect 29b9ffeb6ac8 |grep IPAddress

        "SecondaryIPAddresses": null,

        "IPAddress": "172.17.0.2",

                "IPAddress": "172.17.0.2",

 

  1. 根據上一步中的IPAddress,進行鏈接測試
cxy@debian:~$ psql -h172.17.0.2 -Upostgres -p5432

Password for user postgres:

psql (9.5.0)

Type "help" for help.

 

postgres=# \l

                                 List of databases

   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges  

-----------+----------+----------+------------+------------+-----------------------

 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |

 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +

           |          |          |            |            | postgres=CTc/postgres

 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +

           |          |          |            |            | postgres=CTc/postgres

(3 rows)

cxy@debian:~$ psql -h172.17.0.3 -Upostgres -p5432

Password for user postgres:

psql (9.5.0)

Type "help" for help.

 

postgres=# \l

                                 List of databases

   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges  

-----------+----------+----------+------------+------------+-----------------------

 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |

 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +

           |          |          |            |            | postgres=CTc/postgres

 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +

           |          |          |            |            | postgres=CTc/postgres

(3 rows)

經過宿主機地址和端口鏈接數據庫
cxy@debian:~$ psql -h192.168.10.131 -p5454 -Upostgres

Password for user postgres:

psql (9.5.0)

Type "help" for help.

 

postgres=# \l

                                 List of databases

   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges  

-----------+----------+----------+------------+------------+-----------------------

 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |

 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +

           |          |          |            |            | postgres=CTc/postgres

 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +

           |          |          |            |            | postgres=CTc/postgres

(3 rows)

 

postgres=# \q

cxy@debian:~$ psql -h192.168.10.131 -p5455 -Upostgres

Password for user postgres:

psql (9.5.0)

Type "help" for help.

 

postgres=# \l

                                 List of databases

   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges  

-----------+----------+----------+------------+------------+-----------------------

 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |

 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +

           |          |          |            |            | postgres=CTc/postgres

 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +

           |          |          |            |            | postgres=CTc/postgres

(3 rows)

查看容器內數據文件、配置文件、網絡受權文件設置tcp

sudo docker exec -it 3bf3b533ba05 /bin/bash

全部postgresql配置文件和數據數據文件位於:

/var/lib/postgresql/data   
相關文章
相關標籤/搜索