PostgreSQL主从热备配置
主服务器:main.example.com
从服务器:spare.example.com

1、PostgreSQL安装
2、主数据库服务器设置
添加从服务器信息
vi /var/lib/pgsql/9.3/data/pg_hba.conf

# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
host    all             all             192.168.0.0/24          trust
# 配置从数据库,spare.example.com
host    replication     postgres        192.168.0.27/32         trust
#设置从数据库同步时使用的用户,以及从数据库的ip地址,此处直接用主数据库的postgres账户,可以自己在主数据上新建一个专用同步账号

vi /var/lib/pgsql/9.3/data/postgresql.conf
wal_level = hot_standby
checkpoint_segments = 16
archive_mode = on
archive_command = 'cp %p /var/lib/pgsql/9.3/data/pg_archive/%f'
max_wal_senders = 10          
wal_keep_segments = 32
log_destination = 'csvlog'
logging_collector = on
log_directory = '/var/log/pgsql-log/'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_truncate_on_rotation = on
log_rotation_age = 1d
log_rotation_size = 10MB

log_min_duration_statement = 1000ms
log_lock_waits = on
log_statement = 'ddl'
log_timezone = 'PRC'
lc_messages = 'en_US.UTF-8'
lc_monetary = 'en_US.UTF-8
lc_numeric = 'en_US.UTF-8'
lc_time = 'en_US.UTF-8'
deadlock_timeout = 1s
autovacuum = on
log_autovacuum_min_duration = 0
check_function_bodies = on

建立归档文件和日志文件
mkdir /var/lib/pgsql/9.3/data/pg_archive
chown -R postgres.postgres /var/lib/pgsql/9.3/data/pg_archive
mkdir /var/log/pgsql-log
chown -R postgres.postgres /var/log/pgsql-log

重启动服务
service postgresql-9.3 restart
Stopping postgresql-9.3 service:                           [  OK  ]
Starting postgresql-9.3 service:                           [  OK  ]

psql -U postgres
psql (9.3.4)
Type "help" for help.

基础备份
psql -U postgres

postgres=# select pg_start_backup('');
pg_start_backup
-----------------
0/4000028
(1 row)

postgres=# \q


初始化并启动从数据库,并删除 data目录
/etc/init.d/postgresql-9.3 initdb
service postgresql-9.3 start
chkconfig postgresql-9.3 on
rm -rf /var/lib/pgsql/9.3/data

拷贝数据库至从服务器
scp -rp /var/lib/pgsql/9.3/data root@spare.example.com:/var/lib/pgsql/9.3/
修改从服务器目录权限
chown -R postgres.postgres /var/lib/pgsql/9.3/data

结束主数据库的备份状态,再拷贝主数据的存档文件到从数据库
psql -U postgres
psql (9.3.4)
Type "help" for help.

postgres=# select pg_stop_backup();
NOTICE:  pg_stop_backup complete, all required WAL segments have been archived
pg_stop_backup
----------------
0/4014C90
(1 row)

postgres=# \q

查看归档信息
cd /var/lib/pgsql/9.3/data/pg_archive
ls
000000010000000000000002  000000010000000000000004
000000010000000000000003  000000010000000000000004.00000028.backup

复制存档文件夹至从服务器
scp -rp /var/lib/pgsql/9.3/data/pg_archive root@spare.example.com:/var/lib/pgsql/9.3/data/
修改从服务器目录权限
chown -R postgres.postgres /var/lib/pgsql/9.3/data/pg_archive
从数据库配置
mkdir /var/log/pgsql-log
chown -R postgres.postgres /var/log/pgsql-log

vi /var/lib/pgsql/9.3/data/postgresql.conf
hot_standby = on  

新建recovery.conf文件,并录入以下内容
vi /var/lib/pgsql/9.3/data/recovery.conf

restore_command = 'cp /var/lib/pgsql/9.3/data/pg_archive/%f %p'
standby_mode = 'on'
primary_conninfo = 'host=main.example.com port=5432 user=postgres password=postgres'

删除从数据库postmaster.pid文件以及pg_xlog下的文件,并重新启动服务
rm /var/lib/pgsql/9.3/data/postmaster.pid
rm -rf /var/lib/pgsql/9.3/data/pg_xlog/*
service postgresql-9.3 restart


登陆从服务器查看数据库
psql -U postgres
psql (9.3.4)
Type "help" for help.

postgres=# \l
                                 List of databases
  Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges  

-----------+----------+----------+-------------+-------------+----------------------
-
postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          
+
          |          |          |             |             | postgres=CTc/postgres
template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          
+
          |          |          |             |             | postgres=CTc/postgres
tigase    | tigase   | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)

postgres=#


登陆主服务器创建测试数据库

psql -U postgres
psql (9.3.4)
Type "help" for help.

postgres=# create database test owner tigase;
CREATE DATABASE

登陆从服务器查看数据库
psql -U postgres
psql (9.3.4)
Type "help" for help.

postgres=# \l
                                 List of databases
  Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges  

-----------+----------+----------+-------------+-------------+----------------------
-
postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          
+
          |          |          |             |             | postgres=CTc/postgres
template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          
+
          |          |          |             |             | postgres=CTc/postgres
test      | tigase   | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
tigase    | tigase   | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(5 rows)

postgres=#