主库IP:192.168.230.128

备库IP:192.168.230.129

PostgreSQL版本:

主备机PostgreSQL源码包均位于/opt/soft_bak

OS:CentOS5

主备库PostgreSQL均安装在/usr/local/pg952目录下

数据目录均在/usr/local/pg952/data

主备机为为postgres用户配置如下环境变量:

export PGPORT=5432(postgresql.conf中的端口)

export PGDATA=/usr/local/pg952/data

export LANG=en_US.utf8

export PGHOME=/usr/local/pg952

export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH

export DATE='date + "%Y%m%d%H%M"'

export PATH=$PGHOME/bin:$PATH:.

export MANPATH=$PGHOME/share/man:$MANPATH

export PGHOST=$PGDATA

export PGDATABASE=postgres

export PGUSER=postgres

alias rm='rm -i'

alias ll='ls -lh'

安装PostgreSQL(主、备机器均需要安装PostgreSQL)

创建postgres用户

useradd postgres -p 123456

创建PostgreSQL安装目录

[root@localhost ~]# cd /usr/local/

[root@localhost local]# mkdir pg952

chown -R postgres:postgres /usr/local/pg952/

安装PostgreSQL运行所需要的依赖软件包

yum -y install readline readline-devel zlib zlib-devel openssl openssl-devel gcc make flex bison

解压PostgreSQL

chown -R postgres:postgres /opt/soft_bak/

su - postgres

[postgres@localhost soft_bak]$ tar zxvf postgresql-9.5.2.tar.gz

指定PostgreSQL的安装目录并安装

[postgres@localhost soft_bak]$ cd postgresql-9.5.2

[postgres@localhost postgresql-9.5.2]$ ./configure --prefix=/usr/local/pg952/

[postgres@localhost postgresql-9.5.2]$ gmake world -j 32

[postgres@localhost postgresql-9.5.2]$ gmake install-world -j 32

主库创建PostgreSQL 数据目录

[postgres@localhost postgresql-9.5.2]$ cd /usr/local/pg952/

[postgres@localhost pg952]$ mkdir data

[postgres@localhost pg952]$ ls

bin  data  include  lib  share

备库创建PostgreSQL数据目录

[postgres@localhost postgresql-9.5.2]$ cd /usr/local/pg952/

[postgres@localhost pg952]$ mkdir data

[postgres@localhost pg952]$ chmod 0700 /usr/local/pg952/data/

[postgres@localhost pg952]$ ls

bin  data  include  lib  share

主库初始化PostgreSQL集群

[postgres@localhost pg952]$ initdb -D $PGDATA -k -E UTF8 --locale=C -U postgres -W

配置主库

postgresql.conf

listen_addresses = '*'

port = 5432

max_connections = 500

superuser_reserved_connections = 10

unix_socket_directories = '.'

password_encryption = on

shared_buffers = 2048MB

wal_level = hot_standby

fsync = on

synchronous_commit = on

archive_mode = on

archive_command = 'cd .'

max_wal_senders = 5

hot_standby = on

max_replication_slots = 1

max_wal_size = 10GB

log_destination = 'csvlog'

logging_collector = on

log_directory = 'pg_log'

log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'

log_file_mode = 0600

log_truncate_on_rotation = on

pg_hba.conf

host    all             all           127.0.0.1/32             trust

host    all             all           192.168.230.0/24         trust

host    replication     postgres        127.0.0.1/32             trust

host    replication     postgres        192.168.230.129/24       trust

启动主库

[postgres@localhost pg952]$ pg_ctl -D $PGDATA start

主库创建slot

[postgres@localhost pg952]$ psql -h 192.168.230.128 -p 5432 -U postgres

Password for user postgres:

psql (9.5.2)

Type "help" for help.

postgres=# select pg_is_in_recovery();

-[ RECORD 1 ]-----+--

pg_is_in_recovery | f

postgres=# select * from pg_create_physical_replication_slot('slot1');

slot_name | xlog_position

-----------+---------------

slot1     |

(1 row)

postgres=# \x

Expanded display is on.

postgres=# select * from pg_replication_slots;

-[ RECORD 1 ]+---------

slot_name    | slot1

plugin       |

slot_type    | physical

datoid       |

database     |

active       | f

active_pid   |

xmin         |

