PostgreSQL Exporter is a specialized Prometheus exporter designed to collect
and expose a wide range of PostgreSQL database metrics.
This allows for comprehensive monitoring of database performance,
resource utilization, and query patterns through Prometheus and Grafana.
Our PostgreSQL Exporter is configured in Docker Compose as follows:
postgres-exporter:
<<: *common
image: bitnami/postgres-exporter:latest
profiles: ["grafanaprofile"]
container_name: postgres-exporter
environment:
DATA_SOURCE_NAME: "postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}?sslmode=disable"
DISABLE_DEFAULT_METRICS: "true"
depends_on:
postgres:
condition: service_healthy
prometheus:
condition: service_started
ports:
- "9187:9187"
bitnami/postgres-exporter
imageDATA_SOURCE_NAME
: Connection string for the PostgreSQL databaseDISABLE_DEFAULT_METRICS
: Customizes which metrics are collectedFor optimal monitoring, we have configured PostgreSQL with these key settings in postgresql.conf:
# Enable statistics collection
track_activities = on
track_counts = on
track_io_timing = on
track_functions = all
# Log settings for monitoring
log_destination = 'stderr,csvlog'
logging_collector = on
log_directory = '/var/log/postgresql'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
# Performance-related settings that affect metrics
shared_buffers = 128MB
max_connections = 100
These settings ensure that PostgreSQL tracks the necessary statistics that PostgreSQL Exporter can collect.
PostgreSQL Exporter provides a rich set of metrics, including:
Metric Category | Description | Example Metrics |
---|---|---|
Database Stats | Overall database statistics | pg_database_size_bytes , pg_stat_database_* |
Table Stats | Table-level metrics | pg_stat_user_tables_* , pg_statio_user_tables_* |
Index Stats | Index usage and efficiency | pg_stat_user_indexes_* , pg_statio_user_indexes_* |
Query Stats | Query execution metrics | pg_stat_statements_* (if enabled) |
Connection Stats | Connection pool information | pg_stat_activity_* , pg_stat_database_connections |
Replication | Replication lag and status | pg_stat_replication_* (if replication is configured) |
PostgreSQL Exporter is configured as a scrape target in Prometheus:
scrape_configs:
- job_name: 'postgresql'
static_configs:
- targets: ['postgres-exporter:9187']
Prometheus scrapes these metrics at regular intervals (default 15s), storing them for later querying and visualization.
You can directly access the raw metrics exposed by PostgreSQL Exporter at:
http://localhost:9187/metrics
This endpoint returns all available PostgreSQL metrics in Prometheus format, which is useful for debugging or exploring available metrics.
For optimal visualization, we recommend importing one of these Grafana dashboards:
To import a dashboard:
You can create custom Prometheus queries for specific PostgreSQL metrics:
# Database size
pg_database_size_bytes{datname="your_database"}
# Connection count
pg_stat_activity_count{datname="your_database"}
# Transaction rate
rate(pg_stat_database_xact_commit{datname="your_database"}[5m])
# Cache hit ratio
pg_stat_database_blks_hit / (pg_stat_database_blks_hit + pg_stat_database_blks_read)
If PostgreSQL metrics aren’t appearing in Prometheus:
docker compose ps postgres-exporter
curl http://localhost:9187/metrics
docker compose logs postgres-exporter
SELECT * FROM pg_stat_database;
https://betterstack.com/community/guides/logging/how-to-start-logging-with-postgresql/#setting-up-a-sample-database
https://www.postgresql.org/docs/current/runtime-config-logging.html
https://stackoverflow.com/questions/30848670/how-to-customize-the-configuration-file-of-the-official-postgresql-docker-image