In a Ubuntu machine hosting a postgresql database, I have 16G space taken up at /var/lib/postgresql/9.3/main/pg_log. Apparently, log files from many months ago are being stored as well. I manually deleted a few to get rid of my disk full error. What's a robust way to delete old postgresql logs every week? Anything I can do in postgresql.conf to automate this process?

Currently I have the following in postgresql.conf:

#log_truncate_on_rotation = off         # If on, an existing log file with the
                                        # same name as the new log file will be
                                        # truncated rather than appended to.
                                        # But such truncation only occurs on
                                        # time-driven rotation, not on restarts
                                        # or size-driven rotation.  Default is
                                        # off, meaning append to existing files
                                        # in all cases.
#log_rotation_age = 1d                  # Automatic rotation of logfiles will
                                        # happen after that time.  0 disables.
log_rotation_size = 100MB               # Automatic rotation of logfiles will
                                        # happen after that much log output.
                                        # 0 disables.

I'm thinking I'll enable log_rotation_age and set its value to 30d. But will this auto delete older log files?


You can use the configuration below;

log_truncate_on_rotation = on
log_rotation_age = 1d
log_filename = 'postgresql-%a.log'
log_rotation_size = 0  #just rotate daily

This says create a log file with a name like 'postgresql-Mon.log' and when rotation occurs override the old file with the same name.

  • It will work but its only single log file per server. – Gherman Oct 29 '19 at 16:21
  • @Gherman you will end up with 7 files (1 file per week - day). Can you be more declarative with your comment. – Sahap Asci Oct 30 '19 at 7:46

PostgreSQL supports log rotation feature but doesn't support log removing feature. So user should remove logs manually or has to use logrotate utility to remove them.

Here is the official document.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.