Enable or disable WAL archive in PostgreSQL

Enable or Disable the archive mode in PostgreSQL

Archiving is the process of generating the backup of all transactions in a file that occurred in the database. so that with help of that archive file, you can recover database to any point in time or establish replication.

Transaction happened in the PostgreSQL database write transaction into WAL file like redo log file before it written to disk. Copy of these WAL file is calling archiving in PostgreSQL.

Note: WAL files are stored in $PGDATA/pg_wal in postgresql version 10 and higher. For older version its stored at $PGDATA/pg_xlog.

Check archive mode is enabled or Disabled

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

OR 

postgres=# select name,setting from pg_settings where name like 'archive_mode%';
          name           |                setting
-------------------------+---------------------------------------
 archive_mode            | off

Enable the archive mode WAL in PostgreSQL:

--Enable at Windows:
mkdir C:\TESTWORK\archive
--Connect to the PostgreSQL and enable the archive mode:
postgres=# ALTER SYSTEM SET archive_mode to 'ON';
--Set the archive command to copy the WAL files to archive location:
postgres=# ALTER SYSTEM SET archive_command TO 'copy %p C:\testwork\archive\archive%f';


--Enable at Linux:
mkdir -p /scratch/archive
bash-4.1$ chown postgres:postgres -R /scratch/archive
ALTER SYSTEM SET archive_mode to 'ON';
ALTER SYSTEM SET archive_command TO 'cp %p /scratch/archive/archive%f';

Note: Restart of Server will effect these parameter changes.

--Restart in Windows: 
Open Services.msc and restart the postgreSQL Server service.

-- Restart in Linux
/etc/init.d/postgresql restart

Validate the Archive is enabled or disabled:

postgres=# select name,setting from pg_settings where name like 'archive%';
          name           |                setting
-------------------------+---------------------------------------
 archive_cleanup_command |
 archive_command         | copy %p C:\testwork\archive\archive%f
 archive_library         |
 archive_mode            | on
 archive_timeout         | 0

Disable the Archive in PostgreSQL

 alter system set archive_mode=off;

-- Restart the Service 

Note: For manually switch archive wal log file:

select pg_switch_wal();
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.