Postgresql offers a logging system that helps database administrators monitor server activity, troubleshoot problems, and study database behaviour. When a Postgresql server runs, log files keep growing, so log management becomes a part of database administration.
Log rotation lets PostgreSQL automatically create log files when a set time passes or a set file size is reached. This stops any log file from getting too large and makes it easier to store, search, and keep.
In this article, we are detailing the parameters related to log rotation. These parameters include logging_collector, log_rotation_age, log_rotation_size, log_truncate_on_rotation, log_filename and log_directory.
1.logging_collector
show logging_collector ;
Result:
logging_collector
-------------------
off
(1 row)
Check the path of the postgres configuration file inside the psql terminal like this
show config_file ;
Result:
config_file
-----------------------------------------
/etc/postgresql/18/main/postgresql.conf
(1 row)
Open this file using the sudo nano command.
sudo nano /etc/postgresql/18/main/postgresql.conf
Enable the logging_collector parameter like this.
logging_collector = on # Enable capturing of stderr, jsonlog,
Now, restart PostgreSQL to reflect the changes.
sudo systemctl restart postgresql
Check this parameter’s metadata from pg_settings like this.
select * from pg_settings where name = 'logging_collector';
Result:
-[ RECORD 1 ]---+----------------------------------------------------------------------------
name | logging_collector
setting | off
unit |
category | Reporting and Logging / Where to Log
short_desc | Start a subprocess to capture stderr, csvlog and/or jsonlog into log files.
extra_desc |
context | postmaster
vartype | bool
source | default
min_val |
max_val |
enumvals |
boot_val | off
reset_val | off
sourcefile |
sourceline |
pending_restart | f
Purpose:
This collector will create a separate process to capture the stderr, csvlog, and json type of logs into the log folder inside the cluster directory.
If you are using the postgres version 19, you can see this parameter definition from the guc_parameters.dat file, or if you are using a postgres version lower than 19, you can see this definition from the file named guc_tables.c
{ name => 'logging_collector', type => 'bool', context => 'PGC_POSTMASTER', group => 'LOGGING_WHERE',
short_desc => 'Start a subprocess to capture stderr, csvlog and/or jsonlog into log files.',
variable => 'Logging_collector',
boot_val => 'false',
},2. show log_rotation_age ;
show log_rotation_age ;
Result:
log_rotation_age
------------------
1d
(1 row)
Check this parameter’s metadata in pg_settings like this.
select * from pg_settings where name = 'log_rotation_age';
Result:
-[ RECORD 1 ]---+------------------------------------------------------------------
name | log_rotation_age
setting | 1440
unit | min
category | Reporting and Logging / Where to Log
short_desc | Sets the amount of time to wait before forcing log file rotation.
extra_desc | 0 disables time-based creation of new log files.
context | sighup
vartype | integer
source | default
min_val | 0
max_val | 35791394
enumvals |
boot_val | 1440
reset_val | 1440
sourcefile |
sourceline |
pending_restart | f
Purpose:
This parameter specifies the maximum amount of time that the postgres writes to a log file before automatically creating a new one.
Parameter definition from the file named guc_parameters.dat in the postgres source code.
{ name => 'log_rotation_age', type => 'int', context => 'PGC_SIGHUP', group => 'LOGGING_WHERE',
short_desc => 'Sets the amount of time to wait before forcing log file rotation.',
long_desc => '0 disables time-based creation of new log files.',
flags => 'GUC_UNIT_MIN',
variable => 'Log_RotationAge',
boot_val => 'HOURS_PER_DAY * MINS_PER_HOUR',
min => '0',
max => 'INT_MAX / SECS_PER_MINUTE',
},Now, change the value of this parameter to understand the functionality.
alter system set log_rotation_age = '1min';
select pg_reload_conf();
Result:
pg_reload_conf
----------------
t
(1 row)
3. show log_rotation_size;
show log_rotation_size;
Result:
log_rotation_size
-------------------
10MB
(1 row)
Check this parameter’s metadata like this.
select * from pg_settings where name = 'log_rotation_size';
Result:
-[ RECORD 1 ]---+-----------------------------------------------------------------
name | log_rotation_size
setting | 10240
unit | kB
category | Reporting and Logging / Where to Log
short_desc | Sets the maximum size a log file can reach before being rotated.
extra_desc | 0 disables size-based creation of new log files.
context | sighup
vartype | integer
source | default
min_val | 0
max_val | 2147483647
enumvals |
boot_val | 10240
reset_val | 10240
sourcefile |
sourceline |
pending_restart | f
Purpose:
This parameter specifies the maximum size a PostgreSQL log file can reach before it is automatically rotated and a new log file is created.
Parameter’s definition from the file named guc_parameters.dat file.
{ name => 'log_rotation_size', type => 'int', context => 'PGC_SIGHUP', group => 'LOGGING_WHERE',
short_desc => 'Sets the maximum size a log file can reach before being rotated.',
long_desc => '0 disables size-based creation of new log files.',
flags => 'GUC_UNIT_KB',
variable => 'Log_RotationSize',
boot_val => '10 * 1024',
min => '0',
max => 'INT_MAX',
},Now, change the value of this parameter to 100kb.
alter system set log_rotation_size = '100kB';
select pg_reload_conf();
Result:
pg_reload_conf
----------------
t
(1 row)
Now, check whether the value of this parameter has changed or not.
show log_rotation_size ;
Result:
log_rotation_size
-------------------
100kB
(1 row)
Now, set the parameter named log_statement to generate all the contents in the postgres log file very quickly.
alter system set log_statement = 'all';
select pg_reload_conf();
Result:
pg_reload_conf
----------------
t
(1 row)
Check if the value has changed or not.
show log_statement;
Result:
log_statement
---------------
all
(1 row)
Before setting the log_collector parameter, the postgres logs are generated in this path
postgres@cybrosys:~/18/main/log$ ls /var/log/postgresql
Result:
pgbouncer.log postgresql-17-main.log postgresql-18-main.log postgresql-19-main.log
After setting this parameter, the logs are generated inside the cluster directory.
postgres@cybrosys:~/18/main/log$ ls /var/lib/postgresql/18/main/
Result :
base global pg_commit_ts pg_logical pg_notify pg_serial pg_stat pg_subtrans pg_twophase pg_wal postgresql.auto.conf postmaster.pid
current_logfiles log pg_dynshmem pg_multixact pg_replslot pg_snapshots pg_stat_tmp pg_tblspc PG_VERSION pg_xact postmaster.opts
postgres@cybrosys:~/18/main/log$
Here, we can see that a new log-named folder is created for storing the postgres logs.
Now, check the contents inside the log folder after executing some sample workloads in postgresql.
postgres@cybrosys:~/18/main/log$ ls -lh
Result:
total 88K
-rw------- 1 postgres postgres 4.0K Nov 6 07:33 postgresql-2026-11-06_073356.log
-rw------- 1 postgres postgres 82K Nov 6 07:34 postgresql-2026-11-06_073400.log
Now, based on our custom values of the log_rotation_* related parameters, it will create a new log file in each minute.
4.log_truncate_on_rotation
show log_truncate_on_rotation;
Result:
log_truncate_on_rotation
--------------------------
off
(1 row)
Check this parameter’s metadata like this.
select * from pg_settings where name = 'log_truncate_on_rotation';
Result:
-[ RECORD 1 ]---+--------------------------------------------------------------
name | log_truncate_on_rotation
setting | off
unit |
category | Reporting and Logging / Where to Log
short_desc | Truncate existing log files of same name during log rotation.
extra_desc |
context | sighup
vartype | bool
source | default
min_val |
max_val |
enumvals |
boot_val | off
reset_val | off
sourcefile |
sourceline |
pending_restart | f
Purpose:
This parameter determines whether Postgres truncates (overwrites) an existing log file with the same name or appends new log entries to it during log rotation.
Parameter definition from the file named guc_parameters.dat in the postgres source code version 19.
{ name => 'log_truncate_on_rotation', type => 'bool', context => 'PGC_SIGHUP', group => 'LOGGING_WHERE',
short_desc => 'Truncate existing log files of same name during log rotation.',
variable => 'Log_truncate_on_rotation',
boot_val => 'false',
},5.log_filename
show log_filename ;
Result:
log_filename
--------------------------------
postgresql-%Y-%m-%d_%H%M%S.log
(1 row)
This is the naming format of the postgres log files that are generated in the log folder.
Example:
-rw------- 1 postgres postgres 4.0K Nov 6 07:33 postgresql-2026-11-06_073356.log
-rw------- 1 postgres postgres 101K Nov 6 07:34 postgresql-2026-11-06_073400.log
Check this parameter’s metadata from pg_settings like this.
select * from pg_settings where name = 'log_filename';
Result:
-[ RECORD 1 ]---+------------------------------------------
name | log_filename
setting | postgresql-%Y-%m-%d_%H%M%S.log
unit |
category | Reporting and Logging / Where to Log
short_desc | Sets the file name pattern for log files.
extra_desc |
context | sighup
vartype | string
source | default
min_val |
max_val |
enumvals |
boot_val | postgresql-%Y-%m-%d_%H%M%S.log
reset_val | postgresql-%Y-%m-%d_%H%M%S.log
sourcefile |
sourceline |
pending_restart | f
Purpose:
This parameter specifies the name of the postgres log file generated inside the log folder in the cluster directory.
Parameter definition from the guc_parameters.dat file.
{ name => 'log_filename', type => 'string', context => 'PGC_SIGHUP', group => 'LOGGING_WHERE',
short_desc => 'Sets the file name pattern for log files.',
flags => 'GUC_SUPERUSER_ONLY',
variable => 'Log_filename',
boot_val => '"postgresql-%Y-%m-%d_%H%M%S.log"',
},6.log_directory
show log_directory ;
Result:
log_directory
---------------
log
(1 row)
Check this parameter’s metadata from the pg_settings catalogue like this.
select * from pg_settings where name = 'log_directory';
Result:
-[ RECORD 1 ]---+------------------------------------------------------------------------
name | log_directory
setting | log
unit |
category | Reporting and Logging / Where to Log
short_desc | Sets the destination directory for log files.
extra_desc | Can be specified as relative to the data directory or as absolute path.
context | sighup
vartype | string
source | default
min_val |
max_val |
enumvals |
boot_val | log
reset_val | log
sourcefile |
sourceline |
pending_restart | f
Purpose:
This parameter specifies the path of the generated log file, and we can set whatever path we need.
Parameter definition from guc_parameters.dat file.
{ name => 'log_directory', type => 'string', context => 'PGC_SIGHUP', group => 'LOGGING_WHERE',
short_desc => 'Sets the destination directory for log files.',
long_desc => 'Can be specified as relative to the data directory or as absolute path.',
flags => 'GUC_SUPERUSER_ONLY',
variable => 'Log_directory',
boot_val => '"log"',
check_hook => 'check_canonical_path',
},PostgreSQL’s log rotation settings offer a way to handle database log files without needing to do it by hand. By knowing what logging_collector, log_rotation_age, log_rotation_size, log_truncate_on_rotation, log_filename, and log_directory do, you can decide when log files are made, how they are called, where they are kept, and what happens to log files when they are rotated.
The examples in this guide show how to check these parameters, change their values, reload the settings, and see what happens on the PostgreSQL server. When these settings are set up correctly, log files stay neat, are easier to take care of, and are better for keeping an eye on and fixing problems in databases.