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行修改爲以下內容:
  1. 253     my $postmaster_opts = '-i';
  2. 254     if (!(PgCommon::get_conf_value $version, $cluster, 'postgresql.conf''unix_socket_directory')) {
  3. 255     $postmaster_opts .= '-c unix_socket_directory="' . $info{'socketdir'} . '"';
  4. 256     }
        通過加上 -i 參數,使得通過pg_ctlcluster啓動的PostgreSQL Cluster能夠接受網絡訪問。
       2.2 修改/etc/postgresql/8.3/main/pg_hba.conf文件
       該文件是PostgreSQL Server完成對客戶身份鑑別的配置文件,詳細內容可參見這裏。在此文件內容中添加一行如下的內容(如果是線上的機器,千萬別這麼做,太危險了。):
  1. 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中看到如下內容:
  1. 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
  2. postgres  4897  4890  0 10:38 ?        00:00:00 postgres: writer process                                                      
  3. postgres  4898  4890  0 10:38 ?        00:00:00 postgres: wal writer process                                                  
  4. postgres  4899  4890  0 10:38 ?        00:00:00 postgres: autovacuum launcher process                                         
  5. postgres  4900  4890  0 10:38 ?        00:00:00 postgres: stats collector process                                             
  6. 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。  
  1. 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中增加下述內容:
  1. 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.....
  1. $ ps -ef | grep postgres
  2. 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
  3. postgres  4897  4890  0 10:38 ?        00:00:01 postgres: writer process                                                      
  4. postgres  4898  4890  0 10:38 ?        00:00:01 postgres: wal writer process                                                  
  5. postgres  4899  4890  0 10:38 ?        00:00:00 postgres: autovacuum launcher process                                         
  6. postgres  4900  4890  0 10:38 ?        00:00:00 postgres: stats collector process                                             
  7. 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
  8. pgD1      6311  6304  0 11:38 ?        00:00:00 postgres: writer process                                                      
  9. pgD1      6312  6304  0 11:38 ?        00:00:00 postgres: wal writer process                                                  
  10. pgD1      6313  6304  0 11:38 ?        00:00:00 postgres: autovacuum launcher process                                         
  11. pgD1      6314  6304  0 11:38 ?        00:00:00 postgres: stats collector process                                             
  12. 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
  13. pgD2      6576  6570  0 11:47 ?        00:00:00 postgres: writer process                                                      
  14. pgD2      6577  6570  0 11:47 ?        00:00:00 postgres: wal writer process                                                  
  15. pgD2      6578  6570  0 11:47 ?        00:00:00 postgres: autovacuum launcher process                                         
  16. pgD2      6579  6570  0 11:47 ?        00:00:00 postgres: stats collector process                                             

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章