pgBadger is a Perl tool that reads Postgres logs and produces an HTML report — slow queries, traffic by hour, lock waits, errors, autovacuum activity, and connection patterns. Where pg_stat_statements shows you cumulative query stats inside the database, pgBadger shows you what happened in a window of time outside the database, which is what you want during an incident or for the weekly performance review. This article covers the logging config that pgBadger needs, a daily report pipeline, and the patterns to watch for in the output.
How to verify
Confirm the logging settings and that logs are reaching pgBadger:
psql -c "SHOW log_destination;"
psql -c "SHOW log_line_prefix;"
psql -c "SHOW log_min_duration_statement;"
ls -la /var/log/postgresql/
pgbadger --version
A pgBadger run produces an HTML file in seconds for a few hundred MB of logs; gigabyte-scale logs take longer but parallelise.
What’s happening
Postgres writes logs in a format that depends on log_line_prefix, log_min_duration_statement, log_lock_waits, and a handful of related GUCs. pgBadger parses those logs (CSV format is fastest, syslog and stderr are also supported), groups queries by normalised form, ranks them by total duration, and emits an HTML report with charts. It’s stateless — the report covers exactly the time window of the input logs.
The right settings for pgBadger are not the right settings for “off”. You want every query above some threshold logged, plus lock waits, plus checkpoint activity. The logging volume can be substantial on a busy host (1-10 GB/day is normal); rotate aggressively and ship to a dedicated log volume.
The procedure
-
Configure logging in
postgresql.conf:log_destination = 'stderr' logging_collector = on log_directory = '/var/log/postgresql' log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' log_rotation_age = 1d log_rotation_size = 1GB log_min_duration_statement = 500 # ms — log anything slower log_checkpoints = on log_connections = on log_disconnections = on log_lock_waits = on log_temp_files = 0 # log every temp file log_autovacuum_min_duration = 0 log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d,app=%a,client=%h ' log_statement = 'ddl' # 'all' is too noisy lc_messages = 'C' # English messages for parsinglog_line_prefixis the most important — pgBadger expects a specific shape and the prefix above is the documented minimum. -
Reload Postgres:
sudo systemctl reload postgresql@16-main -
Install pgBadger:
sudo apt install pgbadger pgbadger --version -
Run a one-off report:
pgbadger -j 4 -O /var/www/pgbadger \ -p '%t [%p]: [%l-1] user=%u,db=%d,app=%a,client=%h ' \ /var/log/postgresql/postgresql-2026-06-01*.log-j 4parallelises parsing across 4 cores. Open the HTML output in a browser — the most useful pages are “Top slowest”, “Histogram of query times”, “Most frequent queries”, and “Lock waits”. -
Wire up a daily report:
sudo tee /etc/cron.daily/pgbadger >/dev/null <<'EOF' #!/bin/bash set -e YESTERDAY=$(date -d 'yesterday' +%Y-%m-%d) OUT=/var/www/pgbadger/$YESTERDAY.html pgbadger -j 4 -O /var/www/pgbadger -o $YESTERDAY.html \ -p '%t [%p]: [%l-1] user=%u,db=%d,app=%a,client=%h ' \ /var/log/postgresql/postgresql-$YESTERDAY*.log find /var/www/pgbadger -name '*.html' -mtime +30 -delete EOF sudo chmod +x /etc/cron.daily/pgbadgerFront the directory with Nginx behind basic auth or a VPN — the report contains query text and can leak schema, table names, and sometimes inline literals.
-
For incremental mode on a busy host:
pgbadger --incremental -O /var/www/pgbadger \ -p '...' /var/log/postgresql/*.logMaintains state between runs in
/var/www/pgbadger/LAST_PARSED.tmpso subsequent runs only process new entries — useful when you want hourly reports.
Common pitfalls
log_min_duration_statement = 0logs every statement — fine for a 30-minute capture, fatal for daily. We default to 500ms on production, 100ms on staging.log_statement = 'all'plus a high QPS will overflow your log volume and slow Postgres (logging is synchronous by default). Uselog_min_duration_statementinstead andlog_statement = 'ddl'.- pgBadger needs the exact
log_line_prefixit expects; mismatch and most queries silently fail to parse. - The report is static HTML. Don’t expose it to the public internet — it contains query text and sometimes inline data.
- CSV log destination (
log_destination = 'csvlog') parses faster than stderr — switch to it if pgBadger runs are taking hours.
In the engagements we run, the pgBadger daily report is the morning health check — paired with pg_stat_statements it tells us what got slower yesterday, what triggered lock waits, and whether autovacuum is keeping up. We hold the reports for 30 days and link them into the runbook for incident reviews. For the operating model that includes log analysis as a routine, see managed operations.