postgres=# show archive_mode;
archive_mode
--------------
off
(1 row)
2.开启归档模式
## 创建归档目录
mkdir -p /pgsql15.4/pg_arch
## 配置归档相关参数
postgres=# alter system set archive_mode=on;
ALTER SYSTEM
postgres=# alter system set archive_command ='cp %p /pgsql15.4/pg_arch/%f';
ALTER SYSTEM
注意:%p:表示要归档的文件路径;%f:代表不包含路径信息的wal文件的文件名。
## 重启生效
pg_ctl restart
3.执行wal日志切换,检查是否生成归档
postgres=# select * from pg_ls_waldir() order by modification asc;
name | size | modification
--------------------------+----------+------------------------
000000010000000000000001 | 16777216 | 2023-11-14 10:57:44+08
(1 row)
postgres=# checkpoint;
CHECKPOINT
postgres=# select pg_switch_wal();
pg_switch_wal
---------------
0/1518E68
(1 row)
postgres=# select * from pg_ls_waldir() order by modification asc;
name | size | modification
--------------------------+----------+------------------------
000000010000000000000003 | 16777216 | 2023-11-14 10:57:44+08
000000010000000000000002 | 16777216 | 2023-11-14 11:02:41+08
(2 rows)
[postgres@pg pg_arch]$ ll
total 16384
-rw-------. 1 postgres postgres 16777216 Nov 14 10:57 000000010000000000000001
注意:开启归档后,会将 WAL 日志复制到归档目录,然后被清理掉。