微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

【PostgreSQL】数据库复制

[postgres@king01 ~]$ cd /var/lib/pgsql/9.6/data
[postgres@king01 data]$ vi pg_hba.conf
host    replication     postgres        192.168.1.0/24          md5

[postgres@king01 data]$ vi postgresql.conf
listen_addresses = '0.0.0.0'
max_wal_senders = 5
wal_level = hot_standby 

[postgres@king01 data]$ pg_ctl restart
waiting for server to shut down.... done
server stopped
server starting

[root@king02 ~]# wget https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-centos96-9.6-3.noarch.rpm --no-check-certificate
[root@king02 ~]# rpm -ivh pgdg-centos96-9.6-3.noarch.rpm

[root@king02 ~]# useradd postgres
[root@king02 ~]# id postgres
uid=500(postgres) gid=500(postgres) groups=500(postgres)

[root@king02 ~]# rpm -e postgresql postgresql-devel
[root@king02 ~]# yum install -y postgresql96-server.x86_64 postgresql96-contrib.x86_64

[root@king02 ~]# vi /etc/profile
PATH=$PATH:/usr/pgsql-9.6/bin
export PGDATA=/var/lib/pgsql/9.6/data

[root@king02 ~]# su - postgres
[postgres@king02 ~]$ pg_basebackup -h 192.168.1.201 -U postgres -F p -P -x -R -D /var/lib/pgsql/9.6/data 
Password: 
2161085/2161085 kB (100%), 1/1 tablespace

[postgres@king02 ~]$ cd /var/lib/pgsql/9.6/data
[postgres@king02 data]$ ls -l
total 124
-rw------- 1 postgres postgres   208 Apr 14 18:26 backup_label
drwx------ 6 postgres postgres  4096 Apr 14 18:28 base
drwx------ 2 postgres postgres  4096 Apr 14 18:28 global
drwx------ 2 postgres postgres  4096 Apr 14 18:26 pg_clog
drwx------ 2 postgres postgres  4096 Apr 14 18:28 pg_commit_ts
drwx------ 2 postgres postgres  4096 Apr 14 18:28 pg_dynshmem
-rw------- 1 postgres postgres  4290 Apr 14 18:28 pg_hba.conf
-rw------- 1 postgres postgres  1636 Apr 14 18:28 pg_ident.conf
drwx------ 2 postgres postgres  4096 Apr 14 18:28 pg_log
drwx------ 4 postgres postgres  4096 Apr 14 18:28 pg_logical
drwx------ 4 postgres postgres  4096 Apr 14 18:28 pg_multixact
drwx------ 2 postgres postgres  4096 Apr 14 18:28 pg_notify
drwx------ 2 postgres postgres  4096 Apr 14 18:28 pg_replslot
drwx------ 2 postgres postgres  4096 Apr 14 18:28 pg_serial
drwx------ 2 postgres postgres  4096 Apr 14 18:28 pg_snapshots
drwx------ 2 postgres postgres  4096 Apr 14 18:26 pg_stat
drwx------ 2 postgres postgres  4096 Apr 14 18:28 pg_stat_tmp
drwx------ 2 postgres postgres  4096 Apr 14 18:28 pg_subtrans
drwx------ 2 postgres postgres  4096 Apr 14 18:28 pg_tblspc
drwx------ 2 postgres postgres  4096 Apr 14 18:28 pg_twophase
-rw------- 1 postgres postgres     4 Apr 14 18:28 PG_VERSION
drwx------ 3 postgres postgres  4096 Apr 14 18:28 pg_xlog
-rw------- 1 postgres postgres    88 Apr 14 18:26 postgresql.auto.conf
-rw------- 1 postgres postgres 22369 Apr 14 18:28 postgresql.conf
-rw-rw-r-- 1 postgres postgres   154 Apr 14 18:28 recovery.conf

[postgres@king02 data]$ cat recovery.conf 
standby_mode = 'on'
primary_conninfo = 'user=postgres password=postgres host=192.168.1.201 port=5432 sslmode=disable 
sslcompression=1 krbsrvname=postgres'

[postgres@king02 data]$ vi postgresql.conf
hot_standby = on 

[postgres@king02 ~]$ pg_ctl start
server starting

[postgres@king01 ~]$ psql
psql (9.6.12)
Type "help" for help.

postgres=# select pid,state,client_addr,sync_priority,sync_state from pg_stat_replication;
 pid  |   state   |  client_addr  | sync_priority | sync_state 
------+-----------+---------------+---------------+------------
 2055 | streaming | 192.168.1.202 |             0 | async
(1 row)

postgres=# select pg_xlog_location_diff(pg_current_xlog_location(),replay_location) from pg_stat_replication;
 pg_xlog_location_diff 
-----------------------
                     0
(1 row)

postgres=# select pg_is_in_recovery();
 pg_is_in_recovery 
-------------------
 f
(1 row)

[postgres@king02 ~]$ psql
psql (9.6.12)
Type "help" for help.

postgres=# select pg_is_in_recovery();
 pg_is_in_recovery 
-------------------
 t
(1 row)

postgres=# select pg_last_xlog_receive_location(),pg_last_xlog_replay_location(),pg_last_xact_replay_timestamp();
 pg_last_xlog_receive_location | pg_last_xlog_replay_location | pg_last_xact_replay_timestamp 
-------------------------------+------------------------------+-------------------------------
 0/4A001800                    | 0/4A001800                   | 
(1 row)


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