PostgreSQL 基于Docker的多实例安装

参考:https://hub.docker.com/_/postgres/

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

 

利用Docker进行多实例的安装,相对于源码安装更简单,更方便,隔离性也更好 。

安装Docker

  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

根据系统写入:

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.

 

安装PostgreSQL

  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)

查看容器内数据文件、配置文件、网络授权文件设置

sudo docker exec -it 3bf3b533ba05 /bin/bash

所有postgresql配置文件和数据数据文件位于:

/var/lib/postgresql/data   

 

 

posted @ 2016-01-19 09:14  cxy486  阅读(7130)  评论(0编辑  收藏  举报