In this article
- What you will learn
- Prerequisites
- How it works
- Prepare the archive location
- Turn on archiving
- Prove that WAL files reach the archive
- Monitor archiving and handle failures
- Base backups and archive cleanup
- Alternative: archive_library with basic_archive
- Common mistakes and how to fix them
- Practice exercise
- Quick reference
- Summary and what's next
This is Part 19 of the PostgreSQL DBA series. WAL archiving means PostgreSQL copies every finished write-ahead log (WAL) file to a safe place outside the data directory, using the command you put in archive_command. You turn it on with three settings: wal_level = replica, archive_mode = on and an archive_command (or an archive_library), followed by one restart. By the end of this part you will have archiving running on PostgreSQL 18, you will know how to prove it works, how to spot a failing archive before it fills your disk, and how to clean up old archived files safely.
In Part 18 we covered physical backups with pg_basebackup. A base backup on its own only lets you go back to the moment it was taken. Add a continuous archive of WAL files and you can replay changes up to any later point in time, which is what Part 20 (point-in-time recovery) builds on.
What you will learn
- What WAL archiving is and why a base backup alone is not enough
- How to set
archive_mode,archive_commandandarchive_timeout, and which of them need a restart - How to prove WAL files reach the archive with
pg_switch_wal(),pg_stat_archiverand thearchive_statusfolder - What a failing archive looks like in the log, and how to fix it
- How to remove old archived WAL safely with
pg_archivecleanup - How to use the
basic_archivemodule instead of a shell command
Prerequisites
- Parts 1 to 18 of this series, especially Part 5 (postgresql.conf, reload vs restart), Part 15 (WAL basics) and Part 18 (pg_basebackup).
- One Ubuntu server (we call it db1) with PostgreSQL 18 from the PGDG repository and the default cluster
18 mainrunning on port 5432. - A user with
sudorights. Database commands run as thepostgresoperating system user. - A place for the archive. In this lab it is the folder
/mnt/wal_archive. In production it must be on a different disk, and ideally a different machine, from the database.
How it works
In Part 15 you met WAL: the notebook PostgreSQL writes every change into before it touches the real data files. That notebook is split into files called WAL segments, 16 MB each by default, with names like 000000010000000000000003. The name grows by one for every new segment.
Without archiving, PostgreSQL keeps only the segments it still needs for crash recovery. After a checkpoint, older segments are deleted or renamed and reused. That is fine for crash recovery, but it means the history of changes is lost.
With archiving on, a background process called the archiver copies each segment somewhere safe as soon as the segment is full, before PostgreSQL is allowed to recycle it. Think of it as photocopying each page of the notebook before the page is torn out and reused.
primary db1 (10.0.0.11:5432)
pg_wal/000000010000000000000003 (full segment)
pg_wal/archive_status/...03.ready
|
| archiver runs archive_command
v
archive storage: /mnt/wal_archive/000000010000000000000003
|
| archiver sees exit status 0
v
pg_wal/archive_status/...03.done -> segment may now be recycledThe steps behind that picture:
- When a segment is complete, PostgreSQL writes an empty marker file
<segment>.readyinpg_wal/archive_status/. - The archiver runs your
archive_command. In the command,%pis replaced by the path of the file to archive (relative to the data directory, for examplepg_wal/000000010000000000000003) and%fby the file name alone. Use%%for a real percent sign. - If the command exits with status 0, PostgreSQL treats the file as archived and renames the marker to
.done. Only then can the segment be removed or recycled. - If the command exits with any other status, the file stays
.readyand PostgreSQL tries again later. The segment is kept inpg_waluntil it succeeds.
That last point is the whole safety contract: your command must return 0 only when the file really is safely stored. A command that always returns 0 makes PostgreSQL throw away WAL it never saved. A command that keeps failing makes WAL pile up in pg_wal until the disk fills.
An archive is only useful together with a base backup. The base backup is the starting snapshot of the data files. The archived WAL is every change made after it. Recovery restores the snapshot and replays the WAL on top.
Prepare the archive location
Step 1: Check the current settings
On db1, as the postgres user, open psql and look at the settings that control archiving. The context column tells you how a change is applied: postmaster means a restart is needed, sighup means a reload is enough.
sudo -u postgres psqlSELECT name, setting, context
FROM pg_settings
WHERE name IN ('wal_level', 'archive_mode', 'archive_command',
'archive_library', 'archive_timeout')
ORDER BY name; name | setting | context
-----------------+------------+------------
archive_command | (disabled) | sighup
archive_library | | sighup
archive_mode | off | postmaster
archive_timeout | 0 | sighup
wal_level | replica | postmaster
(5 rows)wal_level is already replica, the default, which is the minimum archiving needs. archive_mode is off, so archive_command shows (disabled). archive_mode has context postmaster, so turning it on will need a restart. The other three can be changed with a reload.
Check: if wal_level shows minimal on your server, set it to replica in the same edit as Step 4. Archiving cannot be enabled with minimal.
Step 2: Create the archive directory
On db1, as your sudo user, create the folder and give it to postgres. The archive command runs as the same operating system user as the server, so postgres must be able to write here. The documentation also advises that the archive should not be readable by other users, because WAL contains every change to your data.
sudo mkdir -p /mnt/wal_archive
sudo chown postgres:postgres /mnt/wal_archive
sudo chmod 700 /mnt/wal_archive
ls -ld /mnt/wal_archive| Command | What it does |
|---|---|
| mkdir -p | Creates the folder, and any missing parent folders |
| chown postgres:postgres | Makes the postgres user and group the owner |
| chmod 700 | Only the owner can read, write or enter the folder |
drwx------ 2 postgres postgres 4096 Nov 16 12:48 /mnt/wal_archivedrwx------ and the owner postgres are what you want. In production, mount a separate disk or a network share from your backup host at this path. If the archive sits on the same disk as the database, one disk failure takes both.
Step 3: Test the archive command by hand
Before PostgreSQL uses a command, run it yourself as postgres with a dummy file. We use the command from the PostgreSQL documentation: test ! -f /mnt/wal_archive/%f && cp %p /mnt/wal_archive/%f. The test ! -f part refuses to overwrite a file that already exists in the archive, so the command fails instead of silently replacing archived WAL.
sudo -u postgres bash -c 'cd /tmp
echo hello > testwal
test ! -f /mnt/wal_archive/testwal && cp testwal /mnt/wal_archive/testwal
echo "exit status: $?"
test ! -f /mnt/wal_archive/testwal && cp testwal /mnt/wal_archive/testwal
echo "exit status: $?"
rm /mnt/wal_archive/testwal testwal'| Part | What it does |
|---|---|
| test ! -f FILE | Succeeds only if FILE does not exist yet |
| && | Runs cp only if the test succeeded |
| cp SOURCE TARGET | Copies the file; returns a non-zero status if it cannot |
| echo $? | Prints the exit status of the previous command |
exit status: 0
exit status: 1The first copy succeeds with status 0. The second attempt finds the file already there and returns 1. That is exactly the behaviour PostgreSQL relies on. The last line removes the test files.
Turn on archiving
Step 4: Edit postgresql.conf
On db1, as your sudo user, edit /etc/postgresql/18/main/postgresql.conf. The settings are in the Archiving section and are commented out by default.
sudo nano /etc/postgresql/18/main/postgresql.conf# /etc/postgresql/18/main/postgresql.conf (before)
#archive_mode = off
#archive_command = ''
#archive_timeout = 0# /etc/postgresql/18/main/postgresql.conf (after)
archive_mode = on
archive_command = 'test ! -f /mnt/wal_archive/%f && cp %p /mnt/wal_archive/%f'
archive_timeout = 60| Setting | Default | Meaning | Applied by |
|---|---|---|---|
| archive_mode | off | on: archive completed segments. always: also archive on a standby | restart |
| archive_command | '' (empty) | Shell command run for each completed segment; %p = path, %f = file name | reload |
| archive_timeout | 0 (off) | Force a switch to a new segment after this many seconds, if there was any activity | reload |
| wal_level | replica | Must be replica or logical for archiving | restart |
A note on archive_mode = always: on a primary it behaves like on. On a standby server, always also archives every WAL file the standby receives, while on archives nothing until the standby is promoted. You will use this in the replication parts.
Why 60 seconds? Without archive_timeout, a quiet server might take hours to fill a 16 MB segment, and the changes in it are not in the archive until then. The documentation says a minute or so is usually reasonable. Every forced switch still produces a full 16 MB file, so a very small value wastes archive space.
Step 5: Restart PostgreSQL and verify
On db1, as your sudo user. archive_mode can only be set at server start, so a reload is not enough. Plan this restart for a quiet time, because it disconnects every session.
sudo systemctl restart postgresql@18-mainThe command prints nothing when it succeeds. Now check the settings as the postgres user:
SELECT name, setting, pending_restart
FROM pg_settings
WHERE name IN ('archive_mode', 'archive_command', 'archive_timeout')
ORDER BY name; name | setting | pending_restart
-----------------+------------------------------------------------------------+-----------------
archive_command | test ! -f /mnt/wal_archive/%f && cp %p /mnt/wal_archive/%f | f
archive_mode | on | f
archive_timeout | 60 | f
(3 rows)All three values are active and pending_restart is f (false) everywhere. Next, confirm that the archiver process exists:
ps -u postgres -o pid,cmd | grep archiver 882 postgres: 18/main: archiverThe 18/main in the process title is the cluster name. If no archiver line appears, archive_mode is still off: re-check the file and that you restarted rather than reloaded.
Prove that WAL files reach the archive
Step 6: Create some changes and force a WAL switch
On db1, as the postgres user in psql. We create a small database with an orders table so there is real WAL to archive, then call pg_switch_wal(). That function closes the current segment right away so it can be archived, instead of waiting for it to fill or for archive_timeout.
CREATE DATABASE salesdb;
\c salesdb
CREATE TABLE orders (
order_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer text NOT NULL,
amount numeric(10,2) NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
INSERT INTO orders (customer, amount)
SELECT 'customer_' || g, round((random() * 5000)::numeric, 2)
FROM generate_series(1, 50000) AS g;
SELECT pg_walfile_name(pg_current_wal_lsn());
SELECT pg_switch_wal();CREATE DATABASE
CREATE TABLE
INSERT 0 50000
pg_walfile_name
--------------------------
000000010000000000000002
(1 row)
pg_switch_wal
---------------
0/23AF720
(1 row)pg_current_wal_lsn() returns the current position in the WAL, and pg_walfile_name() turns that position into a segment file name. Before the switch we were writing into segment ...02. pg_switch_wal() returns the position just after the end of the segment it closed. If there was no WAL activity since the last switch, it does nothing.
Step 7: Check pg_stat_archiver and the archive folder
Still on db1 as postgres. pg_stat_archiver is a one-row view with the archiver's counters. \x on switches psql to expanded output, one column per line, which is easier to read on a narrow screen.
\x on
SELECT * FROM pg_stat_archiver;-[ RECORD 1 ]------+------------------------------
archived_count | 2
last_archived_wal | 000000010000000000000002
last_archived_time | 2026-11-16 12:49:09.690459+00
failed_count | 0
last_failed_wal |
last_failed_time |
stats_reset | 2026-11-16 12:48:21.554886+00Two files were archived and none failed. Segment ...01 had filled up during the insert, and ...02 was closed by pg_switch_wal(). Now look at the archive itself, from a shell on db1:
sudo -u postgres ls -l /mnt/wal_archivetotal 32768
-rw------- 1 postgres postgres 16777216 Nov 16 12:49 000000010000000000000001
-rw------- 1 postgres postgres 16777216 Nov 16 12:49 000000010000000000000002Both files are exactly 16,777,216 bytes (16 MB). A segment closed early by a switch is still a full-size file.
Step 8: Look at the archive_status markers
PostgreSQL tracks progress with the marker files described in How it works. You can list them from SQL with pg_ls_archive_statusdir(), which by default needs superuser or membership in the pg_monitor role.
\x off
SELECT name, size, modification
FROM pg_ls_archive_statusdir()
ORDER BY name; name | size | modification
-------------------------------+------+------------------------
000000010000000000000001.done | 0 | 2026-11-16 12:49:09+00
000000010000000000000002.done | 0 | 2026-11-16 12:49:09+00
(2 rows)Both markers end in .done: archived and safe to recycle. A file ending in .ready is waiting for the archiver. A few .ready files for a short time is normal on a busy server. A growing number means the archive cannot keep up or is failing.
Step 9: Watch archive_timeout at work
Insert a single row, which writes far less than 16 MB, and do not call pg_switch_wal(). Within a minute or two, archive_timeout should close the segment for you.
INSERT INTO orders (customer, amount) VALUES ('small_order', 99.00);
SELECT now()::time(0), pg_walfile_name(pg_current_wal_lsn());INSERT 0 1
now | pg_walfile_name
----------+--------------------------
13:01:30 | 00000001000000000000000A
(1 row)Wait about a minute, then run:
SELECT now()::time(0), last_archived_wal, last_archived_time::time(0)
FROM pg_stat_archiver; now | last_archived_wal | last_archived_time
----------+--------------------------+--------------------
13:02:10 | 00000001000000000000000A | 13:02:03
(1 row)The segment holding the single insert reached the archive at 13:02:03 without any manual switch. The timer counts from the last segment switch, not from your insert, so the exact wait varies; in another run on the same server it took about two minutes. On a quiet server, archive_timeout roughly limits how much recent work exists only on the database server and not yet in the archive.
Monitor archiving and handle failures
Step 10: A monitoring query for daily checks
On db1, as postgres or a monitoring role that is a member of pg_monitor. This query puts the most useful numbers on one line: how long since the last success, the last failed file, and how many files are waiting.
SELECT archived_count,
failed_count,
last_archived_wal,
now() - last_archived_time AS since_last_archive,
last_failed_wal,
(SELECT count(*) FROM pg_ls_archive_statusdir()
WHERE name LIKE '%.ready') AS waiting_files
FROM pg_stat_archiver;What to alert on:
waiting_fileskeeps growing over several checks. This is the most reliable sign of trouble.last_failed_timeis newer thanlast_archived_time: the latest attempt failed.since_last_archiveis much larger thanarchive_timeouton a server that is in use.
The documentation adds two cautions. First, files are usually archived oldest first, but not always (for example after crash recovery or promotion), so do not assume every file older than last_archived_wal is in the archive. Second, if the command is killed by a signal or the shell returns a status above 125 (such as "command not found"), the archiver process restarts and the failure is not counted in pg_stat_archiver. Watch the server log as well.
Step 11: Simulate a failure (lab only)
On db1, as your sudo user, remove write permission from the archive folder so cp fails, then force a switch from psql as postgres.
sudo chmod 500 /mnt/wal_archive\c salesdb
INSERT INTO orders (customer, amount)
SELECT 'late_' || g, 10 FROM generate_series(1, 1000) AS g;
SELECT pg_switch_wal();Now read the end of the server log. On Ubuntu each cluster logs to /var/log/postgresql/postgresql-18-main.log.
sudo tail -n 8 /var/log/postgresql/postgresql-18-main.log2026-11-16 12:49:22.860 UTC [882] DETAIL: The failed archive command was: test ! -f /mnt/wal_archive/000000010000000000000003 && cp pg_wal/000000010000000000000003 /mnt/wal_archive/000000010000000000000003
cp: cannot create regular file '/mnt/wal_archive/000000010000000000000003': Permission denied
2026-11-16 12:49:23.864 UTC [882] LOG: archive command failed with exit code 1
2026-11-16 12:49:23.864 UTC [882] DETAIL: The failed archive command was: test ! -f /mnt/wal_archive/000000010000000000000003 && cp pg_wal/000000010000000000000003 /mnt/wal_archive/000000010000000000000003
cp: cannot create regular file '/mnt/wal_archive/000000010000000000000003': Permission denied
2026-11-16 12:49:24.868 UTC [882] LOG: archive command failed with exit code 1
2026-11-16 12:49:24.868 UTC [882] DETAIL: The failed archive command was: test ! -f /mnt/wal_archive/000000010000000000000003 && cp pg_wal/000000010000000000000003 /mnt/wal_archive/000000010000000000000003
2026-11-16 12:49:24.868 UTC [882] WARNING: archiving write-ahead log file "000000010000000000000003" failed too many times, will try again laterThe archiver tried three times about a second apart, logged the exact command with %p and %f filled in, and the error from cp. Then it backed off and will try again later. The monitoring query from Step 10 now shows the problem:
archived_count | failed_count | last_archived_wal | since_last_archive | last_failed_wal | waiting_files
----------------+--------------+--------------------------+--------------------+--------------------------+---------------
2 | 3 | 000000010000000000000002 | 00:00:19.702188 | 000000010000000000000003 | 1
(1 row)The process title also changes, which is handy in ps or top:
882 postgres: 18/main: archiver failed on 000000010000000000000003Meanwhile, segment ...03 cannot be recycled. If this went on for days on a busy server, pg_wal would keep growing. The documentation is clear about the end point: if the file system holding pg_wal fills up, PostgreSQL does a PANIC shutdown. No committed transaction is lost, but the database stays down until you free space.
Step 12: Fix the cause and confirm archiving catches up
On db1, as your sudo user, restore the permission. You do not need to restart or reload PostgreSQL: the archiver retries on its own.
sudo chmod 700 /mnt/wal_archiveAfter about a minute, check again as postgres:
\x on
SELECT archived_count, last_archived_wal, last_archived_time,
failed_count, last_failed_wal
FROM pg_stat_archiver;-[ RECORD 1 ]------+------------------------------
archived_count | 3
last_archived_wal | 000000010000000000000003
last_archived_time | 2026-11-16 12:50:24.880962+00
failed_count | 3
last_failed_wal | 000000010000000000000003archived_count went up to 3 and the last archived file is the one that failed. failed_count stays at 3 because it is a running total; what matters is that last_archived_time is now later than the failure. The marker for ...03 has changed from .ready to .done.
Base backups and archive cleanup
Step 13: Take a base backup and find its backup history file
An archive grows forever unless you remove files. To know what is safe to remove, you need a base backup. On db1, as postgres, take one with pg_basebackup exactly as in Part 18 (here over the local socket, which the default Ubuntu pg_hba.conf allows for postgres).
sudo -u postgres mkdir -p /var/lib/postgresql/backups
sudo -u postgres pg_basebackup -D /var/lib/postgresql/backups/base_2026_11_16 \
--checkpoint=fast --label=base_2026_11_16 --progress| Option | What it does |
|---|---|
| -D directory | Directory to write the backup into; it must be empty or not exist |
| --checkpoint=fast | Request an immediate checkpoint instead of a spread one, so the backup starts at once |
| --label=label | Text label stored in the backup (default: "pg_basebackup base backup") |
| --progress | Show progress while the backup runs |
waiting for checkpoint
36251/36251 kB (100%), 0/1 tablespace
36251/36251 kB (100%), 1/1 tablespaceWith archiving enabled, the backup also writes a small backup history file into the archive. Its name is the first WAL segment the backup needs, plus a position and .backup:
sudo -u postgres ls -l /mnt/wal_archive
sudo -u postgres cat /mnt/wal_archive/000000010000000000000005.00000028.backuptotal 81924
-rw------- 1 postgres postgres 16777216 Nov 16 12:49 000000010000000000000001
-rw------- 1 postgres postgres 16777216 Nov 16 12:49 000000010000000000000002
-rw------- 1 postgres postgres 16777216 Nov 16 12:50 000000010000000000000003
-rw------- 1 postgres postgres 16777216 Nov 16 12:50 000000010000000000000004
-rw------- 1 postgres postgres 16777216 Nov 16 12:50 000000010000000000000005
-rw------- 1 postgres postgres 328 Nov 16 12:50 000000010000000000000005.00000028.backup
START WAL LOCATION: 0/5000028 (file 000000010000000000000005)
STOP WAL LOCATION: 0/5000120 (file 000000010000000000000005)
CHECKPOINT LOCATION: 0/5000080
BACKUP METHOD: streamed
BACKUP FROM: primary
START TIME: 2026-11-16 12:50:33 UTC
LABEL: base_2026_11_16
START TIMELINE: 1
STOP TIME: 2026-11-16 12:50:33 UTC
STOP TIMELINE: 1The file records the label, the start and stop time, and the WAL range of the backup. This backup starts at segment ...05. To restore it and replay forward, you need ...05 and every segment after it. Segments ...01 to ...04 are only useful to older base backups.
Step 14: Remove archived WAL older than a base backup
On db1, as postgres. pg_archivecleanup removes every WAL file in a folder that comes before a given file name. If you give it a .backup file name, it uses only the segment part, so this is a safe way to say "remove everything older than this base backup". On Ubuntu the command is on the PATH through postgresql-common.
sudo -u postgres pg_archivecleanup --dry-run /mnt/wal_archive \
000000010000000000000005.00000028.backup/mnt/wal_archive/000000010000000000000003
/mnt/wal_archive/000000010000000000000004
/mnt/wal_archive/000000010000000000000001
/mnt/wal_archive/000000010000000000000002The dry run only prints the files it would remove: segments 01 to 04, nothing from 05 onwards. Now run it for real with debug output:
sudo -u postgres pg_archivecleanup -d /mnt/wal_archive \
000000010000000000000005.00000028.backup
sudo -u postgres ls /mnt/wal_archive| Option | What it does |
|---|---|
| -n, --dry-run | Print the names of the files that would be removed, remove nothing |
| -d, --debug | Print debug logging output on stderr |
| -b, --clean-backup-history | Remove backup history files as well |
| -x ext, --strip-extension=ext | Strip an extension (for example .gz) from file names before deciding, for compressed archives |
pg_archivecleanup: keeping WAL file "/mnt/wal_archive/000000010000000000000005" and later
pg_archivecleanup: removing file "/mnt/wal_archive/000000010000000000000003"
pg_archivecleanup: removing file "/mnt/wal_archive/000000010000000000000004"
pg_archivecleanup: removing file "/mnt/wal_archive/000000010000000000000001"
pg_archivecleanup: removing file "/mnt/wal_archive/000000010000000000000002"
000000010000000000000005
000000010000000000000005.00000028.backupThe four older segments are gone. Segment 05 and the backup history file stay (history files are removed only with -b). In production you would run this from a script after each successful, verified base backup, pointing at the oldest backup you still keep. Part 21 shows how pgBackRest handles this retention for you.
Alternative: archive_library with basic_archive
Since PostgreSQL 15 you can archive with a loadable archive module instead of a shell command, set in archive_library. A module runs inside the archiver process instead of starting a shell for every file, which the documentation notes can perform better. PostgreSQL ships one example module, basic_archive, which copies each segment to a folder. It is mainly a starting point for writing your own, but it shows how the setting works. Production tools such as pgBackRest use archive_command.
Step 15: Switch to basic_archive (optional)
On db1, as your sudo user, create a target folder, then change /etc/postgresql/18/main/postgresql.conf. You must empty archive_command: the two settings cannot both be set.
sudo mkdir -p /mnt/wal_archive_lib
sudo chown postgres:postgres /mnt/wal_archive_lib
sudo chmod 700 /mnt/wal_archive_lib# /etc/postgresql/18/main/postgresql.conf (after)
archive_mode = on
archive_command = ''
archive_library = 'basic_archive'
basic_archive.archive_directory = '/mnt/wal_archive_lib'archive_library can be changed with a reload; the postmaster restarts the archiver process to load the new module. archive_mode is unchanged, so no restart is needed.
sudo systemctl reload postgresql@18-mainAfter the next segment switch, the file appears in /mnt/wal_archive_lib and the archiver shows it as before:
1168 postgres: 18/main: archiver last was 000000010000000000000007To go back, remove the two basic_archive lines, restore the archive_command from Step 4 and reload. The log confirms the change:
LOG: parameter "archive_library" removed from configuration file, reset to default
LOG: parameter "archive_command" changed to "test ! -f /mnt/wal_archive/%f && cp %p /mnt/wal_archive/%f"
LOG: restarting archiver process because value of "archive_library" was changedStep 16: How to turn archiving off (undo)
Skip this step if you are continuing with Part 20. If you need to switch archiving off completely, set archive_mode = off in /etc/postgresql/18/main/postgresql.conf and restart, because archive_mode only changes at server start.
sudo systemctl restart postgresql@18-main
sudo -u postgres psql -c 'SHOW archive_mode;' -c 'SHOW archive_command;' archive_mode
--------------
off
(1 row)
archive_command
-----------------
(disabled)
(1 row)Part 20 restores a base backup and replays the archive, so keep archiving on. If you tried Step 15, repeat Step 13 first so your newest base backup comes after the gap in /mnt/wal_archive. Set archive_mode = on again and restart if you ran this step. To pause archiving for a short time without a restart, set archive_command = '' and reload. WAL then collects in pg_wal until you put a working command back, so watch disk space.
Common mistakes and how to fix them
archive_mode changed but nothing happens
You edited archive_mode and ran SELECT pg_reload_conf();. The log says:
LOG: parameter "archive_mode" cannot be changed without restarting the server
LOG: configuration file "/etc/postgresql/18/main/postgresql.conf" contains errors; unaffected changes were appliedand pg_settings shows archive_mode | off | pending_restart = t. Fix: sudo systemctl restart postgresql@18-main.
LOG: archive command failed with exit code 1
The command ran but returned 1. The next log line shows the exact command and the error from cp: usually Permission denied (wrong owner or mode on the folder), No space left on device (archive disk full) or a mount that disappeared. Fix: run the command from the DETAIL line yourself as postgres, correct the cause, and watch pg_stat_archiver catch up as in Step 12. If the file already exists in the archive with different contents, find out why before removing anything: two servers may be writing to one folder.
FATAL: both "archive_command" and "archive_library" set
FATAL: both "archive_command" and "archive_library" set
DETAIL: Only one of "archive_command", "archive_library" may be set.The server keeps running, but the archiver exits and nothing is archived. Fix: set one of them to '' and reload.
FATAL: WAL archival cannot be enabled when "wal_level" is "minimal"
The server refuses to start. Fix: set wal_level = replica (and remove any max_wal_senders = 0 you added for minimal) and start again. Base backups taken while wal_level was minimal cannot be used for point-in-time recovery.
archive_command = '/bin/true' to "stop the WAL growing"
This makes PostgreSQL believe every file was archived and recycle it. The documentation warns that it breaks the chain of WAL files needed for recovery. Fix: fix the real archive problem, then take a new base backup, because the archive now has a gap.
Archive on the same disk as the database
It passes every test in this article and protects nothing when the disk fails. Fix: mount separate storage at the archive path, or move to a tool that sends WAL to another host (Part 21). Also note that plain cp does not force the copy to be flushed to disk before it returns, while the documentation expects an archived file to be fully persisted; dedicated backup tools take care of this.
Practice exercise
- Change
archive_timeoutto 120 seconds without restarting the server, and prove the new value is active. - Break archiving the same way as Step 11, then write a single query that returns the name of every WAL file still waiting to be archived. Fix the folder afterwards.
Quick reference
# 1. Check archiving settings (psql as postgres)
sudo -u postgres psql -c "SELECT name, setting, context FROM pg_settings WHERE name IN ('wal_level','archive_mode','archive_command','archive_library','archive_timeout');"
# 2. Create the archive directory
sudo mkdir -p /mnt/wal_archive
sudo chown postgres:postgres /mnt/wal_archive
sudo chmod 700 /mnt/wal_archive
# 4. Settings in /etc/postgresql/18/main/postgresql.conf
# archive_mode = on
# archive_command = 'test ! -f /mnt/wal_archive/%f && cp %p /mnt/wal_archive/%f'
# archive_timeout = 60
sudo nano /etc/postgresql/18/main/postgresql.conf
# 5. Restart (archive_mode needs it) and check the archiver process
sudo systemctl restart postgresql@18-main
ps -u postgres -o pid,cmd | grep archiver
# 6-7. Force a WAL switch and check the archive
sudo -u postgres psql -c 'SELECT pg_switch_wal();'
sudo -u postgres psql -x -c 'SELECT * FROM pg_stat_archiver;'
sudo -u postgres ls -l /mnt/wal_archive
# 8 and 10. Files waiting to be archived
sudo -u postgres psql -c "SELECT count(*) FROM pg_ls_archive_statusdir() WHERE name LIKE '%.ready';"
# 11. Read archiver errors in the log
sudo tail -n 20 /var/log/postgresql/postgresql-18-main.log
# 13. Base backup (writes a .backup history file into the archive)
sudo -u postgres pg_basebackup -D /var/lib/postgresql/backups/base_2026_11_16 --checkpoint=fast --label=base_2026_11_16 --progress
# 14. Remove archived WAL older than that backup (dry run first)
sudo -u postgres pg_archivecleanup --dry-run /mnt/wal_archive 000000010000000000000005.00000028.backup
sudo -u postgres pg_archivecleanup -d /mnt/wal_archive 000000010000000000000005.00000028.backup
# Reloadable settings (archive_command, archive_library, archive_timeout)
sudo systemctl reload postgresql@18-mainSummary and what's next
- WAL archiving copies each completed WAL segment out of
pg_walso changes after a base backup can be replayed - Turn it on with
wal_level = replica,archive_mode = on(restart) and anarchive_command(reload) - The command must return 0 only when the file is safely stored, and must refuse to overwrite an existing file
- Prove it works with
pg_switch_wal(),pg_stat_archiverand the archive folder - Alert on a growing number of
.readyfiles and onarchive command failedin the log archive_timeoutlimits how long a change can wait before it is archived; about a minute is reasonable- Clean up with
pg_archivecleanupusing the.backupfile of the oldest base backup you keep, dry run first
In Part 20 we use the base backup and WAL archive from this part to perform point-in-time recovery step by step.