Deploy the PostgreSQL Server Exporter as a standalone binary on Linux, macOS, or BSD systems.
Download
Download the latest release from the GitHub releases page.
Available architectures:
- linux-amd64
- linux-arm64
- linux-armv7
- linux-ppc64le
- darwin-amd64
- darwin-arm64
- freebsd-amd64
Download the binary
wget https://github.com/prometheus-community/postgres_exporter/releases/download/v0.15.0/postgres_exporter-0.15.0.linux-amd64.tar.gz
Replace v0.15.0 with the latest version and adjust the architecture as needed.Extract the archive
tar xvfz postgres_exporter-0.15.0.linux-amd64.tar.gz
cd postgres_exporter-0.15.0.linux-amd64
Move the binary to system path
sudo mv postgres_exporter /usr/local/bin/
sudo chmod +x /usr/local/bin/postgres_exporter
Verify installation
postgres_exporter --version
Building from Source
To build the latest version from source:
Clone the repository
git clone https://github.com/prometheus-community/postgres_exporter.git
cd postgres_exporter
Build the binary
This requires Go 1.21 or later. Install the binary
sudo mv postgres_exporter /usr/local/bin/
Running the Exporter
Using Environment Variables
Set the data source name
export DATA_SOURCE_NAME="postgresql://postgres_exporter:password@localhost:5432/postgres?sslmode=disable"
Or use the split format:export DATA_SOURCE_URI="localhost:5432/postgres?sslmode=disable"
export DATA_SOURCE_USER="postgres_exporter"
export DATA_SOURCE_PASS="password"
Start the exporter
The exporter will start on port 9187 by default. Verify metrics
curl http://localhost:9187/metrics
Using Password Files (Recommended)
Store database passwords in files with restricted permissions instead of environment variables.
Create a password file
sudo mkdir -p /etc/postgres_exporter
echo "your_password" | sudo tee /etc/postgres_exporter/password.txt
sudo chmod 600 /etc/postgres_exporter/password.txt
Set file ownership
sudo chown postgres_exporter:postgres_exporter /etc/postgres_exporter/password.txt
Run with password file
export DATA_SOURCE_URI="localhost:5432/postgres?sslmode=require"
export DATA_SOURCE_USER="postgres_exporter"
export DATA_SOURCE_PASS_FILE="/etc/postgres_exporter/password.txt"
postgres_exporter
Using Configuration File
For multi-target deployments:
Create a configuration file
Create /etc/postgres_exporter/postgres_exporter.yml:auth_modules:
primary:
type: userpass
userpass:
username: postgres_exporter
password: primary_password
options:
sslmode: require
replica:
type: userpass
userpass:
username: postgres_exporter
password: replica_password
options:
sslmode: require
Set file permissions
Configuration files may contain sensitive credentials. Protect them with appropriate permissions.
sudo chmod 600 /etc/postgres_exporter/postgres_exporter.yml
sudo chown postgres_exporter:postgres_exporter /etc/postgres_exporter/postgres_exporter.yml
Run with configuration file
postgres_exporter --config.file=/etc/postgres_exporter/postgres_exporter.yml
Command-Line Flags
Common flags:
postgres_exporter \
--web.listen-address=":9187" \
--web.telemetry-path="/metrics" \
--config.file="/etc/postgres_exporter/postgres_exporter.yml" \
--log.level="info" \
--log.format="logfmt"
Available Flags
| Flag | Default | Description |
|---|
--web.listen-address | :9187 | Address to listen on |
--web.telemetry-path | /metrics | Path for metrics endpoint |
--config.file | postgres_exporter.yml | Configuration file path |
--log.level | info | Log level (debug, info, warn, error) |
--log.format | logfmt | Log format (logfmt, json) |
--disable-default-metrics | false | Disable built-in metrics |
--disable-settings-metrics | false | Disable pg_settings metrics |
--extend.query-path | - | Path to custom queries YAML |
See all available flags:
Running as Non-Root User
Create a dedicated user
sudo useradd --system --no-create-home --shell /bin/false postgres_exporter
Create directories
sudo mkdir -p /etc/postgres_exporter
sudo mkdir -p /var/log/postgres_exporter
sudo chown postgres_exporter:postgres_exporter /etc/postgres_exporter
sudo chown postgres_exporter:postgres_exporter /var/log/postgres_exporter
Run as the dedicated user
sudo -u postgres_exporter DATA_SOURCE_URI="localhost:5432/postgres?sslmode=disable" \
DATA_SOURCE_USER="postgres_exporter" \
DATA_SOURCE_PASS_FILE="/etc/postgres_exporter/password.txt" \
postgres_exporter
Local Socket Connection
For local PostgreSQL installations using Unix sockets:
sudo -u postgres DATA_SOURCE_NAME="user=postgres host=/var/run/postgresql/ sslmode=disable" \
postgres_exporter
This is the recommended approach for local deployments on Debian/Ubuntu systems.
Multiple Databases
To scrape multiple databases from a single exporter instance:
export DATA_SOURCE_NAME="port=5432,port=6432"
postgres_exporter
Or use multi-target mode with a configuration file (see Multi-Target Support).
Custom Queries
To add custom metrics:
Create a queries file
Create /etc/postgres_exporter/queries.yaml with your custom queries.
Run with custom queries
postgres_exporter --extend.query-path=/etc/postgres_exporter/queries.yaml
Security Considerations
- Never run as root in production
- Use password files instead of environment variables
- Set file permissions to 600 for password and config files
- Use SSL/TLS connections to PostgreSQL (
sslmode=require)
- Limit network access to the metrics endpoint (port 9187)
- Create a dedicated database user with minimal privileges
- Consider using systemd for process management (see systemd deployment)
Verifying the Deployment
Check that metrics are being collected:
curl -s http://localhost:9187/metrics | grep pg_up
You should see:
Check for errors in the logs:
postgres_exporter --log.level=debug