Postgres 9.2 Replication Configuration on CentOS 5

本文详细介绍了如何配置masterserver和slaveserver进行数据复制与同步,包括配置postgresql.conf和pg_hba.conf文件,执行数据备份与恢复流程,以及在slaveserver上进行数据库初始化和配置,最终实现读写分离与高可用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Master server configuration


Edit postgresql.conf

Add variables

archive_mode = on
archive_command = 'cp "%p" /data/pgsql/data/pg_xlog/archive_status/"%f"'
archive_timeout = 500
wal_level = 'hot_standby'
max_wal_senders=1
wal_keep_segments=50

Edit pg_hba.conf

Add the following line replace this ip with your slave host name

host     replication     postgres       192.168.1.15/32     trust

Next thing we need to do is take data snapshot of data from master and then move that to slave server

[postgres@zwc ~]$ psql -c "select pg_start_backup('replbackup');"
 pg_start_backup 
-----------------
 0/64000020
(1 row)
[postgres@zwc ~]$ tar -cfP pg_backup.tar data
[postgres@zwc ~]$ psql -c "select pg_stop_backup();"
NOTICE:  pg_stop_backup complete, all required WAL segments have been archived
 pg_stop_backup 
----------------
 0/640000E0
(1 row)

Move this data to slave server

[postgres@zwc ~]$ scp pg_backup.tar postgres@192.168.1.15:/tmp


Slave server configuration


Move the existing data directory to a new folder

[postgres@pgstandby ~]$ pgstop
[postgres@pgstandby ~]$ mv data data.old

Unzip master server data snapshot file that is copied into this server

[postgres@pgstandby ~]$ tar -xvf /tmp/pg_backup.tar 

Remove postmaster.pid so standby server does not see the primary server’s pid as its own

[postgres@pgstandby data]$ rm -f postmaster.pid 

Edit postgresql.conf

Add variables

hot_standby = on

Edit recovery.conf file

Add variables

[postgres@pgstandby data]$ cp -rp ../share/recovery.conf.sample recovery.conf
standby_mode = on
primary_conninfo = 'host=zwc port=1521'

Start slave database server

To test replication, simple insert into a table on master server and query the same from slave server

[postgres@zwc ~]$ psql -h zwc -U zwc -d testdb
Password for user zwc: 
psql (9.2.4)
Type "help" for help.

testdb=> \dt+
                       List of relations
 Schema |   Name    | Type  | Owner |    Size    | Description 
--------+-----------+-------+-------+------------+-------------
 public | orderinfo | table | zwc   | 8192 bytes | 
 public | t01       | table | zwc   | 8192 bytes | 
(2 rows)

testdb=> create table t02(id integer);
CREATE TABLE
testdb=> \dt+
                       List of relations
 Schema |   Name    | Type  | Owner |    Size    | Description 
--------+-----------+-------+-------+------------+-------------
 public | orderinfo | table | zwc   | 8192 bytes | 
 public | t01       | table | zwc   | 8192 bytes | 
 public | t02       | table | zwc   | 0 bytes    | 
(3 rows)

testdb=> \q
[postgres@zwc ~]$ 
[postgres@zwc ~]$ psql -h pgstandby -U zwc -d testdb
Password for user zwc: 
psql (9.2.4)
Type "help" for help.

testdb=> \dt+
                       List of relations
 Schema |   Name    | Type  | Owner |    Size    | Description 
--------+-----------+-------+-------+------------+-------------
 public | orderinfo | table | zwc   | 8192 bytes | 
 public | t01       | table | zwc   | 8192 bytes | 
 public | t02       | table | zwc   | 0 bytes    | 
(3 rows)

testdb=> insert into t02 values(1),(2);
ERROR:  cannot execute INSERT in a read-only transaction
testdb=> \q
[postgres@zwc ~]$ psql -h zwc -U zwc -d testdb
Password for user zwc: 
psql (9.2.4)
Type "help" for help.

testdb=> insert into t02 values(1),(2);
INSERT 0 2
testdb=> \q
[postgres@zwc ~]$ psql -h pgstandby -U zwc -d testdb
Password for user zwc: 
psql (9.2.4)
Type "help" for help.

testdb=> select * from t02;
 id 
----
  1
  2
(2 rows)

testdb=> 

Standby database log

STATEMENT:  insert into t02 values(1),(2);
LOG:  connection received: host=192.168.1.13 port=27595
LOG:  connection received: host=192.168.1.13 port=27596
LOG:  connection authorized: user=zwc database=testdb
LOG:  restartpoint starting: time
LOG:  restartpoint complete: wrote 21 buffers (0.5%); 0 transaction log file(s) added, 0 removed, 0 recycled; write=2.019 s, sync=0.009 s, total=2.031 s; sync files=17, longest=0.005 s, average=0.000 s
LOG:  recovery restart point at 0/7C015D50
DETAIL:  last completed transaction was at log time 2013-09-03 22:03:22.099392+08
ERROR:  cannot execute DELETE in a read-only transaction
STATEMENT:  delete from t02 where id=1;






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值