开启PG的归档模式

开启PG的归档模式

1.查看pg的数据目录

postgres=# SHOW data_directory;
     data_directory     
------------------------
 /var/lib/pgsql/12/data
(1 row)

2.查看pg的归档情况

postgres=# show archive_mode;
 archive_mode 
--------------
 off
(1 行记录)

可以看到,归档模式为OFF。

3.查看归档的模式和位置

rpm安装的pg,配置文件在/var/lib/pgsql/12/data/postgresql.conf

[postgres@localhost data]$ cat postgresql.conf |grep archive_mode
#archive_mode = off             # enables archiving; off, on, or always
[postgres@localhost data]$ cat postgresql.conf |grep archive_command
#archive_command = ''           # command to use to archive a logfile segment

4.建立归档目录

[postgres@localhost ~]$ cd /var/lib/pgsql/12/
[postgres@localhost 12]$ ls -l
总用量 8
drwx------ 20 postgres postgres 4096 6月  17 16:21 data
-rw-------  1 postgres postgres  997 6月  17 16:21 initdb.log
[postgres@localhost 12]$ mkdir arch

5.配置归档参数

vi /var/lib/pgsql/12/data/postgresql.conf

# - Archiving -

archive_mode = on              # enables archiving; off, on, or always
                                # (change requires restart)
archive_command = 'DATE=`date +%Y%m%d`;DIR="/var/lib/pgsql/12/arch/$DATE";(test -d $DIR || mkdir -p $DIR)&& cp %p $DIR/%f'             # command to use to archive a logfile segment

6.重启pg

systemctl stop postgresql-12
systemctl start postgresql-12 

7.查看&&切换归档日志

[postgres@localhost ~]$ psql
psql (12.3)
输入 "help" 来获取帮助信息.

postgres=# show archive_mode;
 archive_mode 
--------------
 on
(1 行记录)

postgres=# select pg_switch_wal();
 pg_switch_wal 
---------------
 0/169E208
(1 行记录)

8.查看目录位置的归档文件

[postgres@localhost ~]$ ls -l /var/lib/pgsql/12/arch/
总用量 4
drwx------ 2 postgres postgres 4096 6月  17 16:55 20200617


[postgres@localhost 20200617]$ ls -l 
总用量 32768
-rw------- 1 postgres postgres 16777216 6月  17 16:55 000000010000000000000001
-rw------- 1 postgres postgres 16777216 6月  17 16:57 000000010000000000000002
原文地址:https://www.cnblogs.com/zhangshengdong/p/13153422.html