catalog_xmin |

restart_lsn  |

备库从主库做basebackup(即主库的基础备份)

[postgres@localhost bin]$ pg_basebackup -h 192.168.230.128 -P -Fp -Xs -v -p 5432 -U postgres -D /usr/local/pg952/data/

WARNING:  skipping special file "./.s.PGSQL.5432"

transaction log start point: 0/7000024 on timeline 1

pg_basebackup: starting background WAL receiver

WARNING:  skipping special file "./.s.PGSQL.5432"g952/data//backup_label)

21715/21715 kB (100%), 1/1 tablespace

transaction log end point: 0/7000118

pg_basebackup: waiting for background process to finish streaming ...

pg_basebackup: base backup completed

[postgres@localhost bin]$ cp /usr/local/pg952/share/postgresql/recovery.conf.sample /usr/local/pg952/data/recovery.conf

[postgres@localhost bin]$ cd /usr/local/pg952/data/

recovery.conf

recovery_target_timeline = 'latest'

standby_mode = on

primary_conninfo = 'host=192.168.230.128 port=5432 user=postgres password=postgres'

primary_slot_name = 'slot1'

启动备库

[postgres@localhost data]$ pg_ctl -D $PGDATA start

在主库上验证slot工作状态

[postgres@localhost pg952]$ psql -h 192.168.230.128 -p 5432 -U postgres

psql (9.5.2)

Type "help" for help.

postgres=# \x

Expanded display is on.

postgres=# select * from pg_replication_slots;

-[ RECORD 1 ]+----------

slot_name    | slot1

plugin       |

slot_type    | physical

datoid       |

database     |

active       | t

active_pid   | 30131

xmin         |

catalog_xmin |

restart_lsn  | 0/80001F8

在主库上查看复制状态

postgres=# select * from pg_stat_replication ;

-[ RECORD 1 ]----+------------------------------

pid              | 30131

usesysid         | 10

usename          | postgres

application_name | walreceiver

client_addr      | 192.168.230.129

client_hostname  |

client_port      | 44356

backend_start    | 2016-04-25 21:40:27.740259-07

backend_xmin     |

state            | streaming

sent_location    | 0/80181F0

write_location   | 0/80181F0

flush_location   | 0/80181F0

replay_location  | 0/80181F0

sync_priority    | 0

sync_state       | async

验证主备复制

主库创建表并插入数据

[postgres@localhost pg952]$ psql -h 192.168.230.128 -p 5432 -U postgres

psql (9.5.2)

Type "help" for help.

postgres=# create table test(id int);

CREATE TABLE

postgres=# insert into test values (1),(2),(3),(4),(5);

INSERT 0 5

postgres=# select * from test ;

-[ RECORD 1 ]

id | 1

-[ RECORD 2 ]

id | 2

-[ RECORD 3 ]

id | 3

-[ RECORD 4 ]

id | 4

-[ RECORD 5 ]

id | 5

在从库上验证数据是否从主库复制到备库

[postgres@localhost data]$ psql -h 192.168.230.129 -p 5432 -U postgres

psql (9.5.2)

Type "help" for help.

postgres=# \d

List of relations

Schema | Name | Type  |  Owner

--------+------+-------+----------

public | test | table | postgres

(1 row)

postgres=# select * from test ;

id

----

1

2

3

4

5

(5 rows)

活跃状态的 slot 不可以删除,需要取消从库的 primary_slotname = 'slot1' 设置, 之后重启从库

active 表示是否使用,  如果不想使用 replication_slot ,需要删掉 slots.

删除slot

select pg_drop_replication_slot('slot1');

