暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

PG开启归档步骤

原创 不为什么 2023-11-07
568

1、查询数据库的安装位置和归档时否开启
step 1:查看数据目录(找出conf文件位置)
postgres=# show data_directory;
   data_directory   
--------------------
/postgresql/pgdata
(1 row)

step 2:查看归档是否开启
postgres=# show archive_mode;
archive_mode
--------------
off
(1 row)

2、开启归档模式(开启后有一定的性能损耗)
step 1:创建归档目录
mkdir -p /postgresql/archive

step 2:修改目录所有者
chown postgres.postgres /postgresql/archive

step 3:修改postgresql.conf配置文件
archive_mode = on #开启归档

archive_command = 'DATE=date +%Y%m%d;

DIR="/postgresql/archive/$DATE";(test -d $DIR || mkdir -p $DIR)&& cp %p $DIR/%f' 

#配置归档命令,%p : 表示将要归档的wal文件包含完整路径的信息的文件名,%f : 代表不包含路径信息的wal文件的文件名

step 3:重启pg
3、验证归档情况
step 1:查看归档是否开启
postgres=# show archive_mode;
archive_mode
--------------
on
(1 row)

postgres=# select name,setting from pg_settings where name like 'archive%';
          name           |                                              setting                                             
-------------------------+---------------------------------------------------------------------------------------------------
archive_cleanup_command |
archive_command         | DATE=$(date +%Y%m%d);DIR="/postgresql/archive/$DATE";(test -d $DIR || mkdir -p $DIR)&& cp %p $DIR/%f
archive_mode            | on
archive_timeout         | 1800
(4 rows)



step 2:检查点 , 刷新脏数据
checkpoint

OK
时间: 0.004s

step 3:查看归档情况
select pg_switch_wal();
postgres=# select pg_switch_wal();
pg_switch_wal
---------------
0/17287E70
(1 row)


step 4:查看归档文件
[postgres@webserver ~]$ cd /postgresql/archive/

[postgres@webserver archive]$ ls -ltr
总用量 65536
-rw------- 1 postgres postgres 16777216 5月  19 05:57 000000010000000000000017
-rw------- 1 postgres postgres 16777216 5月  19 05:59 000000010000000000000018
-rw------- 1 postgres postgres 16777216 5月  19 05:59 000000010000000000000019
-rw------- 1 postgres postgres 16777216 5月  19 06:20 00000001000000000000001A
drwx------ 2 postgres postgres       70 5月  19 06:21 20230519
[postgres@webserver archive]$ ls -ltr 20230519/
总用量 32768
-rw------- 1 postgres postgres 16777216 5月  19 06:21 00000001000000000000001B
-rw------- 1 postgres postgres 16777216 5月  19 06:21 00000001000000000000001C
[postgres@webserver archive]$
[postgres@webserver archive]$ ls -ltr /postgresql/pgdata/pg_wal/
总用量 147456
-rw------- 1 postgres postgres 16777216 5月  19 02:42 00000001000000000000001B
-rw------- 1 postgres postgres 16777216 5月  19 02:42 00000001000000000000001C
-rw------- 1 postgres postgres 16777216 5月  19 02:42 00000001000000000000001D
-rw------- 1 postgres postgres 16777216 5月  19 02:42 00000001000000000000001E
-rw------- 1 postgres postgres 16777216 5月  19 02:42 00000001000000000000001F
-rw------- 1 postgres postgres 16777216 5月  19 05:57 000000010000000000000017
-rw------- 1 postgres postgres 16777216 5月  19 05:59 000000010000000000000018
-rw------- 1 postgres postgres 16777216 5月  19 05:59 000000010000000000000019
drwx------ 2 postgres postgres      117 5月  19 05:59 archive_status
-rw------- 1 postgres postgres 16777216 5月  19 05:59 00000001000000000000001A
[postgres@webserver archive]$ ls -ltr /postgresql/pgdata/pg_wal/archive_status/
总用量 0
-rw------- 1 postgres postgres 0 5月  19 05:57 000000010000000000000017.done
-rw------- 1 postgres postgres 0 5月  19 05:59 000000010000000000000018.done
-rw------- 1 postgres postgres 0 5月  19 05:59 000000010000000000000019.done
[postgres@webserver archive]$

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论