Prerequisites
Before starting, ensure you have:- A running PostgreSQL server (versions 13-18 recommended)
- Docker installed, or access to download the binary
- Network access from the exporter to PostgreSQL (default port 5432)
- PostgreSQL user credentials with monitoring permissions
This guide uses Docker for the fastest setup. See Installation for other methods.
Launch in 3 Steps
Start a Test PostgreSQL Database (Optional)
If you don’t have a PostgreSQL instance available, start one for testing:This creates a PostgreSQL 16 instance on
localhost:5432 with:- Username:
postgres - Password:
password - Database:
postgres
Run the PostgreSQL Exporter
Start the exporter container with connection parameters:
Understanding the Configuration
Understanding the Configuration
DATA_SOURCE_URI: PostgreSQL connection string in formathost:port/database?optionsDATA_SOURCE_USER: PostgreSQL username with monitoring accessDATA_SOURCE_PASS: User password--net=host: Shares host network (simplifies local testing)- Port 9187: Default metrics endpoint (automatically exposed)
Secure Production Configuration
For production deployments, use file-based secrets instead of environment variables:The container runs as uid/gid 65534 (nobody). Ensure mounted password files are readable by this user.
Verify Metrics Collection
Test the metrics endpoint to confirm the exporter is working:You should see Prometheus-formatted metrics including:
Key Metrics to Look For
Key Metrics to Look For
pg_up{} 1: Exporter successfully connected to PostgreSQLpg_static{version=...}: PostgreSQL version informationpg_stat_database_*: Database-level statistics (enabled by default)pg_exporter_last_scrape_error 0: No collection errors
Configure Prometheus Scraping
Add the exporter to your Prometheus configuration:Replace
localhost:9187 with your exporter’s hostname/IP if running on different servers.Enable Optional Collectors
By default, the exporter enables essential collectors. Enable additional collectors using command-line flags:Useful Optional Collectors
Useful Optional Collectors
--collector.stat_statements: Top queries by execution time (requirespg_stat_statementsextension)--collector.long_running_transactions: Detect queries exceeding thresholds--collector.database_wraparound: Monitor transaction ID wraparound risk--collector.stat_checkpointer: Checkpoint performance metrics--collector.postmaster: PostgreSQL server process information
PostgreSQL User Setup
Create a dedicated monitoring user with appropriate permissions:Verify Full Setup
Run these checks to confirm everything is working:Common Issues
pg_up shows 0
pg_up shows 0
Connection to PostgreSQL failed. Check:
- PostgreSQL is running and accessible:
psql -h localhost -U postgres - Credentials are correct in
DATA_SOURCE_USERandDATA_SOURCE_PASS - Network connectivity:
telnet postgres-host 5432 pg_hba.confallows connections from exporter IP- Firewall rules allow port 5432
No metrics for specific collectors
No metrics for specific collectors
- Check collector is enabled:
docker exec postgres-exporter ps aux - Verify PostgreSQL version compatibility (some collectors require PostgreSQL 10+)
- Check user permissions:
SELECT * FROM pg_stat_activityshould return results
'permission denied' errors in logs
'permission denied' errors in logs
- For PostgreSQL 10+: Run
GRANT pg_monitor TO postgres_exporter; - For PostgreSQL 9.x: Create helper functions (see PostgreSQL User Setup above)
- Check
SEARCH_PATHincludes monitoring schema
High CPU/memory usage
High CPU/memory usage
- Disable expensive collectors you don’t need
- Increase
PG_EXPORTER_COLLECTION_TIMEOUTif queries are timing out - For
stat_statementscollector, reduce--collector.stat_statements.limit
Next Steps
Installation Methods
Binary installation, Kubernetes, and from-source builds
Configuration Reference
Complete guide to all flags, collectors, and environment variables
Multi-Target Setup
Monitor multiple PostgreSQL instances from one exporter
Prometheus Queries
Example PromQL queries for common monitoring scenarios