Enable Archive Mode in PostgreSQL 17
Table of Contents
0. Importance of Archive Log Files
1. Verify Existing Archive Mode
2. Ensure Archive Directory Exists
3. Edit postgresql.conf
4. Restart PostgreSQL
5. Verify the Configuration
6. Test Archiving Works
0. Importance of Archive Log Files
Archive log files, also known as WAL archives (Write-Ahead Log archives) , play a critical role in PostgreSQL for data protection, recovery, and replication.
A. Point-in-Time Recovery (PITR): Restore the database to a specific point in time, useful for accidental data changes or loss.
B. Continuous Backup: Works with base backups to enable robust disaster recovery.
C. Streaming Replication Support: Helps standby servers catch up if they fall behind.
D. Disaster Recovery: Enables full recovery after hardware or data corruption.
E. Data Audit & Analysis: Allows decoding WAL logs for auditing and compliance.
🛑 Important Note:
If WAL archiving is disabled and a backup is taken, you can only restore to the exact backup time, not to any point after.
1. Verify Existing Archive Mode
[postgres@lxicbpgdsgv01 ~]$ psql
psql (17.6)
Type "help" for help.
postgres=# SHOW archive_mode;
archive_mode
--------------
off <--- it's off
(1 row)
postgres=# SHOW archive_command;
archive_command
-----------------
(disabled) <----
(1 row)
[root@lxicbpgdsgv01 ~]# systemctl status postgresql-17
● postgresql-17.service - PostgreSQL 17 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-17.service; enabled; preset: disabled)
Active: active (running) since Tue 2025-10-07 18:33:05 +08; 2min 26s ago
Docs: https://www.postgresql.org/docs/17/static/
Process: 5911 ExecStartPre=/usr/pgsql-17/bin/postgresql-17-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 5916 (postgres)
Tasks: 7 (limit: 15700)
Memory: 21.3M
CPU: 177ms
CGroup: /system.slice/postgresql-17.service
├─5916 /usr/pgsql-17/bin/postgres -D /pgData/pgsql17/data/
├─5917 "postgres: logger "
├─5918 "postgres: checkpointer "
├─5919 "postgres: background writer "
├─5921 "postgres: walwriter "
├─5922 "postgres: autovacuum launcher "
└─5923 "postgres: logical replication launcher "
Oct 07 18:33:05 lxicbpgdsgv01.rajasekhar.com systemd[1]: Starting PostgreSQL 17 database server...
Oct 07 18:33:05 lxicbpgdsgv01.rajasekhar.com postgres[5916]: 2025-10-07 18:33:05.392 +08 [5916] LOG: redirecting log output to logging collector process
Oct 07 18:33:05 lxicbpgdsgv01.rajasekhar.com postgres[5916]: 2025-10-07 18:33:05.392 +08 [5916] HINT: Future log output will appear in directory "log".
Oct 07 18:33:05 lxicbpgdsgv01.rajasekhar.com systemd[1]: Started PostgreSQL 17 database server.
[root@lxicbpgdsgv01 ~]#
Currently, there is no active archive process running in the background.
2. Ensure Archive Directory Exists
[root@lxicbpgdsgv01 ~]# mkdir -p /pgArch/pgsql17/arch/
[root@lxicbpgdsgv01 ~]# chown postgres:postgres /pgArch/pgsql17/arch/
[root@lxicbpgdsgv01 ~]# chmod 700 /pgArch/pgsql17/arch/
3. Edit postgresql.conf
[postgres@lxicbpgdsgv01 ~]$ vi /pgData/pgsql17/data/postgresql.conf
Update or add the following lines:
archive_mode = on
archive_command = 'cp %p /pgArch/pgsql17/arch/%f'
Explanation:
%p
= Full path of WAL file%f
= WAL file namecp %p /pgArch/pgsql17/arch/%f
= Command to copy the WAL file to archive directory
4. Restart PostgreSQL
[root@lxicbpgdsgv01 ~]# systemctl status postgresql-17
● postgresql-17.service - PostgreSQL 17 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-17.service; enabled; preset: disabled)
Active: active (running) since Tue 2025-10-07 23:48:29 +08; 18s ago
Docs: https://www.postgresql.org/docs/17/static/
Process: 7868 ExecStartPre=/usr/pgsql-17/bin/postgresql-17-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 7873 (postgres)
Tasks: 8 (limit: 15700)
Memory: 19.0M
CPU: 96ms
CGroup: /system.slice/postgresql-17.service
├─7873 /usr/pgsql-17/bin/postgres -D /pgData/pgsql17/data/
├─7874 "postgres: logger "
├─7875 "postgres: checkpointer "
├─7876 "postgres: background writer "
├─7878 "postgres: walwriter "
├─7879 "postgres: autovacuum launcher "
├─7880 "postgres: archiver " <------
└─7881 "postgres: logical replication launcher "
Oct 07 23:48:29 lxicbpgdsgv01.rajasekhar.com systemd[1]: Starting PostgreSQL 17 database server...
Oct 07 23:48:29 lxicbpgdsgv01.rajasekhar.com postgres[7873]: 2025-10-07 23:48:29.503 +08 [7873] LOG: redirecting log output to logging collector process
Oct 07 23:48:29 lxicbpgdsgv01.rajasekhar.com postgres[7873]: 2025-10-07 23:48:29.503 +08 [7873] HINT: Future log output will appear in directory "log".
Oct 07 23:48:29 lxicbpgdsgv01.rajasekhar.com systemd[1]: Started PostgreSQL 17 database server.
[root@lxicbpgdsgv01 ~]#
Make sure the archiver process is running.
5. Verify the Configuration
[postgres@lxicbpgdsgv01 ~]$ psql -c "SHOW archive_mode;"
archive_mode
--------------
on <-----------
(1 row)
[postgres@lxicbpgdsgv01 ~]$ psql -c "SHOW archive_command;"
archive_command
-------------------------------
cp %p /pgArch/pgsql17/arch/%f <-----------------
(1 row)
6. Test Archiving Works
[postgres@lxicbpgdsgv01 ~]$ psql -c "SELECT pg_switch_wal();"
Check archive folder for new WAL files:
[postgres@lxicbpgdsgv01 ~]$ ls -lrth /pgArch/pgsql17/arch/
total 16M
-rw-------. 1 postgres postgres 16M Oct 7 23:50 000000010000000000000001
[postgres@lxicbpgdsgv01 ~]$ psql -c "SELECT pg_switch_wal();"
[postgres@lxicbpgdsgv01 ~]$ ls -lrth /pgArch/pgsql17/arch/
total 32M
-rw-------. 1 postgres postgres 16M Oct 7 23:50 000000010000000000000001
-rw-------. 1 postgres postgres 16M Oct 7 23:51 000000010000000000000002
Archiving is working successfully.
Caution: Your use of any information or materials on this website is entirely at your own risk. It is provided for educational purposes only. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.
Thank you,
Rajasekhar Amudala
Email: br8dba@gmail.com
Linkedin: https://www.linkedin.com/in/rajasekhar-amudala/