PostgreSQL log rotation size based

Viewed 715

I try to limit PostgreSQL logs based on size.
I have tried to configure logs configuration like that:

log_destination = 'csvlog'
logging_collector = on
log_directory = '/var/log/pgsql'
log_filename = 'postgresql-%a.log'
log_truncate_on_rotation = on 
log_rotation_age = 1d
log_rotation_size = 100kB

The logs rotate based on age works well, but the sized based rotation doesn't seem to work.

I have limited the log size to 100kB but the csv file's size is growing up to 600MB. Eventually, there is only one file created by the 'size based rotation' which is 1.5MB (postgresql-Sun.csv-20210502.gz). How can I make the log rotation work based on size?

Postgresql version - 12.2

1 Answers

Per my comment, the part you are missing is Log rotation log_truncate_on_rotation "However, truncation will occur only when a new file is being opened due to time-based rotation, not during server startup or size-based rotation." In that section there is an example of how to do what you want using @jjanes suggestion.

Related