Better Stack PostgreSQL logging

Start logging in 2 minutes

What you would like to collect from your PostgreSQL?

Collect logs & metrics

Collect logs and metrics from your PostgreSQL database using Vector and Prometheus postgres exporter.

1. Enable query statistics

The query performance charts in the PostgreSQL dashboard read from the pg_stat_statements extension. Add it to shared_preload_libraries in postgresql.conf and restart PostgreSQL:

postgresql.conf
shared_preload_libraries = 'pg_stat_statements'

Then create the extension:

Enable pg_stat_statements
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

Already set shared_preload_libraries?

Keep the existing entries and add pg_stat_statements to the comma-separated list.

2. Create a monitoring user

Create a dedicated PostgreSQL user for Vector to scrape metrics. The pg_monitor role lets it read the statistics views, including pg_stat_statements:

Create a new user
psql -h localhost -U postgres \
  -c "CREATE USER better_stack_metrics WITH PASSWORD 'your_password_here'; \
      GRANT pg_monitor TO better_stack_metrics; \
      GRANT CONNECT ON DATABASE postgres TO better_stack_metrics; \
      GRANT USAGE ON SCHEMA public TO better_stack_metrics;"

Fill in your password

Replace your_password_here with password of your choice.

Create /etc/postgres_exporter.conf config file using PostgreSQL user password you've just set:

/etc/postgres_exporter.conf
export DATA_SOURCE_NAME="postgresql://better_stack_metrics:your_password_here@localhost:5432/postgres"

3. Install PostgreSQL exporter

Install latest postgres exporter:

Install postgres exporter
curl -sSL https://telemetry.betterstack.com/install/postgres_exporter | sudo -E bash

4. Enable the query statistics collector

The exporter's stat_statements collector is off by default. Add --collector.stat_statements to the exporter command in /usr/local/bin/better_stack_postgres_exporter.sh:

/usr/local/bin/better_stack_postgres_exporter.sh
#!/bin/bash
source /etc/postgres_exporter.conf
exec /usr/local/bin/postgres_exporter --collector.postmaster --collector.stat_statements

Reinstalling the exporter resets this file

The install script regenerates the wrapper without the flag. Re-add --collector.stat_statements after any reinstall.

5. Run exporter

Start the PostgreSQL exporter:

Linux macOS
systemctl start postgres_exporter
launchctl start com.prometheus.postgres_exporter

6. Set up Vector

Select your platform:

Install and configure Vector on Ubuntu or Debian:

Set up Vector
curl -sSL https://telemetry.betterstack.com/setup-vector/postgresql_with_metrics/$SOURCE_TOKEN \
  -o /tmp/setup-vector.sh && \
  bash /tmp/setup-vector.sh


Curious about what the script does?

  • Installs Vector using official package.
  • Configures Vector for your Better Stack source.
  • Enables and restarts Vector.

The script will ask for confirmation before making any changes.

You should see your logs in Better Stack → Live tail.

Collect only logs

Collect logs from your PostgreSQL database using Vector.

Select your platform:

Install and configure Vector on Ubuntu or Debian:

Set up Vector
curl -sSL https://telemetry.betterstack.com/setup-vector/postgresql/$SOURCE_TOKEN \
  -o /tmp/setup-vector.sh && \
  bash /tmp/setup-vector.sh


Curious about what the script does?

  • Installs Vector using official package.
  • Configures Vector for your Better Stack source.
  • Enables and restarts Vector.

The script will ask for confirmation before making any changes.

You should see your logs in Better Stack → Live tail.

Need help?

Please let us know at hello@betterstack.com.
We're happy to help! 🙏

Additional information

Looking for manual instructions to set up Vector?

Find step-by-step instructions in our dedicated Vector guide.