運行環境:
Primary: 192.168.111.150
Standby: 192.168.111.151
192.168.111.152
OS: CentOS5.8
PostgreSQL: 9.1.2 版本以上才支持Streaming Replication。
1. 環境規划
Primary和Standby節點最好有相同的環境。
2. 安裝PostgreSQL
1)在Primary和Standy節點上安裝PostgreSQL軟件,安裝路徑為/opt/pgsql-9.1.2
2)設置postgres用戶的環境變量
PGHOME=/opt/pgsql-9.1.2
PGDATA=/storage0/database/postgres/main
PATH=$PG_HOME/bin:$PATH:$HOME/bin
3. Primary節點
1)切換到postgres用戶
$su - postgres
2)初始化數據庫
$initdb
3)配置pg_hba.conf
在# IPv4 local connections下面添加一行,設置PostgreSQL的訪問及其權限
host all all 192.168.111.1/24 trust
在# replication privilege.下面添加一行,設置replication用戶及權限
host replication postgres 192.168.111.1/24 trust
4)配置postgresql.conf
配置監聽,修改listen_addresses = 'localhost'
listen_addresses = '*'# what IP address(es) to listen on;
配置Primary Replication參數
wal_level = hot_standby
max_wal_senders = 5
wal_keep_segments = 32
archive_mode = on
archive_command = 'cp %p /storage0/database/postgres/archive/%f < /dev/null'
"/storage0/database/postgres/archive"是Replication的archive的存儲路徑。PostgreSQL會將Replication的WAL保存在 "/storage0/database/postgres/archive"路徑下。
5) 啟動Primary上的PostgreSQL數據庫
$pg_ctl start
6) 在primary上執行以下命令
$psql -c "SELECT pg_start_backup('label', true)"
將Primary的PGDATA目錄下的文件,除了postmaster.pid復制到Standby節點的“/storage0/database/postgres/main”目錄下,該目錄是 Standby節點上的PostgreSQL數據庫的PGDATA目錄。
$rsync -a ${PGDATA}/ postgres@192.168.111.151:/storage0/database/postgres/main --exclude postmaster.pid
$psql -c "SELECT pg_stop_backup()"

192.168.111.151的/storage0/database/postgres/main目錄下的內容為

4. Standby節點
PGDATA=/storage0/database/postgres/main
Standby節點的PGDATA路徑就是Primary節點的PGDATA的副本
1)配置postgresql.conf
設置hot_standby為
hot_standby= on
2)編輯recovery.conf,文件路徑為$(PGDATA)/recovery.conf,內容為
---------------------------------------------------------------------------------------------------------------------------
# Specifies whether to start the server as a standby. In streaming replication,
# this parameter must to be set to on.
standby_mode = 'on'
# Specifies a connection string which is used for the standby server to connect
# with the primary.
primary_conninfo = 'host=192.168.111.150 port=5432 user=postgres'
# Specifies a trigger file whose presence should cause streaming replication to
# end (i.e., failover).
trigger_file = '/storage0/database/postgres/trigger'
# Specifies a command to load archive segments from the WAL archive. If
# wal_keep_segments is a high enough number to retain the WAL segments
# required for the standby server, this may not be necessary. But
# a large workload can cause segments to be recycled before the standby
# is fully synchronized, requiring you to start again from a new base backup.
restore_command = 'cp /storage0/database/postgres/archive/%f %p'
--------------------------------------------------------------------------------------------------------------------------
3)復制pg_xlog下的所有文件到/storage0/database/postgres/archive目錄下
4)啟動Standby節點,完成Replication。
完成Streaming Replication配置。
啟動pgAdmin,連接192.168.111.150節點,創建test數據庫,刷新 192.168.111.151節點。可以看到在192.168.111.151上也生成了test數據庫。