一、 简介:

         PG在9.*版本后热备提供了新的一个功能,那就是Stream Replication的读写分离,是PG高可用性的一个典型应用。这个功能在oracle中叫active dataguard,在PostgreSQL中称为hot standby。 
 

二、系统环境

   系统平台:CentOS 6.2
   PostgreSQL版本:9.5.0
   master :   192.168.1.202
   slave  :   192.168.1.201
 
、搭建步骤
   数据库搭建参考地址http://www.cnblogs.com/lottu/p/5149191.html
   注:slave库可不需要init数据库;
 
  • master库操作
  1. 创建流复制用户repuser
CREATE USER repuser replication LOGIN CONNECTION LIMIT 3 ENCRYPTED PASSWORD 'li0924';
# replication    做流复制的时候用到的一个用户属性,一般单独设定。
# LOGIN  用户登录的属性;CREATE USER默认自带;所以LOGIN关键字可以略去。
 
   2. 在master数据库的/data/pgdata/postgresql.conf文件中设置如下配置项: 
 
wal_level = hot_standby
max_wal_senders = 1
wal_keep_segments = 32
# max_wal_senders   是slave库的节点数,有多少个slave库就设多少,
# wal_keep_segments  默认值是16,是PG_XLOG下的日志文件数相关参数
 
   3.在主数据库中的/var/lib/pgsql/data/pg_hba.conf中添加如下配置:
host    replication     repuser         192.168.1.201/16          md5
# 第二项必须填 replication;

4.重新启动主数据库,让配置生效:

pg_stop;pg_start
  5.采用热备份的方式;把master数据库的PGDATA目录下面的文件传到slave库中.同时把创建的数据文件夹也传过去;
这个步骤就是一个数据库克隆的工作。
 
  • slave库操作

1.在slave数据库的/data/pgdata/postgresql.conf文件中设置如下配置项:

hot_standby = on

2.在PGDATA目录下;也就这里的/data/pgdata;新建文件recovery.conf

standby_mode = 'on'
primary_conninfo = 'host=192.168.1.202 port=5432 user=repuser password=li0924'
trigger_file = '/data/pgdata/trigger_standby'                   

3.删除原先从master库上过来的/data/pgdata/postmaster.pid文件,

rm /data/pgdata/postmaster.pid   

4.根据master库中的环境变量文件;修改slave库的postgres用户的环境变量 然后启动备库:

pg_start
 
 

四、验证工作

  • 查看进程

master库

[postgres@sdserver40_210 ~]$ ps -ef | grep postgre
root      2021   556  0 15:18 pts/1    00:00:00 su - postgres
postgres  2022  2021  0 15:18 pts/1    00:00:00 -bash
postgres  2239     1  0 15:24 pts/1    00:00:00 /opt/pgsql95/bin/postgres
postgres  2249  2239  0 15:24 ?        00:00:00 postgres: checkpointer process   
postgres  2250  2239  0 15:24 ?        00:00:00 postgres: writer process   
postgres  2251  2239  0 15:24 ?        00:00:00 postgres: wal writer process   
postgres  2252  2239  0 15:24 ?        00:00:00 postgres: autovacuum launcher process   
postgres  2253  2239  0 15:24 ?        00:00:00 postgres: archiver process   last was 00000006000000000000001E.00000028.backup
postgres  2254  2239  0 15:24 ?        00:00:00 postgres: stats collector process   
postgres  3235  2239  0 15:54 ?        00:00:00 postgres: wal sender process repuser 183.60.192.229(40399) streaming 0/1F000D80
   

slave库

[postgres@sdserver40_222 pgdata]$ ps -ef | grep postgres
postgres  6856     1  0 15:54 pts/0    00:00:00 /opt/pgsql/bin/postgres
postgres  6863  6856  0 15:54 ?        00:00:00 postgres: startup process   recovering 00000006000000000000001F
postgres  6864  6856  0 15:54 ?        00:00:00 postgres: checkpointer process   
postgres  6865  6856  0 15:54 ?        00:00:00 postgres: writer process   
postgres  6866  6856  0 15:54 ?        00:00:00 postgres: wal receiver process   streaming 0/1F000CA0
postgres  6867  6856  0 15:54 ?        00:00:00 postgres: stats collector process   
root      6922 30527  0 16:08 pts/0    00:00:00 su - postgres
postgres  6923  6922  0 16:08 pts/0    00:00:00 -bash
postgres  6974  6923  0 16:49 pts/0    00:00:00 ps -ef
postgres  6975  6923  0 16:49 pts/0    00:00:00 grep postgres
 
 
  • 数据验证

在master库操作

[postgres@sdserver40_210 ~]$ psql mydb repuser
psql (9.5.0)
Type "help" for help.
mydb=> \d 
             List of relations
 Schema | Name  |     Type      |  Owner   
--------+-------+---------------+----------
 public | dept  | table         | lottu
 public | emp   | foreign table | postgres
 public | test  | table         | lottu
 public | trade | table         | lottu
(4 rows)
 
mydb=> create table t_lottu (id int primary key,name varchar(20));
CREATE TABLE
mydb=> \d
              List of relations
 Schema |  Name   |     Type      |  Owner   
--------+---------+---------------+----------
 public | dept    | table         | lottu
 public | emp     | foreign table | postgres
 public | t_lottu | table         | repuser
 public | test    | table         | lottu
 public | trade   | table         | lottu
(5 rows)
 
