PostgreSQL归档配置及自动清理归档日志

在通常的生产环境中,数据库都须要开启归档模式,那么在pg中如何开启归档模式呢?web

pg中的归档配置涉及几个参数以下:数据库

# - Archiving - 
  
是否开启归档  
#archive_mode = off # enables archiving; off, on, or always 
                                # (change requires restart) 
  
归档命令,注意 %p  %f  %% 格式化的含义。  
%p 是被归档的redo文件的路径,    
%f 是被归档的redo文档的文件名     
%% 是百分号  
#archive_command = '' # command to use to archive a logfile segment 
                                # placeholders: %p = path of file to archive 
                                # %f = file name only 
                                # e.g. 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f' 
  
超时强制归档,例:若是10分钟数据库都没有什么活动,一个redo文件没有写完,就不会归档,  
可是咱们但愿数据库至少10分钟要切换一个日志,则能够使用archive_timeout  
#archive_timeout = 0 # force a logfile segment switch after this 
                                # number of seconds; 0 disables 

–归档配置方法为:
一、建立归档目录
pg12@oracle-> mkdir -p $PGDATA/archive/bash

二、编辑归档脚本
该脚本还能够删除7天内的归档日志。
pg12@oracle-> vi $PGDATA/arch.shoracle

test ! -f $PGDATA/arch/$1 && cp --preserve=timestamps $2 $PGDATA/arch/$1 ; find /arch/ -type f -mtime +7 -exec rm -f {} \;

三、配置归档相关参数svg

wal_level = replica  
archive_mode = on  
archive_command = 'arch.sh %f %p'

配置完以后重启数据库服务便可。
pg12@oracle-> ps -ef|grep archiver
pg12 21338 21331 0 20:20 ? 00:00:00 postgres: archiverpost