replication_slot and PostgreSQL Replication的更多相关文章

  1. PostgreSQL Replication之第一章 理解复制概念(1)

    PostgreSQL Replication系列翻译自PostgreSQL Replication一书 在本章中,将会介绍不同的复制概念,您会了解哪些类型的复制对哪一种实用场景是最合适的. 在本章的最 ...

  2. [转]PostgreSQL Replication之扩展与BDR

    原文:https://www.cnblogs.com/xmzzp/p/6284300.html postgres 实现master, slave ,且master是多主. -------------- ...

  3. PostgreSQL Replication之第十五章 与Walbouncer 一起工作

    与Walbouncer 一起工作 在本书的最后一章,将引导您通向2014年发布的一个工具,称为walbouncer.本书中的大多数技巧说明了如何复制整个数据库实例,如何分片,等等.在最后一章,是关于w ...

  4. PostgreSQL Replication之第十四章 扩展与BDR

    在这一章中,将向您介绍一个全新的技术,成为BDR.双向复制(BDR),在PostgreSQL的世界里,它绝对是一颗冉冉升起的新星.在不久的将来,许多新的东西将会被看到,并且人们可以期待一个蓬勃发展的项 ...

  5. PostgreSQL Replication之第十二章 与Postgres-XC一起工作(7)

    12.7 处理故障转移和删除节点 在本节中,我们将看看故障切换如何处理.我们还将看看如何使用安全可靠的方法添加节点到Postgres-XC设置以及如何从Postgres-XC设置删除节点. 12.7. ...

  6. PostgreSQL Replication之第十二章 与Postgres-XC一起工作(4)

    12.4 性能优化 Postgres-XC不是一个奇特的PostgreSQL版本,而是一个真正的分布式系统.这意味这,您不能只存储数据,希望事情超出服务器之外的快速,高效.如果您想优化速度,思考数据是 ...

  7. PostgreSQL Replication之扩展与BDR

    在这一章中,将向您介绍一个全新的技术,成为BDR.双向复制(BDR),在PostgreSQL的世界里,它绝对是一颗冉冉升起的新星.在不久的将来,许多新的东西将会被看到,并且人们可以期待一个蓬勃发展的项 ...

  8. PostgreSQL Replication之第四章 设置异步复制(4)

    4.4 基于流和基于文件的恢复 生活并不总只是黑色或白色:有时也会有一些灰色色调.对于某些情况下,流复制可能恰到好处.在另一些情况下,基于文件复制和PITR是您所需要的.但是也有许多情况下,您既需要流 ...

  9. PostgreSQL Replication之第四章 设置异步复制(2)

    4.2 配置级联复制 正如您在本章已经看到的,设置流复制真的很容易.只需要设置几个参数,做一个基础备份,并享受您的复制设置. 在许多情况下,这种情况更有一点点微妙.在这个例子中我们假设:我们要使用一个 ...

随机推荐

  1. java语言中数值自动转换的优先顺序

    转换原则:从低精度向高精度转换byte .short.int.long.float.double.char数据类型的转换,分为自动转换和强制转换.自动转换是程序在执行过程中“悄然”进行的转换,不需要用 ...

  2. Nginx 日志文件切割

    Nginx 是一个非常轻量的 Web 服务器,体积小.性能高.速度快等诸多优点.但不足的是也存在缺点,比如其产生的访问日志文件一直就是一个,不会自动地进行切割,如果访问量很大的话,将 导致日志文件容量 ...

  3. Linux core 文件介绍

    Linux core 文件介绍 http://www.cnblogs.com/dongzhiquan/archive/2012/01/20/2328355.html 1. core文件的简单介绍在一个 ...

  4. SpringMVC接收checkbox传值

    Controller方法形参接收checkbox的值,既可以用String,也可以用String[]. 字符串数组接收的测试代码如下: @Controller @RequestMapping(&quo ...

  5. [LeetCode]题解(python):035-Search Insert Position

    题目来源 https://leetcode.com/problems/search-insert-position/ Given a sorted array and a target value, ...

  6. JS控制flash的播放

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  7. Sql server与Excel的数据互通导入导出

    现在,我先从Sql server数据表导出到Excel中,再从Excel数据表导出到Sql server中: 一.Sql server数据表导出到Excel中: 1.新建一个Excel,选择“数据”菜 ...

  8. zt:如何快速赚取人生第一个100万?

    投递人 itwriter 发布于 2016-06-20 23:43 评论(16) 有1795人阅读 原文链接 [收藏] « » 不久前我开始使用「分答」,将回答问题的价格设置成 10 元.花最长 1 ...

  9. oracle语句随笔

    oracle语句随笔 dmp数据的导入. ; --创建用户 GRANT CONNECT,RESOURCE,DBA TO memsspc; --赋值权限 --cmd 中导入命令 IMP memsspc@ ...

  10. MySQL一些常用的时间函数

    https://my.oschina.net/sallency/blog/470370