PostgreSQL Logging Configuration
3 minute read
Source: pinned upstream
README.md, sections “PostgreSQL Configuration” and “Log Statements”.
pgBadger can only report information that PostgreSQL writes to the log. Start with a parseable prefix and a deliberate statement-logging policy, then add the operational events you want to analyze.
Minimum query logging
To include query text and duration, enable duration-based statement logging:
0 logs every completed statement. On a busy server, choose a higher threshold in milliseconds to control log volume. Measure the overhead and storage growth before enabling a low threshold in production.
If you only need duration and query counts, not the query text, use:
Prefer log_min_duration_statement when you need the slowest-query and total-query-time reports.
Required prefix fields
A custom log_line_prefix must include both:
- a time field:
%t,%m, or%n; - a process or session field:
%por%c.
A minimal stderr prefix is:
A more useful prefix records user, database, application, and client:
The equivalent prefix for a syslog destination omits the timestamp and process fields already supplied by syslog:
Another supported key order is:
When your prefix is not one of pgBadger’s recognized forms, pass the exact value with --prefix. Do not simplify or retype it differently from postgresql.conf.
Recommended event settings
Enable the event classes you want to appear in the report:
These settings can produce substantial log traffic. In particular, log_temp_files = 0 and log_autovacuum_min_duration = 0 log every qualifying event; adjust them to match the workload and retention budget.
Keep server messages in English
The parser recognizes PostgreSQL server messages in English. Use either:
or:
Locales such as fr_FR.UTF-8 are not supported by the upstream parser.
Avoid conflicting statement settings
Do not enable log_min_duration_statement, log_duration, and log_statement = 'all' together. The same execution can be logged more than once, which inflates pgBadger counters and greatly increases log volume.
| Goal | Recommended setting |
|---|---|
| Query text plus timing | log_min_duration_statement = 0 or a chosen threshold |
| Duration and count only | log_min_duration_statement = -1, log_duration = on |
| Broad statement auditing | Treat log_statement as a separate logging policy; do not combine all three settings for pgBadger statistics |
After reloading PostgreSQL, inspect several real log entries before running a large analysis. Verify that the timestamp, process/session identifier, user, database, application, and client fields match the selected format.