postgres-exporter is a free and open-source Prometheus exporter for PostgreSQL metrics. Postgres Exporter connects to PostgreSQL databases and exports metrics for Prometheus monitoring, essential for database observability
1. Prerequisites
2. Supported Operating Systems
This guide supports installation on:
3. Installation
RHEL/CentOS/Rocky Linux/AlmaLinux
# Install EPEL repository if needed
sudo dnf install -y epel-release
# Install postgres-exporter
sudo dnf install -y prometheus-postgres-exporter
# Enable and start service
sudo systemctl enable --now postgres_exporter
# Configure firewall
sudo firewall-cmd --permanent --add-port=9187/tcp
sudo firewall-cmd --reload
# Verify installation
postgres_exporter --version
Debian/Ubuntu
# Update package index
sudo apt update
# Install postgres-exporter
sudo apt install -y prometheus-postgres-exporter
# Enable and start service
sudo systemctl enable --now postgres_exporter
# Configure firewall
sudo ufw allow 9187
# Verify installation
postgres_exporter --version
Arch Linux
# Install postgres-exporter
sudo pacman -S prometheus-postgres-exporter
# Enable and start service
sudo systemctl enable --now postgres_exporter
# Verify installation
postgres_exporter --version
Alpine Linux
# Install postgres-exporter
apk add --no-cache prometheus-postgres-exporter
# Enable and start service
rc-update add postgres_exporter default
rc-service postgres_exporter start
# Verify installation
postgres_exporter --version
openSUSE/SLES
# Install postgres-exporter
sudo zypper install -y prometheus-postgres-exporter
# Enable and start service
sudo systemctl enable --now postgres_exporter
# Configure firewall
sudo firewall-cmd --permanent --add-port=9187/tcp
sudo firewall-cmd --reload
# Verify installation
postgres_exporter --version
macOS
# Using Homebrew
brew install prometheus-postgres-exporter
# Start service
brew services start prometheus-postgres-exporter
# Verify installation
postgres_exporter --version
FreeBSD
# Using pkg
pkg install prometheus-postgres-exporter
# Enable in rc.conf
echo 'postgres_exporter_enable="YES"' >> /etc/rc.conf
# Start service
service postgres_exporter start
# Verify installation
postgres_exporter --version
Windows
# Using Chocolatey
choco install prometheus-postgres-exporter
# Or using Scoop
scoop install prometheus-postgres-exporter
# Verify installation
postgres_exporter --version
Initial Configuration
Basic Configuration
# Create configuration directory
sudo mkdir -p /etc/prometheus-postgres-exporter
# Set up basic configuration
# See official documentation for detailed configuration options
# Test configuration
postgres_exporter --version
5. Service Management
systemd (RHEL, Debian, Ubuntu, Arch, openSUSE)
# Enable service
sudo systemctl enable postgres_exporter
# Start service
sudo systemctl start postgres_exporter
# Stop service
sudo systemctl stop postgres_exporter
# Restart service
sudo systemctl restart postgres_exporter
# Check status
sudo systemctl status postgres_exporter
# View logs
sudo journalctl -u postgres_exporter -f
OpenRC (Alpine Linux)
# Enable service
rc-update add postgres_exporter default
# Start service
rc-service postgres_exporter start
# Stop service
rc-service postgres_exporter stop
# Restart service
rc-service postgres_exporter restart
# Check status
rc-service postgres_exporter status
rc.d (FreeBSD)
# Enable in /etc/rc.conf
echo 'postgres_exporter_enable="YES"' >> /etc/rc.conf
# Start service
service postgres_exporter start
# Stop service
service postgres_exporter stop
# Restart service
service postgres_exporter restart
# Check status
service postgres_exporter status
launchd (macOS)
# Using Homebrew services
brew services start prometheus-postgres-exporter
brew services stop prometheus-postgres-exporter
brew services restart prometheus-postgres-exporter
# Check status
brew services list | grep prometheus-postgres-exporter
Windows Service Manager
# Start service
net start postgres_exporter
# Stop service
net stop postgres_exporter
# Using PowerShell
Start-Service postgres_exporter
Stop-Service postgres_exporter
Restart-Service postgres_exporter
# Check status
Get-Service postgres_exporter
Advanced Configuration
See the official documentation for advanced configuration options.
Reverse Proxy Setup
nginx Configuration
upstream prometheus-postgres-exporter_backend {
server 127.0.0.1:9187;
}
server {
listen 80;
server_name prometheus-postgres-exporter.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name prometheus-postgres-exporter.example.com;
ssl_certificate /etc/ssl/certs/prometheus-postgres-exporter.example.com.crt;
ssl_certificate_key /etc/ssl/private/prometheus-postgres-exporter.example.com.key;
location / {
proxy_pass http://prometheus-postgres-exporter_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Apache Configuration
<VirtualHost *:80>
ServerName prometheus-postgres-exporter.example.com
Redirect permanent / https://prometheus-postgres-exporter.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName prometheus-postgres-exporter.example.com
SSLEngine on
SSLCertificateFile /etc/ssl/certs/prometheus-postgres-exporter.example.com.crt
SSLCertificateKeyFile /etc/ssl/private/prometheus-postgres-exporter.example.com.key
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:9187/
ProxyPassReverse / http://127.0.0.1:9187/
</VirtualHost>
HAProxy Configuration
frontend prometheus-postgres-exporter_frontend
bind *:80
bind *:443 ssl crt /etc/ssl/certs/prometheus-postgres-exporter.pem
redirect scheme https if !{ ssl_fc }
default_backend prometheus-postgres-exporter_backend
backend prometheus-postgres-exporter_backend
balance roundrobin
server prometheus-postgres-exporter1 127.0.0.1:9187 check
Security Configuration
Basic Security Setup
# Set appropriate permissions
sudo chown -R prometheus-postgres-exporter:prometheus-postgres-exporter /etc/prometheus-postgres-exporter
sudo chmod 750 /etc/prometheus-postgres-exporter
# Configure firewall
sudo firewall-cmd --permanent --add-port=9187/tcp
sudo firewall-cmd --reload
# Enable SELinux policies (if applicable)
sudo setsebool -P httpd_can_network_connect on
Database Setup
See official documentation for database configuration requirements.
Performance Optimization
System Tuning
# Basic system tuning
echo 'net.core.somaxconn = 65535' | sudo tee -a /etc/sysctl.conf
echo 'net.ipv4.tcp_max_syn_backlog = 65535' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
Monitoring
Basic Monitoring
# Check service status
sudo systemctl status postgres_exporter
# View logs
sudo journalctl -u postgres_exporter -f
# Monitor resource usage
top -p $(pgrep prometheus-postgres-exporter)
9. Backup and Restore
Backup Script
#!/bin/bash
# Basic backup script
BACKUP_DIR="/backup/prometheus-postgres-exporter"
DATE=$(date +%Y%m%d_%H%M%S)
mkdir -p "$BACKUP_DIR"
tar -czf "$BACKUP_DIR/prometheus-postgres-exporter-backup-$DATE.tar.gz" /etc/prometheus-postgres-exporter /var/lib/prometheus-postgres-exporter
echo "Backup completed: $BACKUP_DIR/prometheus-postgres-exporter-backup-$DATE.tar.gz"
Restore Procedure
# Stop service
sudo systemctl stop postgres_exporter
# Restore from backup
tar -xzf /backup/prometheus-postgres-exporter/prometheus-postgres-exporter-backup-*.tar.gz -C /
# Start service
sudo systemctl start postgres_exporter
6. Troubleshooting
Common Issues
1. Service won't start:
# Check logs
sudo journalctl -u postgres_exporter -n 100
sudo tail -f /var/log/prometheus-postgres-exporter/prometheus-postgres-exporter.log
# Check configuration
postgres_exporter --version
# Check permissions
ls -la /etc/prometheus-postgres-exporter
2. Connection issues:
# Check if service is listening
sudo ss -tlnp | grep 9187
# Test connectivity
telnet localhost 9187
# Check firewall
sudo firewall-cmd --list-all
3. Performance issues:
# Check resource usage
top -p $(pgrep prometheus-postgres-exporter)
# Check disk I/O
iotop -p $(pgrep prometheus-postgres-exporter)
# Check connections
ss -an | grep 9187
Integration Examples
Docker Compose Example
version: '3.8'
services:
prometheus-postgres-exporter:
image: prometheus-postgres-exporter:latest
ports:
- "9187:9187"
volumes:
- ./config:/etc/prometheus-postgres-exporter
- ./data:/var/lib/prometheus-postgres-exporter
restart: unless-stopped
Maintenance
Update Procedures
# RHEL/CentOS/Rocky/AlmaLinux
sudo dnf update prometheus-postgres-exporter
# Debian/Ubuntu
sudo apt update && sudo apt upgrade prometheus-postgres-exporter
# Arch Linux
sudo pacman -Syu prometheus-postgres-exporter
# Alpine Linux
apk update && apk upgrade prometheus-postgres-exporter
# openSUSE
sudo zypper update prometheus-postgres-exporter
# FreeBSD
pkg update && pkg upgrade prometheus-postgres-exporter
# Always backup before updates
tar -czf /backup/prometheus-postgres-exporter-pre-update-$(date +%Y%m%d).tar.gz /etc/prometheus-postgres-exporter
# Restart after updates
sudo systemctl restart postgres_exporter
Regular Maintenance
# Log rotation
sudo logrotate -f /etc/logrotate.d/prometheus-postgres-exporter
# Clean old logs
find /var/log/prometheus-postgres-exporter -name "*.log" -mtime +30 -delete
# Check disk usage
du -sh /var/lib/prometheus-postgres-exporter
Additional Resources
---
Note: This guide is part of the HowToMgr collection. Always refer to official documentation for the most up-to-date information.