同一机器上如何运行多个PostgreSQL实例
最近要做一些有关PostgreSQL多实例运行的例子,一般都普遍需要在不同的机器上安装上PostgreSQL Server,然后在不同的机器上进行配置,比如通过plproxy进行一些demo的设置。当然这样的需求可以通过多个虚拟机的方式实现,比如创建多个vmware的虚拟机,大家一起跑就是了。不过这样有点资源浪费,而且需要维护不同的机器。尝试着在同一台机器上运行多个PostgreSQL Server实例来完成需要。
目标:在一台机器上运行多个PostgreSQL Server实例.
运行环境:Ubnntu 8.04.1(vmware中运行)
1. 首先通过sudo apt-get install
postgresql安装上PostgreSQL
Server。在安装的过程中会建议安装其它有关的pkg,可以都装上,最后安装完成后最好有以下包列表:
postgresql
postgresql-8.3
postgresql-client-8.3
postgresql-client-common
postgresql-common
安装完成后,apt-get会自动根据安装脚本完成PostgreSQL的数据库初始化工作,并启动一个PostgreSQL实例(称为一个cluster),且该实例的名称是:main.
请记住main,我们在后面还会用到。
2. 修改文件
2.1
修改/usr/bin/pg_ctlcluster
/usr/bin/pg_ctlcluster是用以控制PostgreSQL
Server启动、停止、重启的脚本,便于完成对PostgreSQL Server的控制。默认的PostgreSQL
cluster只能通过本地访问,不能通过TCP/IP以网络方式进行。修改/usr/bin/pg_ctlcluster文件,把253行修改为以下内容:
253 my $postmaster_opts = '-i';
254 if (!(PgCommon::get_conf_value $version, $cluster, 'postgresql.conf', 'unix_socket_directory')) {
255 $postmaster_opts .= '-c unix_socket_directory="' . $info{'socketdir'} . '"';
256 }
通过加上 -i 参数,使得通过pg_ctlcluster启动的PostgreSQL Cluster能够接受网络访问。2.2 修改/etc/postgresql/8.3/main/pg_hba.conf文件
该文件是PostgreSQL Server完成对客户身份鉴别的配置文件,详细内容可参见这里。在此文件内容中添加一行如下的内容(如果是线上的机器,千万别这么做,太危险了。):
host all all 0.0.0.0 0.0.0.0 trust
然后运行sudo -u postgres /usr/bin/pg_ctlcluster 8.3 main restart 重新启动。启动完成后,运行 ps -ef | grep postgres 可在console中看到如下内容:
postgres 4890 1 0 10:38 ? 00:00:01 /usr/lib/postgresql/8.3/bin/postgres -D /var/lib/postgresql/8.3/main -i -c config_file=/etc/postgresql/8.3/main/postgresql.conf
postgres 4897 4890 0 10:38 ? 00:00:00 postgres: writer process
postgres 4898 4890 0 10:38 ? 00:00:00 postgres: wal writer process
postgres 4899 4890 0 10:38 ? 00:00:00 postgres: autovacuum launcher process
postgres 4900 4890 0 10:38 ? 00:00:00 postgres: stats collector process
xuepeng 6068 5940 0 11:01 pts/0 00:00:00 grep postgres
我们的第一个pgcluster(名称为main)已经正常运行。
3. 创建第二个pg cluster
默认的pg
cluster的名称为main,我们创建的第二个cluster名称为pgD1(当然你可以随意命名,完全是自由的)。首先我们能想到的是main和pgD1应该运行在不同的端口号上。main使用默认的5432,为便于记忆,那我们让pgD1使用5433。其次,不同的cluster还必须使用不同数据文件目录,也就是ps结果中
-D /var/lib/postgresql/8.3/main
所代表的值。每一个运行的cluster都需要使用自己私有的数据文件,保存表、视图、函数、触发器等等一切的东西。因此我们需要一个不同的数据文件目录。以上两个方面是最重要的。
其实,在安装PostgreSQL的过程中已经安装了几个对pgcluster的维护脚本,上面的/usr/bin/pg_ctlcluster只是其中的一个。创建新的cluster可以使用/usr/bin/pg_createcluster来完成,非常方便。
在创建之前,为便于概念的理解,我们使用useradd
pgD1完成ubuntu的帐号创建,并使用该帐号作为pgD1的默认owner和superuser。
sudo /usr/bin/pg_createcluster -u 1001 -g 1001 -d /var/lib/postgresql /8.3/D1 -s /var/run/pgD1 --local zh_CN.UTF-8 -e utf8 -p 5433 --start --start-conf auto 8.3 pgD1
上述命令执行完成后即完成了名为pgD1的cluster的创建。-u 和-g 参数指明该cluster的默认owner和group,其它参数可参见pg_createcluster的help,很容易理解。完成创建后,pgD1的配置文件都在/etc/postgresql/8.3/pgD1/目录下。为了同样的原因,我们需要在/etc/postgresql/8.3/pgD1/pg_hba.conf中增加下述内容:
host all all 0.0.0.0 0.0.0.0 trust
最后,运行 sudo -u pgD1 /usr/bin/pg_ctlcluster 8.3 pgD1 restart 完成重启, 名称为pgD1的cluster创建完成。4. 按照上述步骤,可以继续创建新的pgcluster,pgD2, pgD3.....
$ ps -ef | grep postgres
postgres 4890 1 0 10:38 ? 00:00:01 /usr/lib/postgresql/8.3/bin/postgres -D /var/lib/postgresql/8.3/main -i -c config_file=/etc/postgresql/8.3/main/postgresql.conf
postgres 4897 4890 0 10:38 ? 00:00:01 postgres: writer process
postgres 4898 4890 0 10:38 ? 00:00:01 postgres: wal writer process
postgres 4899 4890 0 10:38 ? 00:00:00 postgres: autovacuum launcher process
postgres 4900 4890 0 10:38 ? 00:00:00 postgres: stats collector process
pgD1 6304 1 0 11:38 ? 00:00:00 /usr/lib/postgresql/8.3/bin/postgres -D /var/lib/postgresql/8.3/D1 -i -c config_file=/etc/postgresql/8.3/pgD1/postgresql.conf
pgD1 6311 6304 0 11:38 ? 00:00:00 postgres: writer process
pgD1 6312 6304 0 11:38 ? 00:00:00 postgres: wal writer process
pgD1 6313 6304 0 11:38 ? 00:00:00 postgres: autovacuum launcher process
pgD1 6314 6304 0 11:38 ? 00:00:00 postgres: stats collector process
pgD2 6570 1 3 11:47 ? 00:00:00 /usr/lib/postgresql/8.3/bin/postgres -D /var/lib/postgresql/8.3/D2 -i -c config_file=/etc/postgresql/8.3/pgD2/postgresql.conf
pgD2 6576 6570 0 11:47 ? 00:00:00 postgres: writer process
pgD2 6577 6570 0 11:47 ? 00:00:00 postgres: wal writer process
pgD2 6578 6570 0 11:47 ? 00:00:00 postgres: autovacuum launcher process
pgD2 6579 6570 0 11:47 ? 00:00:00 postgres: stats collector process
同一机器上如何运行多个PostgreSQL实例