OpenTelemetry Support
...
Service Integration Guides
Postgres Monitoring

Enable logging in PostgreSQL

5min

This document provides instructions for enabling logging in PostgreSQL.

Enabling Logging in PostgreSQL

  1. Edit the postgresql.conf which locate at /etc/postgresql/<version>/main/postgresql.conf file. This file is usually found in the data directory of your PostgreSQL installation. Open it in your preferred text editor.
  2. Set Logging Parameters: Add or modify the following parameters to enable logging:
  3. Use the below configuration for postgreSQL version >=15
# Use these settings for pgsql version >= 15 log_destination = 'jsonlog' logging_collector = on log_min_messages = warning log_min_duration_statement = 250 # duration in ms. Change this for desired slow query logging

Use the below configuration for postgreSQL version < 15

# Use these settings for pgsql version < 15 log_destination = 'stderr' logging_collector = on log_min_messages = warning log_min_duration_statement = 250 # duration in ms. Change this for desired slow query logging log_line_prefix = '%m [%p] %d %u '

JSON log is only supported in postgresql 15 and above

3. Save Changes and Restart PostgreSQL: After making changes, save the postgresql.conf file and restart the PostgreSQL server to apply the new logging settings.

systemctl restart postgresql

Important Notes

  • PostgreSQL Version Limitations:
    • Versions 14 and older: Cannot save logs in JSON format. Supported formats are stderr, csvlog, andsyslog.
    • Versions 15, 16, and 17: JSON logging is supported, providing more structured and flexible log data.
  • Check Your PostgreSQL Version: You can check your PostgreSQL version by running the following command in the psql command line interface:
SELECT version();