PostgreSQL Streaming Replication – Archive Command Failed with Exit Code 1

postgresql

When I bring up the replica Node I am get the following error in the log

2020-04-27 00:12:26.278 EDT [3084] LOG:  archive command failed with exit code 1
2020-04-27 00:12:26.278 EDT [3084] DETAIL:  The failed archive command was: test ! -f /var/lib/postgresql/pg_log_archive/main/00000001000000000000000C && cp pg_wal/00000001000000000000000C /var/lib/postgresql/pg_log_archive/replica/00000001000000000000000C

The achive command is set to below:

archive_command = 'test ! -f /var/lib/postgresql/pg_log_archive/main/%f && cp %p /var/lib/postgresql/pg_log_archive/replica/%f'

The configuration:

Primary Server postgresql.conf:

wal_level = replica
wal_log_hints = on
archive_mode = on
archive_command = 'test ! -f /var/lib/postgresql/pg_log_archive/main/%f && cp %p /var/lib/postgresql/pg_log_archive/replica/%f'
max_wal_senders = 10
wal_keep_segments = 64

Standby server postgres.conf:

wal_level = replica
wal_log_hints = on
archive_mode = on
archive_command = 'test ! -f /var/lib/postgresql/pg_log_archive/replica/%f && cp %p /var/lib/postgresql/pg_log_archive/main/%f'
max_wal_senders = 10
wal_keep_segments = 64
hot_standby = on

recovery.conf:

standby_mode = on
primary_conninfo = 'host=192.168.56.103 port=5432 user=postgres password=****'
restore_command ='cp var/lib/postgresql/pg_log_archive/replica/%f %p'
recovery_target_timeline ='latest'

Best Answer

There are two possibilities:

  1. You have archive_mode = always on the standby.

    Then the standby server will try to archive WAL segments (redundantly).

  2. The standby server is not really a standby server, but a standalone database. That means that you have made a mistake setting up the standby server.

    The way to determine is that is the case, run

    SELECT pg_is_in_recovery();
    

    If that returns FALSE, the server is not a standby.