mydb=> insert into t_lottu values (1001,'lottu');
INSERT 0 1
mydb=> insert into t_lottu values (1002,'vincent');
INSERT 0 1
mydb=> insert into t_lottu values (1003,'rax');
INSERT 0 1
mydb=> select * from t_lottu;
  id  |  name   
------+---------
 1001 | lottu
 1002 | vincent
 1003 | rax
(3 rows)
 

登录slave库查看数据

[postgres@sdserver40_222 pgdata]$ psql mydb repuser
psql (9.5.0)
Type "help" for help.
 
mydb=> select * from t_lottu;
  id  |  name   
------+---------
 1001 | lottu
 1002 | vincent
 1003 | rax
(3 rows)
 
mydb=> delete from t_lottu where id = 1003;
ERROR:  cannot execute DELETE in a read-only transaction

参考博客 --http://my.oschina.net/Kenyon/blog/54967

PostgreSQL Hot Standby的搭建的更多相关文章

  1. postgresql数据库异步流复制hot standby环境搭建

    生命不息,test不止. 最近组里面修改了几个postgresql的bug,要进行回归测试,除了前面提到的WAL的RT测试和Mirroring Controller的RT测试,还要测试下postgre ...

  2. PostgreSQL Hot Standby的主备切换

    一. 简介:          PG在9.*版本后热备提供了新的一个功能,那就是Stream Replication的读写分离,是PG高可用性的一个典型应用.其中备库是只读库:若主库出现故障:备库这个 ...

  3. Postgresql监控pgwatch的搭建

    一,需要环境: You will need a handful of components to make this work: - Apache (webserver) #apache搭建web页面 ...

  4. Logical standby database 搭建(配置)

    说明 Logical standby 数据库是通过Physical standby数据库转换的.本Logical standby是通过之前创建的Physical standby转换的. Physica ...

  5. postgresql 10.5 主从复制--搭建测试

    env: role master slave host pg1 pg2 ip 11 12 pg-version 10.5 10.5 1 初始化查看 [ceiec@localhost ~]$ df -h ...

  6. postgresql集群的搭建

    目录 架构图 部署详情 postgresql的安装与配置 pgpool的安装与配置 写在安装前 postgresql是一款很强大的数据库,具体有多强大呢,请谷歌... 网上的相关资料不是很多,参考了大 ...

  7. ORACLE Physical Standby DG搭建

    主库: 一:强制force logging: alter database force logging; 二:开启主库的归档模式 三:主库添加standby redo log,比redo日志组多一组: ...

  8. Nginx+PostgreSQL+Django+UWSGI搭建

    最近因为项目上的需要开始大量使用nginx,因此也想趁机将以前常用的django+apache的架构换成django+nginx.常见的 django webapp 部署方式采用FCGI 或 WSGI ...

  9. postgreSQL 时间线

    “时间线”(Timeline)是PG一个很有特色的概念,在备份恢复方面的文档里面时有出现.但针对这个概念的详细解释却很少,也让人不太好理解,我们在此仔细解析一下. 时间线的引入 为了理解引入时间线的背 ...

随机推荐

  1. UILabel属性

    1.text:设置标签显示文本. 2.attributedText:设置标签属性文本. Ios代码 NSString *text = @"first"; NSMutableAttr ...

  2. Function.prototype.bind接口浅析

    本文大部分内容翻译自 MDN内容, 翻译内容经过自己的理解. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo ...

  3. Android之Handler,举例说明如何更新UI

    方法一:(java习惯,在android不推荐使用) 刚刚开始接触android线程编程的时候,习惯好像java一样,试图用下面的代码解决问题 new Thread( new Runnable() { ...

  4. 基于DES算法加密的防撞库密码系统项目总结

    项目内容:基于DES算法加密的防撞库密码系统 小组名:zqhzkzkj 目标:1.对用户输入的8位字符进行DES加密,要求用户输入8位密钥 2.对于不同的网站,不同的用户名生成不同的密码 小组成员:周 ...

  5. 实现 DIV 固定定位在网页主体部分最右侧

    position:fixed 相对于窗口的固定定位,这个窗口可理解为可视窗口,除了浏览器自己的东西,剩下的就是这个可视窗口.而大部分的网页都是窄屏设计,比如说网页主体部分固定宽 1200px,或者自适 ...

  6. Orthomcl的详细使用

    参考了众多文章并结合实际操作后的感想. 参考:http://www.plob.org/2013/09/18/6174.html 参考:http://www.plob.org/2012/06/12/22 ...

  7. 树形DP(Holiday's Accommodation HDU4118)

    题意:有n间房子,之间有n-1条道路连接,每个房间里住着一个人,这n个人都想到其他房间居住,并且每个房间不能有两个人,问所有人的路径之和最大是多少? 分析:对于每条边来说,经过改边的人由该边两端元素个 ...

  8. fzu 2111 Min Number

      http://acm.fzu.edu.cn/problem.php?pid=2111  Problem 2111 Min Number Accept: 572    Submit: 1106Tim ...

  9. poj 2509 Peter's smokes

    http://poj.org/problem?id=2509 Peter's smokes Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  10. Java基础(30):String对象的常用方法与实例(String类)

    Java 中 String 类的常用方法 Ⅰ String 类提供了许多用来处理字符串的方法,例如,获取字符串长度.对字符串进行截取.将字符串转换为大写或小写.字符串分割等,下面我们就来领略它的强大之 ...