Enable PostgreSQL query logging

Configure PostgreSQL SQL logging, distinguish reloadable settings from collector startup settings, and choose a narrower slow-query capture when appropriate.

Best practices
Illustration of monitoring and troubleshooting application behavior

To record SQL statements, set log_statement to the category you need. To have PostgreSQL itself manage log files, enable logging_collector. These are separate decisions: statements can also go to stderr, a service journal, or syslog without PostgreSQL's file collector.

Write statement logs to managed files

An example postgresql.conf:

log_destination = 'stderr'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_statement = 'all'

Here, pg_log is an explicitly chosen directory relative to data_directory, not a universal default. Ask the server for its active paths:

SHOW config_file;
SHOW data_directory;
SHOW log_directory;

Enabling or disabling logging_collector requires restarting the correct database instance. On a systemd installation, that might be:

sudo systemctl restart postgresql

Distribution packages can use a versioned or cluster-specific unit instead. Identify it before restarting; the generic unit may be only an umbrella service.

Change statement logging without a restart

For a privileged, persistent instance-level setting:

ALTER SYSTEM SET log_statement = 'all';
SELECT pg_reload_conf();

ALTER SYSTEM writes a persistent setting. It is not automatically temporary. Record the previous value and restore it after the investigation. If you added a new override and want to remove it:

ALTER SYSTEM RESET log_statement;
SELECT pg_reload_conf();

Resetting exposes any value in another configuration source, so check the effective result with SHOW log_statement.

Pick the appropriate category

Setting What it covers
none No statement-category logging
ddl Definition changes such as CREATE, ALTER, and DROP
mod Definition changes and data modifications
all Read statements as well as changes

Other error and duration settings can still log SQL when log_statement is none. Conversely, all does not guarantee every malformed SQL string appears: logging occurs after basic parsing.

Prefer a bounded performance investigation

log_min_duration_statement = 500

This records completed statements taking at least 500 ms. Threshold units are milliseconds, and 0 records all completed statement durations. Keep enough process or session context to link separate entries.

Full statement logging can generate considerable I/O and sensitive data. Once the path is confirmed, DataKit file collection can centralize it. Test multiline boundaries and timestamps using a fresh sample, and review parameter exposure before ingestion.

Can I limit logging to a user? With suitable privileges, ALTER ROLE app_user SET log_statement = 'all' sets a default for that role's new sessions. Reconnect to test it and reset the role setting when finished.

Where is the file? In this example it is below data_directory/pg_log. In another deployment, inspect the destination and service redirection instead of assuming that path.

See PostgreSQL logging configuration.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting