目標:在一臺機器上運行多個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 }
2.2 修改/etc/postgresql/8.3/main/pg_hba.conf文件
該文件是PostgreSQL Server完成對客戶身份鑑別的配置文件,詳細內容可參見這裏。在此文件內容中添加一行如下的內容(如果是線上的機器,千萬別這麼做,太危險了。):
- host all all 0.0.0.0 0.0.0.0 trust
啓動完成後,運行 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
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的配置文件都在/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
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