PostgreSQL Server Log
PostgreSQL server logs including queries, errors, connections, and performance data
Quick Facts
Default Path (Linux)
/var/log/postgresql/postgresql-17-main.logDocker
stderrDefault Format
stderr (text)
JSON Native
Yes (since 15)
Rotation
log_rotation_age/size or logrotate
Log Example
Default format: Standard Error Format
Example Log Entrylog
2025-12-20 14:32:18.123 UTC [1234] postgres@mydb LOG: statement: SELECT * FROM users WHERE id = 1Structure:
timestamp [pid] user@database level: messagePaths by Platform
Debian / Ubuntu
/var/log/postgresql/postgresql-17-main.logRHEL / CentOS
/var/lib/pgsql/data/log/Available Formats
Standard Error Format
Default
Example:
2025-12-20 14:32:18.123 UTC [1234] postgres@mydb LOG: statement: SELECT * FROM users WHERE id = 1Structure:
timestamp [pid] user@database level: messageCSV Log Format
Config Required
Example:
2025-12-20 14:32:18.123 UTC,"postgres","mydb",1234,"192.168.1.100:54321",12345678.1234,1,"SELECT",2025-12-20 14:30:00 UTC,1/1,0,LOG,00000,"statement: SELECT * FROM users WHERE id = 1",,,,,,"psql"Structure:
timestamp,user,database,pid,client,session_id,line_num,command_tag,session_start,vxid,txid,level,code,message,detail,hint,query,pos,context,location,appJSON Log Format
Native
Config Required
Available since version 15
Example:
{"timestamp":"2025-12-20 14:32:18.123 UTC","pid":1234,"user":"postgres","dbname":"mydb","error_severity":"LOG","message":"statement: SELECT * FROM users WHERE id = 1"}Structure:
JSON object with structured fieldsFields Reference
| Field | Type | Description | Example |
|---|---|---|---|
timestamp | datetime | When the log entry was created | 2025-12-20 14:32:18.123 UTC |
pid | integer | Process ID of the backend | 1234 |
user | string | Database user name | postgres |
dbname | string | Database name | mydb |
client_addr | ip | Client IP address | 192.168.1.100 |
error_severity | string | Log level | LOG |
message | string | Log message content | statement: SELECT * FROM users WHERE id = 1 |
duration | float (milliseconds) | Query duration (when log_duration or log_min_duration_statement enabled) | 12.345 |
sql_state_code | string | SQL state error code | 42P01 |
application_name | string | Application name from connection | psql |
Parsing Patterns
Grok Patterns
stderr:
%{TIMESTAMP_ISO8601:timestamp} %{TZ:timezone} \[%{NUMBER:pid}\] %{DATA:user}@%{DATA:database} %{WORD:level}: %{GREEDYDATA:message}Collector Configurations
logstashruby
1filter {2 grok {3 match => { "message" => "%{TIMESTAMP_ISO8601:timestamp} %{TZ:timezone} \[%{NUMBER:pid}\] %{DATA:user}@%{DATA:database} %{WORD:level}: %{GREEDYDATA:pg_message}" }4 }5 if "duration:" in [pg_message] {6 grok {7 match => { "pg_message" => "duration: %{NUMBER:duration:float} ms" }8 }9 }10}Configuration
Enable Logging
Enable detailed logging in postgresql.conf
postgresql.conf
# Logging destination
logging_collector = on
log_directory = 'log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
# What to log
log_statement = 'all' # none, ddl, mod, all
log_duration = on
log_min_duration_statement = 1000 # Log queries > 1 second
# Connection logging
log_connections = on
log_disconnections = on
# Error logging
log_error_verbosity = default # terse, default, verboseEnable Json
Recommended
Enable JSON logging format (PostgreSQL 15+)
# In postgresql.conf
log_destination = 'jsonlog'
logging_collector = on
log_directory = 'log'
log_filename = 'postgresql.json'Log Rotation
Tool: PostgreSQL native or logrotate | Config: postgresql.conf
postgresql.conf
# Native rotation
log_rotation_age = 1d # Rotate daily
log_rotation_size = 100MB # Or when reaching 100MB
log_truncate_on_rotation = on
# With logrotate (if logging_collector = off)
/var/log/postgresql/*.log {
daily
rotate 7
compress
missingok
notifempty
postrotate
/usr/bin/pg_ctlcluster 17 main reload
endscript
}Use Cases
Slow query analysis
Find queries exceeding duration threshold
duration
message
duration > 1000Connection monitoring
Track connection patterns
user
client_addr
message
Error rate
Monitor ERROR and FATAL events
error_severity
Troubleshooting
All Available Variables
log_line_prefix
%aApplication name%uUser name%dDatabase name%rRemote host and port%hRemote host%pProcess ID%tTimestamp without milliseconds%mTimestamp with milliseconds%iCommand tag%eSQL state error code%cSession ID%lSession line number%sSession start timestamp%vVirtual transaction ID%xTransaction ID%qStop here in non-session processesTested On
v17.0 on Ubuntu 24.04
db_expert - 2025-12-12
v16.4 on Debian 12
admin - 2025-11-01
Last updated: 2025-12-12 by db_expert
3 contributors187 upvotes
Validated
Community Discussions
Help improve this documentation
Found an error or want to add more examples? Contributions are welcome!