How to View All PostgreSQL Logs by Combining Journalctl Outputs


2 views

When debugging PostgreSQL issues, you might notice that logs appear in different journalctl outputs. For example:

journalctl -u postgresql.service
journalctl SYSLOG_IDENTIFIER=postgres

These commands often show different log entries because PostgreSQL uses multiple logging mechanisms:

PostgreSQL can log through:

  • Systemd service unit output (-u postgresql.service)
  • Direct syslog messages (SYSLOG_IDENTIFIER=postgres)
  • Stderr output when running in non-systemd mode

To see all PostgreSQL-related logs in one stream, use:

journalctl -f _COMM=postgres

This combines:

  • Service unit logs
  • Syslog messages
  • Direct process output

For more targeted log viewing:

# Show error messages only
journalctl -p err _COMM=postgres

# Show logs from last hour
journalctl --since "1 hour ago" _COMM=postgres

# Show logs with specific process ID
journalctl _PID=2361

If you're still missing logs, check:

# Verify journal storage size
journalctl --disk-usage

# Check if logs persist after reboot
journalctl --list-boots | head -5

Remember that some PostgreSQL configurations may log directly to files rather than the journal. Check your postgresql.conf for logging configurations.


When working with PostgreSQL in systemd environments, logs can appear in different journal sources:

# Shows service-specific logs
journalctl -u postgresql.service

# Shows logs by syslog identifier
journalctl SYSLOG_IDENTIFIER=postgres

To view all PostgreSQL-related logs regardless of their source, use this comprehensive command:

journalctl -f _SYSTEMD_UNIT=postgresql.service + SYSLOG_IDENTIFIER=postgres

This combines both service unit logs and process-specific logs in real-time (-f flag). The '+' operator acts as a logical OR between filters.

For more precise log filtering, consider these additional approaches:

# Show logs with priority ERROR or higher
journalctl -u postgresql.service -p err

# Filter by time range
journalctl -u postgresql.service --since "2023-08-01" --until "2023-08-31"

# Show logs from specific process ID
journalctl _PID=2361

# JSON output for parsing
journalctl -u postgresql.service -o json

To ensure all PostgreSQL logs are properly captured:

# Check current PostgreSQL logging configuration
sudo -u postgres psql -c "SHOW log_destination;"

# Typical recommended settings in postgresql.conf:
log_destination = 'syslog'
syslog_ident = 'postgres'
syslog_facility = 'LOCAL0'

When dealing with large log volumes:

# Limit output to last 100 entries
journalctl -u postgresql.service -n 100

# Show disk usage
journalctl --disk-usage

# Set maximum usage limit (1GB in this case)
sudo journalctl --vacuum-size=1G