PostgreSQL9.1 双机部署配置(主备数据同步)
首先下载PostgreSQL9.1,安装到主备Linux服务器上
1. 修改/opt/PostgreSQL/9.1/data/postgresq.conf文件(配置主服务器)
wal_level = hot_standby
max_wal_senders = 1
wal_keep_segments = 32
archive_mode = on
archive_command = 'cp %p /path_to/archive/%f'
2. 修改/opt/PostgreSQL/9.1/data/pg_hba.conf文件(设置访问权限)
Host replication postgres Standby_IP/32 trust
(standby_IP是备服务器IP,不用密码访问)
3. 启动主服务器,进行基础备份
/etc/init.d/postgresql-9.1 start (启动服务器)
psql -U postgres
postgres# select pg_start_backup('hot_backup'); (开始备份)
postgres# \q
tar -zcvf pgsql.tar.gz data/ (将data目录打包)
psql -U postgres
postgres# select pg_stop_backup(); (停止备份)
4. 将基础备份拷贝到备用服务器
scp pgsql.tar.gz Standby_IP:/usr/local/ (standby_IP是备服务器IP)
tar –zxvf pgsql.tar.gz (在备份服务器上解压)
chown postgres:postgres –R /usr/local/pgsql/data (修改文件所属用户)
5. 在备用服务器的PostgreSQL中配置 /usr/local/pgsql/data/postgresql.conf文件中的参数
vi postgresql.conf
hot_standby = on
6. 在备用服务器PostgreSQL的data目录下建立 /usr/local/pgsql/data/recovery.conf文件
vi recovery.conf
standby_mode = ‘on’
primary_conninfo = 'host=Primay_IP user=postgres port=5432'
7. 修改备用服务器启动脚本,启动备用服务器postgreSQL服务
vi /etc/init.d/postgresql-9.1
(将其中的/opt/PostgreSQL/9.1/data路径全部改为/usr/local/pgsql/data)
/etc/init.d/postgresql-9.1 start
最后要注意主备之间的防火墙是关闭的,否则无法自动同步数据