PostgreSQL WAL archiving fails with 'archive_command' permission denied after automated logrotate changed owner
Environment
Primary PostgreSQL 13 on Ubuntu 20.04, wal_level=replica, archive_mode=on, archive_command='bash /opt/pg/archive-wal.sh %p %f', WALs archived to NFS mount /mnt/pg_archive; cron-driven logrotate runs daily at 02:00.
Symptoms
WAL archival stops at 02:05 with repeated errors in PostgreSQL log: 'archive command failed with exit code 1' and WAL files pile up in pg_wal directory; replication to hot standby falls behind and streaming eventually disconnects when disk fills.
Observations
Checking system logs shows logrotate ran just before the failure and changed ownership of /opt/pg/archive-wal.sh to root:root with mode 700; previously it was postgres:postgres 750. Running the script as the postgres user now returns 'Permission denied' when trying to write to /mnt/pg_archive because the NFS directory retained original perms but the script is not executable by postgres. Manual chmod/chown fixes allow archive to resume.
Already attempted
Re-applied original ownership/chmod and restarted PostgreSQL; WAL archiving resumed. Added verbose logging to archive-wal.sh to capture failures. Reviewed logrotate config for /var/log/postgresql but found an extra global postrotate script that invoked a permissions fix incorrectly targeting /opt/pg; disabled it temporarily.
Question
What safer practices and checks should I put in place so automated rotation and maintenance cannot accidentally change ownership or permissions of PostgreSQL helper scripts and stop WAL archiving again, and what automated recovery steps are advisable when archive_command fails?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Databases30% match
PostgreSQL replica stops streaming WAL after pg_repack on primary; replication slot reports missing WAL
Open0 answers - Databases27% match
PostgreSQL WAL archiver fails with 'could not stat' after backup script replaced pg_wal with symlink
Open0 answers - Databases25% match
PostgreSQL standby stops applying WAL during long-running transactions on primary after pg_basebackup restore
Open0 answers - Databases25% match
PostgreSQL replica falls behind and reports WAL streaming delay when frequent autovacuum runs on primary
Open0 answers - Databases24% match
PostgreSQL WAL archiving stalls during pg_basebackup when archive_command uses rsync over SSH
Open0 answers