-- 该路径也需要在从库创建
mkdir -p /postgresql/archive
chown -R postgres.postgres /postgresql/archive
-- 主库配置归档
wal_level='replica'
archive_mode='on'
archive_command='cp %p /postgresql/archive/%f'
restore_command='cp /postgresql/archive/%f %p'
max_wal_senders=10
wal_sender_timeout=60s
wal_keep_size=256
--重启数据库
pg_ctl stop
pg_ctl start
[root@test archive]# ps -ef|grep post
root 1485 1 0 08:57 ? 00:00:00 /usr/libexec/postfix/master -w
postfix 1502 1485 0 08:57 ? 00:00:00 qmgr -l -t unix -u
postfix 18200 1485 0 13:55 ? 00:00:00 pickup -l -t unix -u
pgsql 22160 1 0 14:12 ? 00:00:00 /postgresql/pg13/bin/postgres
pgsql 22162 22160 0 14:12 ? 00:00:00 postgres: logger
pgsql 22164 22160 0 14:12 ? 00:00:00 postgres: checkpointer
pgsql 22165 22160 0 14:12 ? 00:00:00 postgres: background writer
pgsql 22166 22160 0 14:12 ? 00:00:00 postgres: walwriter
pgsql 22167 22160 0 14:12 ? 00:00:00 postgres: autovacuum launcher
pgsql 22168 22160 0 14:12 ? 00:00:00 postgres: archiver
pgsql 22169 22160 0 14:12 ? 00:00:00 postgres: stats collector
pgsql 22170 22160 0 14:12 ? 00:00:00 postgres: logical replication launcher
pgsql 22172 22160 0 14:12 ? 00:00:00 postgres: walsender pgsql 192.168.56.125(40206) streaming 0/B0000D8
pgsql 22174 2756 0 14:12 pts/0 00:00:00 rlwrap psql -h 192.168.56.100 -p 5432 postgres pgsql
pgsql 22175 22174 0 14:12 pts/1 00:00:00 psql -h 192.168.56.100 -p 5432 postgres pgsql
pgsql 22180 22160 0 14:12 ? 00:00:00 postgres: pgsql postgres 192.168.56.100(64860) idle
root 22192 13050 0 14:12 pts/2 00:00:00 grep --color=auto post
select * from pg_settings where name in ('wal_level','archive_mode','archive_command');
--切换归档
select pg_switch_wal();
--查看归档状态
test=# select * from pg_stat_get_archiver();
-[ RECORD 1 ]------+-----------------------------------------
archived_count | 8
last_archived_wal | 000000010000000000000008.00000028.backup
last_archived_time | 2023-07-28 10:44:59.302381+08
failed_count | 0
last_failed_wal |
last_failed_time |
stats_reset | 2023-07-27 16:35:50.49841+08
--操作系统级别
[root@test archive]# pwd
/postgresql/archive
[root@test archive]#ll
-rw------- 1 pgsql pgsql 16777216 Jul 28 10:17 000000010000000000000001
-rw------- 1 pgsql pgsql 16777216 Jul 28 10:17 000000010000000000000002
-rw------- 1 pgsql pgsql 16777216 Jul 28 10:18 000000010000000000000003
-rw------- 1 pgsql pgsql 16777216 Jul 28 10:19 000000010000000000000004
-rw------- 1 pgsql pgsql 16777216 Jul 28 10:21 000000010000000000000005
-rw------- 1 pgsql pgsql 16777216 Jul 28 10:25 000000010000000000000006
-rw------- 1 pgsql pgsql 16777216 Jul 28 10:44 000000010000000000000007
-rw------- 1 pgsql pgsql 16777216 Jul 28 10:44 000000010000000000000008
-rw------- 1 pgsql pgsql 322 Jul 28 10:44 000000010000000000000008.00000028.backup
[root@test archive]# ps -ef|grep post
root 1485 1 0 08:57 ? 00:00:00 /usr/libexec/postfix/master -w
postfix 1502 1485 0 08:57 ? 00:00:00 qmgr -l -t unix -u
postfix 18200 1485 0 13:55 ? 00:00:00 pickup -l -t unix -u
pgsql 22160 1 0 14:12 ? 00:00:00 /postgresql/pg13/bin/postgres
pgsql 22162 22160 0 14:12 ? 00:00:00 postgres: logger
pgsql 22164 22160 0 14:12 ? 00:00:00 postgres: checkpointer
pgsql 22165 22160 0 14:12 ? 00:00:00 postgres: background writer
pgsql 22166 22160 0 14:12 ? 00:00:00 postgres: walwriter
pgsql 22167 22160 0 14:12 ? 00:00:00 postgres: autovacuum launcher
pgsql 22168 22160 0 14:12 ? 00:00:00 postgres: archiver last was 00000001000000000000000B
pgsql 22169 22160 0 14:12 ? 00:00:00 postgres: stats collector
pgsql 22170 22160 0 14:12 ? 00:00:00 postgres: logical replication launcher
pgsql 22172 22160 0 14:12 ? 00:00:00 postgres: walsender pgsql 192.168.56.125(40206) streaming 0/C000060
pgsql 22174 2756 0 14:12 pts/0 00:00:00 rlwrap psql -h 192.168.56.100 -p 5432 postgres pgsql
pgsql 22175 22174 0 14:12 pts/1 00:00:00 psql -h 192.168.56.100 -p 5432 postgres pgsql
pgsql 22180 22160 0 14:12 ? 00:00:00 postgres: pgsql postgres 192.168.56.100(64860) idle
root 22324 13050 0 14:15 pts/2 00:00:00 grep --color=auto post
--主库创建复制用户
create role repuser login encrypted password 'reppasswd' replication;
--创建备份路径
mkdir -p /bk
chown pgsql.pgsql /bk
-- 远程备份
su - postgres
pg_basebackup -h 192.168.56.100 -p 5432 -U pgsql -l bk20230728 -F p -P -R -D /bk
[root@pg ~]# ll /bk/
-rw-------. 1 pgsql pgsql 0 Jul 27 22:44 standby.signal
-- 关闭从库,删除从库的数据文件,并且将备份文件覆盖从库的数据文件
pg_ctl stop
mv /postgresql/pgdata /postgresql/20230728pgdata
mkdir /postgresql/pgdata
chmod 700 /postgresql/pgdata
cp -r /bk/* /postgresql/pgdata
--修改配置文件
cat >> /postgresql/pgdata/postgresql.conf <<"EOF"
wal_level='replica'
archive_mode='on'
archive_command='cp %p /postgresql/archive/%f'
restore_command='cp /postgresql/archive/%f %p'
max_wal_senders=10
wal_sender_timeout=60s
wal_keep_size=256
primary_conninfo = 'host=192.168.56.100 port=5432 user=repuser password=reppasswd'
EOF
--重启
pg_ctl stop
pg_ctl start
--主库查看wal日志发送状态
postgres=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid | 22172
usesysid | 16384
usename | pgsql
application_name | walreceiver
client_addr | 192.168.56.125
client_hostname |
client_port | 40206
backend_start | 2023-07-28 14:12:26.157227+08
backend_xmin |
state | streaming
sent_lsn | 0/C000148
write_lsn | 0/C000148
flush_lsn | 0/C000148
replay_lsn | 0/C000148
write_lag |
flush_lag |
replay_lag |
sync_priority | 0
sync_state | async
reply_time | 2023-07-28 14:23:09.779059+08
--从库查看wal日志接收状态
test=# select * from pg_stat_wal_receiver;
-[ RECORD 1 ]---------+------------------------------------------------------------------------------------------------------------
pid | 2106
status | streaming
receive_start_lsn | 0/B000000
receive_start_tli | 1
written_lsn | 0/C000148
flushed_lsn | 0/C000148
received_tli | 1
last_msg_send_time | 2023-07-28 14:25:29.789323+08
last_msg_receipt_time | 2023-07-28 14:25:29.7901+08
latest_end_lsn | 0/C000148
latest_end_time | 2023-07-28 14:17:29.470132+08
slot_name |
sender_host | 192.168.56.100
sender_port | 5432
conninfo | user=pgsql password=******** channel_binding=disable dbname=replication host=192.168.56.100 port=5432 fallback_application_name=walreceiver sslmode=disable sslcompression=0 ssl_min_protocol_version=TLSv1.2 gssencmode=disable krbsrvname=postgres target_session_attrs=any
--主
[pgsql@test ~]$ /postgresql/pg13/bin/pg_controldata | grep state
Database cluster state: in production
--备
[pgsql@pg pgdata]$ /postgresql/pg13/bin/pg_controldata | grep state
Database cluster state: in archive recovery
--主
postgres=# select pg_is_in_recovery();
-[ RECORD 1 ]-----+--
pg_is_in_recovery | f
--备
test=# select pg_is_in_recovery();
-[ RECORD 1 ]-----+--
pg_is_in_recovery | t
至此,standby部署完成