Production Deployment
Install the release package, create a monitoring role, then choose systemd, Docker, or Kubernetes below. Finish by checking /metrics and adding the exporter to Prometheus.
The Command Reference lists flags, environment variables, defaults, and precedence. Command-line settings take priority over their environment aliases. Logging, action, and exporter-toolkit web flags use the command line only. Collector SQL and metric definitions belong in YAML configuration.
Process Settings
This foreground example uses .pgpass for the database password:
Auto-discovery is enabled by default. If the database is unavailable at startup, the exporter starts its HTTP listener and retries in the background. Set --fail-fast to exit when the initial database check fails.
Connection URL Sources
Set the target with --url or PG_EXPORTER_URL, or read a secret file through PG_EXPORTER_URL_FILE. See the command reference for the complete precedence rules. A URL without sslmode uses disable; set it explicitly for remote TLS connections.
To use a URL file, clear PG_EXPORTER_URL and PGURL, and omit --url. For a packaged service, also remove the default PG_EXPORTER_URL line from /etc/default/pg_exporter.
Monitoring User and Credentials
Create the Monitoring User
The main target pool allows up to three connections. Each additional discovered database allows one. Size CONNECTION LIMIT for that total and for any other exporters sharing the role. Grant CONNECT on each target database if it is not already available; optional views and extensions can need further grants. See Security.
Manage Passwords with .pgpass
Take the password out of the URL and let libpq’s .pgpass provide it:
The package runs the service as prometheus. Check its home with getent passwd prometheus and install .pgpass there. Newly created package accounts use /var/lib/prometheus; Security gives the directory and file installation commands.
TLS for the Database Connection
Protecting the HTTP Port
Beyond /metrics, the /reload, /explain, and /stat management endpoints let anyone with port access read configuration and runtime state, or trigger reloads. If the exporter is reachable from a shared network, enable TLS / Basic Auth via --web.config.file (exporter-toolkit web configuration), or restrict access at the firewall / reverse-proxy layer.
Systemd Deployment (RPM/DEB packages)
The packages install the binary at /usr/bin/pg_exporter, collectors at /etc/pg_exporter.yml, and settings at /etc/default/pg_exporter. The unit is /usr/lib/systemd/system/pg_exporter.service for RPM and /lib/systemd/system/pg_exporter.service for DEB:
Environment file /etc/default/pg_exporter (package defaults):
Replace the default URL with your monitoring user and target. Add settings with environment aliases, such as PG_EXPORTER_DISABLE_INTRO, directly to this file. Put command-line-only flags in PG_EXPORTER_OPTS. The package marks the environment and collector files as configuration files to preserve local edits during upgrades.
Start and verify the service:
Check that the metrics include pg_up 1. To follow new log entries, run sudo journalctl -u pg_exporter -f.
After editing /etc/default/pg_exporter, run sudo systemctl restart pg_exporter. For changes to collector YAML only, reload it directly with systemctl or HTTP:
Docker Deployment
Create pg_exporter.url with your database connection URL. This example expects a TLS certificate for db.example.com and a CA file named ca.crt:
Keep the URL file private, then mount it and the CA into the container:
Docker Compose:
The image includes the default collector configuration. To replace it, add a read-only mount at /etc/pg_exporter.yml. The scratch image includes the system CA bundle and license, but no shell. Publicly trusted certificates can use the bundled roots; mount your private CA and set sslrootcert when using a private PKI with sslmode=verify-ca or verify-full.
Check the running container:
Publish port 9630 on an address reachable by Prometheus and restrict its network access. If Prometheus runs on the Docker host, use -p 127.0.0.1:9630:9630.
Kubernetes Deployment
Create a Secret from the URL and CA files above, in the namespace where the exporter will run:
Save this manifest as pg-exporter.yaml:
Both probes use /version to check that the exporter can serve HTTP. /liveness and /readiness are aliases of /up: they report database health. Using them here would restart the exporter or remove its Service endpoint during a database outage, when its metrics are most useful.
Apply the manifest and check the rollout:
While port forwarding runs, check /metrics from another terminal. Adjust the resource requests and limits after measuring your collector workload. If HTTP authentication is enabled, configure the probes to authenticate as well.
Auto-Discovery
Auto-discovery adds eligible databases from the same PostgreSQL instance. Use an include list to limit it to selected databases:
The URL database is always monitored. Include/exclude lists apply only to additional databases; exclusion wins if a name appears in both lists. An empty include list allows all otherwise eligible databases.
- Cluster-level collectors (
tags: [cluster]) run once on the main connection. - Database-level collectors also run on discovered databases. Bundled collectors use
datnamelabels to identify results; custom SQL must supply its declared labels. - Each scrape checks for created or dropped databases.
--no-auto-discoveryrestricts collection to the URL database.
Monitoring PgBouncer
Set the database name in the URL to pgbouncer to switch to PgBouncer mode (this is what triggers the detection):
Supply this account through PgBouncer authentication and add it to stats_users. Store its password in .pgpass. In this mode, the exporter runs the PgBouncer SHOW collectors. Run separate exporter processes for PostgreSQL and PgBouncer, with separate ports.
The default self-metric namespace is pgbouncer when no namespace is set. The package environment file sets PG_EXPORTER_NAMESPACE=pg; change it to pgbouncer for a packaged PgBouncer service. This setting affects built-in self-metrics, not YAML-defined metric names.
Prometheus Scraping and Alerting
Scrape Configuration
Kubernetes service discovery:
Start with a 15-second interval and a 10-second scrape timeout. A collector result can be reused until its ttl expires; many bundled collectors use 10 seconds. Measure total scrape duration before reducing the interval or increasing the number of databases.
Alert Rules
These examples use the default pg self-metric namespace:
Role-Based Traffic Routing
The /primary, /replica, and /read health-check endpoints can serve directly as health probes for HAProxy and similar load balancers, enabling primary/replica read-write splitting. Endpoint semantics and a complete HAProxy example are in the API Reference.