I am trying to the setup and control PostgreSQL logs by Linux log rotate as the following
logrotate_PostgreSQL.conf
file looks like this
/var/log/PG/*.log {
size 10K
rotate 10
copytruncate
missingok
delaycompress
nomail
sharedscripts
postrotate
/bin/kill -HUP ` systemctl show --property MainPID postgresql-15.service | grep MainPID | sed s'/=/ /g' | awk '{print $NF}' ` 2>/dev/null || true
endscript
}
as we can see from above we defined the size to 10KB , so once postgres.log
log reached the 10KB then log rotate should create the backup log as postgres.log.1
we activate the log rotate as the following
su postgres -c '/usr/sbin/logrotate -d /etc/logrotate_PostgreSQL.conf
and output is like this
reading config file /etc/logrotate_PostgreSQL.conf
Allocating hash table for state file, size 15360 B
Handling 1 logs
rotating pattern: /var/log/PG/*.log 10240 bytes (10 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/PG/postgres.log
log needs rotating
rotating log /var/log/PG/postgres.log, log->rotateCount is 10
dateext suffix '-20230827'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /var/log/PG/postgres.log.10 to /var/log/PG/postgres.log.11 (rotatecount 10, logstart 1, i 10),
renaming /var/log/PG/postgres.log.9 to /var/log/PG/postgres.log.10 (rotatecount 10, logstart 1, i 9),
renaming /var/log/PG/postgres.log.8 to /var/log/PG/postgres.log.9 (rotatecount 10, logstart 1, i 8),
renaming /var/log/PG/postgres.log.7 to /var/log/PG/postgres.log.8 (rotatecount 10, logstart 1, i 7),
renaming /var/log/PG/postgres.log.6 to /var/log/PG/postgres.log.7 (rotatecount 10, logstart 1, i 6),
renaming /var/log/PG/postgres.log.5 to /var/log/PG/postgres.log.6 (rotatecount 10, logstart 1, i 5),
renaming /var/log/PG/postgres.log.4 to /var/log/PG/postgres.log.5 (rotatecount 10, logstart 1, i 4),
renaming /var/log/PG/postgres.log.3 to /var/log/PG/postgres.log.4 (rotatecount 10, logstart 1, i 3),
renaming /var/log/PG/postgres.log.2 to /var/log/PG/postgres.log.3 (rotatecount 10, logstart 1, i 2),
renaming /var/log/PG/postgres.log.1 to /var/log/PG/postgres.log.2 (rotatecount 10, logstart 1, i 1),
renaming /var/log/PG/postgres.log.0 to /var/log/PG/postgres.log.1 (rotatecount 10, logstart 1, i 0),
copying /var/log/PG/postgres.log to /var/log/PG/postgres.log.1
truncating /var/log/PG/postgres.log
running postrotate script
running script with arg /var/log/PG/*.log : "
/bin/kill -HUP ` systemctl show --property MainPID postgresql-15.service | grep MainPID | sed s'/=/ /g' | awk '{print $NF}' ` 2>/dev/null || true
"
removing old log /var/log/PG/postgres.log.11
error: error opening /var/log/PG/postgres.log.11: No such file or directory
but when we are looking under /var/log/PG
we see the following
du -sh *
123M postgres.log
552M postgres.log.1
84K postgres.log.2
ls -ltr
total 690416
-rw------- 1 postgres postgres 85064 Aug 27 21:00 postgres.log.2
-rw------- 1 postgres postgres 578772465 Aug 27 21:05 postgres.log.1
-rw------- 1 postgres postgres 68130128 Aug 27 22:08 postgres.log
so we can saw that postgres.log
not rotated even we defined the size to 10KB
what is wrong with my configuration?
references -
https://www.ibm.com/support/pages/how-cause-postgres-log-pglog-always-rotate-1-gb https://www.mohitkhare.com/blog/manage-logs-with-logrotate/