Sub topic 7
Telling PostgreSQL how to fetch archived WAL
This chapter covers the recovery side of archiving: how PostgreSQL finds archived WAL segments during recovery, and how restore_command and recovery.signal control that. It assumes you can edit postgresql.conf, start and stop the server with systemctl, and run commands as the postgres user. It also assumes you have already set up archive_command and taken a base backup, as covered in earlier chapters.
Why recovery needs a fetch command
A base backup holds the data files as they were at one moment. Everything that happened afterwards lives in WAL segments, and those segments sit in your archive, not in the restored data directory. When PostgreSQL starts in recovery mode, it needs a way to copy each segment from the archive back to the server so it can replay it.
archive_command pushes segments out, and restore_command pulls them back. PostgreSQL does not know where your archive is or what storage it uses. It runs the command you give it, once per segment, and checks the exit status.
The restore_command setting
restore_command is a shell command. PostgreSQL substitutes two placeholders before running it:
| Placeholder | Meaning |
|---|---|
| `%f` | The file name of the WAL segment being requested, for example `000000010000000000000042` |
| `%p` | The path where PostgreSQL wants the file copied to, relative to the data directory |
| `%r` | The name of the file that contains the last valid restart point (used by some warm-standby tools) |
| `%%` | A literal percent sign |
The example from this tutorial is:
# recovery settings (PostgreSQL 12 and later)
restore_command = 'cp /mnt/archive/%f "%p"'
recovery_target_time = '2026-11-27 14:30:00+00'
recovery_target_action = 'promote'The command mirrors the archive_command from the archiving chapter. That one wrote to /mnt/archive/%f, so this one reads from the same place. The quotes around "%p" protect against spaces in paths. Because %p is a path relative to the data directory, the command must run with that directory as its working directory. PostgreSQL does this for you.
What the exit status means
PostgreSQL reads the exit status of restore_command carefully.
- Exit status 0 means the file was copied. PostgreSQL replays it and asks for the next one.
- A non-zero exit status means the file is not available. PostgreSQL treats this as the end of the archive, not necessarily as an error.
- Termination by a signal other than SIGTERM is treated as a fatal error and recovery aborts.
This is why a failed cp at the end of recovery is normal. After the last archived segment, PostgreSQL asks for the next segment, cp fails with "No such file or directory", and recovery concludes that it has replayed everything available. You will see lines like this in the log:
LOG: restored log file "000000010000000000000042" from archive
cp: cannot stat '/mnt/archive/000000010000000000000043': No such file or directory
LOG: redo done at 0/42001F40The cp error on the second line is expected. It is only a problem if it appears before you reach the target you wanted, because that means a segment is missing from the archive.
Segment names and what else gets requested
PostgreSQL may also ask for files that are not ordinary segments:
- Timeline history files, named like
00000002.history. These are requested to work out which timeline to follow. A missing history file is normal if the cluster never changed timelines. - Partial segments, named like
000000010000000000000042.partial, which a promoted standby may archive. These are only requested in specific cases.
Your command should treat any %f the same way: copy it if it exists, fail with a non-zero status if it does not. Do not hard-code a pattern that only matches 24-character segment names.
Signalling that recovery should happen
Having a restore_command is not enough. PostgreSQL only enters archive recovery when it sees a signal file in the data directory. Since PostgreSQL 12, there are two:
| File | Mode |
|---|---|
| `recovery.signal` | Targeted archive recovery. The server recovers to the end of the archive or to the configured target, then ends recovery and becomes writable. |
| `standby.signal` | Standby mode. The server keeps waiting for new WAL and never ends recovery on its own. |
For point-in-time recovery you want recovery.signal. The file can be empty. Its presence is what matters:
sudo -u postgres touch /var/lib/postgresql/16/main/recovery.signalIf both files exist, standby.signal wins and the server stays in standby mode. That is a common cause of "my restore never finishes".
When recovery ends, PostgreSQL deletes recovery.signal itself. This prevents the server from trying to recover again at the next restart.
What happens at startup
With recovery.signal present and restore_command set, startup proceeds like this:
- PostgreSQL reads
backup_labelin the data directory. That file was written bypg_basebackupand records the WAL position where replay must begin. - It looks for the starting segment, first in
pg_wal, then by runningrestore_command. - It replays records in order, fetching each new segment as it needs it.
- It stops when it reaches a recovery target, or when
restore_commandreports that no more files are available. - It ends recovery according to
recovery_target_action, then removesrecovery.signal.
The base backup taken with -X stream already contains the WAL generated during the backup, so the first segments are found in pg_wal and restore_command is only needed for later ones.
Writing a more careful restore_command
For a local archive, the plain cp is fine. A few variations come up in practice.
For a compressed archive, decompress into %p:
restore_command = 'gunzip < /mnt/archive/%f.gz > "%p"'If the decompression fails halfway, PostgreSQL still sees the non-zero exit status, but a partial file may be left behind. PostgreSQL discards the file in that case, so this is safe.
For a remote archive, use a tool that fails loudly:
restore_command = 'scp archive-host:/srv/wal/%f "%p"'Make sure the postgres user can authenticate non-interactively, for example with an SSH key. A command that prompts for a password will hang recovery.
For object storage or anything more complex, put the logic in a script and call it:
#!/bin/bash
# /usr/local/bin/fetch_wal.sh usage: fetch_wal.sh <file-name> <destination-path>
set -euo pipefail
src="/mnt/archive/$1"
dst="$2"
if [ ! -f "$src" ]; then
exit 1
fi
cp "$src" "$dst"restore_command = '/usr/local/bin/fetch_wal.sh %f "%p"'The script returns 1 for a missing file and, thanks to set -e, any failure of cp also yields a non-zero status.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Server starts and accepts connections immediately, without replaying archived WAL | `recovery.signal` was not created, or it was created in the wrong directory | Create it in the data directory and restart |
| `FATAL: using recovery command file "recovery.conf" is not supported` | Settings left in the old file | Move the settings to `postgresql.conf` and delete `recovery.conf` |
| Log shows `could not open file "pg_wal/..."` then `restore_command` errors for a segment you expected | Segment missing or has a different name in the archive | Check the archive listing and the `archive_command` that wrote it |
| Recovery ends early at the first `cp` error | Archive has a gap, or permissions deny read access to the `postgres` user | Check that the file exists and is readable by `postgres` |
| Restore never completes and the server stays read-only | `standby.signal` exists as well | Remove it, then restart |
| Recovery hangs | `restore_command` is waiting on a password prompt or a network timeout | Use key-based authentication and test the command by hand as `postgres` |
Test the command by hand
Before relying on it, run the command manually as the postgres user with a real segment name:
sudo -u postgres bash -c 'cd /var/lib/postgresql/16/main && cp /mnt/archive/000000010000000000000042 "pg_wal/test_restore" && echo ok'Remove the test file afterwards. This checks permissions and paths without starting recovery.
Putting it together
The command, the signal file and the base backup work as a set. The restore procedure from the topic's code samples stops the server, moves the broken data directory aside, copies the base backup in, sets mode 700, creates recovery.signal, and adds restore_command and a target before starting the server. The next chapters cover how to choose the target and run the whole procedure.
Exercise
A recovery log ends with "cp: cannot stat '/mnt/archive/000000010000000000000043': No such file or directory" followed by "redo done". The last segment you archived was 000000010000000000000042. Is something wrong? How would your answer change if segment 000000010000000000000040 had been missing instead?
Show answer
Nothing is wrong in the first case. Segment 43 does not exist because the archive ends at 42, so the non-zero exit status from restore_command tells PostgreSQL there is no more WAL to replay, and recovery concludes normally. If segment 40 were missing, the same error would appear earlier, at 40, and recovery would stop there, well before the point you wanted. That is a gap in the archive, and you would need to find the lost segment or accept a recovery that ends earlier than intended.
Exercise
You create recovery.signal and set restore_command, but after the restore the server remains read-only and keeps waiting for WAL. What is the most likely cause, and what do you do?
Show answer
A standby.signal file also exists in the data directory. It takes precedence, so the server runs as a standby and never ends recovery. Stop the server, delete standby.signal, keep recovery.signal, and start again.