I’m using Postgres 9.5 on Ubuntu 14.04. I wanted to rotate my logs whenever they reach a certain size. So I edited /etc/postgresql/9.5/main/postgresql.conf and set this
log_rotation_size = 50MB # Automatic rotation of logfiles will
# happen after that much log output.
# 0 disables.
Then I restarted my server
sudo /etc/init.d/postgresql restart
However after running some operations, I ran out of disk space, and discovered my log had not rotated at all …
myuser@mymachine:~$ ls -al /var/log/postgresql/postgresql-9.5-main.log
-rw-r----- 1 postgres postgres 3165773943 Dec 29 18:34 /var/log/postgresql/postgresql-9.5-main.log
What else do I need to do to get the log rotation to kick in?
Best Answer
In Debian/Ubuntu, per policy it's the
logrotate
package that is in charge of handling log rotation and purge for all services, PostgreSQL included.From https://www.debian.org/doc/debian-policy/ch-files.html :
So there is a
/etc/logrotate.d/postgresql-common
configuration file that can be tuned to rotate log files when they grow over a size given by thesize
keyword , among a lot of other options.On the other hand, should you want to use the PostgreSQL feature to achieve this, as opposed to the pre-configured method of the OS, you need to consider all logging parameters in
postgresql.conf
, not justlog_rotation_size
. First a Debian/Ubuntu packaged PostgreSQL will turn offlogging_collector
to not interfere withlogrotate
, but most oflog_*
options depend on it to be effective, as mentioned in the doc, for instance:https://www.postgresql.org/docs/current/static/runtime-config-logging.html