pg归档日志的开启与关闭

来源:互联网 发布:医学论文数据库 编辑:程序博客网 时间:2024/05/07 23:05
postgres.conf文件的配置对所有数据库起作用:
postgres=# \l
                                        List of databases
   Name    |    Owner    | Encoding |   Collate   |    Ctype    |        Access privileges        
-----------+-------------+----------+-------------+-------------+---------------------------------
 postgres  | postgres9.6 | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres9.6 | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/"postgres9.6"               +
           |             |          |             |             | "postgres9.6"=CTc/"postgres9.6"
 template1 | postgres9.6 | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/"postgres9.6"               +
           |             |          |             |             | "postgres9.6"=CTc/"postgres9.6"
 test      | postgres9.6 | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(4 rows)


postgres=# \c
You are now connected to database "postgres" as user "postgres9.6".
postgres=# show archive_mode;
 archive_mode 
--------------
 on
(1 row)


postgres=# select pg_switch_xlog();
 pg_switch_xlog 
----------------
 0/C000078
(1 row)






test=# \c
You are now connected to database "test" as user "postgres9.6".
test=# select pg_switch_xlog();
 pg_switch_xlog 
----------------
 0/B02A090
(1 row)


[root@db 20170206]# pwd
/home/postgres9.6/arch/20170206
[root@db 20170206]# ls
00000001000000000000000A  00000001000000000000000B  00000001000000000000000C


============================================================================================================================


开启归档模式:
[postgres9.6@db data]$ pwd
/home/postgres9.6/data
[postgres9.6@db data]$ ll postgresql.conf 
-rw------- 1 postgres9.6 postgres 22369 Feb  6 13:35 postgresql.conf


vi postgres.conf修改如下:
wal_level = replica                     # minimal, replica, or logical
archive_mode = on               # enables archiving; off, on, or always
                                # (change requires restart)
archive_command = 'DATE=`date +%Y%m%d`;DIR="/home/postgres9.6/arch/$DATE";(test -d $DIR || mkdir -p $DIR)&& cp %p $DIR/%f'              # command to use to archive a logfile segment
其中: %p表示wal日志文件的路径,%f表示wal日志文件名称。




[postgres9.6@db data]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
[postgres9.6@db data]$ pg_ctl start
server starting
[postgres9.6@db data]$ LOG:  database system was shut down at 2017-02-06 13:40:26 CST
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started




postgres=# \c
You are now connected to database "postgres" as user "postgres9.6".
postgres=# select pg_switch_xlog();
 pg_switch_xlog 
----------------
 0/F000120
(1 row)


postgres=# select pg_switch_xlog();
 pg_switch_xlog 
----------------
 0/10000078
(1 row)


postgres=#  show archive_mode;
 archive_mode 
--------------
 on
(1 row)






test=# \c
You are now connected to database "test" as user "postgres9.6".
test=# select pg_switch_xlog();
 pg_switch_xlog 
----------------
 0/11000078
(1 row)


test=# show archive_mode;
 archive_mode 
--------------
 on
(1 row)


[root@db 20170206]# pwd
/home/postgres9.6/arch/20170206
[root@db 20170206]# ls
00000001000000000000000F  000000010000000000000010  000000010000000000000011


##### pg里不同数据库之间日志(归档日志或者pc_xlog)(目录)是共享的。####


关闭归档模式:
将以上三个参数注释或者修改即可