Deployment
Use this page to deploy pg_exporter in production.
It describes process arguments, environment variables, monitoring users, credentials, and deployment with systemd, Docker, or Kubernetes.
It also describes pgBouncer monitoring, auto-discovery, Prometheus scrapes, and alerts.
The exporter reads process configuration from two sources. Command-line arguments take precedence:
- Command-line arguments (
--url,--config, …) - Environment variables (every flag has a corresponding
PG_EXPORTER_*variable)
YAML collector definitions control metric collection.
The default file is /etc/pg_exporter.yml. You can also use a configuration directory.
See the Configuration reference.
Command-Line Arguments
The full flag list from pg_exporter --help:
These behaviors affect deployment:
- Startup policy: startup is non-blocking by default. If the database is unreachable, HTTP endpoints still start. A background probe retries until recovery. To fail when the database is unreachable, set
--fail-fast. For example, use this option when systemd or an orchestrator controls restarts. - Telemetry path validation:
--web.telemetry-pathrejects empty paths, conflicts with built-in endpoints, and non-canonical paths such as//metrics. v1.5.0 also verifies that the active HTTP mux selects the metrics handler, preventing escaped paths from returning the landing page or a redirect. Prefer an unescaped path such as/metrics.
Connection URL Sources
The exporter checks these sources in order, skipping empty command-line and environment values:
--url/-ucommand-line argumentPG_EXPORTER_URLenvironment variablePGURLenvironment variable- Content of the file pointed to by
PG_EXPORTER_URL_FILE(fits container Secret mounts) - Default
postgresql:///?sslmode=disable(local-first, fits same-host deployment)
Once PG_EXPORTER_URL_FILE is selected, an unreadable, empty, or whitespace-only file prevents startup; it does not fall back to the local default.
v1.5.0 validates URL syntax, percent escapes, and query parameter names. Names cannot be empty or contain whitespace or =, and URLs cannot contain an unescaped #. Invalid input stops startup with a fixed diagnostic that omits the raw connection string, preventing password disclosure or silently discarded TLS settings. Valid URL passwords in user information and password= parameters are redacted in logs.
If the URL omits sslmode, the exporter appends sslmode=disable.
At startup, it also clears libpq service-file environment variables, such as PGSERVICE / PGSERVICEFILE, and logs this action.
Service files can override an explicit connection target.
Clearing these variables ensures that the exporter connects to the URL shown in the log.
Monitoring User and Credentials
Create the Monitoring User
On PostgreSQL 10–13, an administrator must also run the following grant in the exporter’s main connection database to read subscription OIDs. Granting it only in the database owning the subscription is insufficient. This does not expose the password-bearing subconninfo column. PostgreSQL 14+ needs no extra grant under standard catalog ACLs.
Size CONNECTION LIMIT for the number of discovered databases. A limit that is too low can prevent connections to additional databases. Keep a connection limit to prevent connection exhaustion if the configuration is incorrect.
The exporter normally uses one to a few connections. With auto-discovery, it uses one connection per database.
Manage Passwords with .pgpass
Remove the password from the URL. Use libpq’s .pgpass file to supply it:
Packaged services run as the prometheus system user.
From v1.4.0, its HOME is /var/lib/prometheus. Libpq looks for ~/.pgpass there.
The package does not create this directory. Before you use .pgpass, run:
install -d -o prometheus -g prometheus /var/lib/prometheus
TLS for the Database Connection
Protecting the HTTP Port
The HTTP port provides /metrics and the management endpoints /reload, /explain, and /stat.
Anyone with access to the port can read configuration and runtime state, or start a reload.
On a shared network, enable TLS / Basic Auth with --web.config.file (exporter-toolkit web configuration).
You can also restrict access through a firewall or reverse proxy.
Systemd Deployment (RPM/DEB packages)
RPM/DEB packages include a service unit and an environment file. After installation, edit the environment file and start the service:
Environment file /etc/default/pg_exporter (package defaults):
Each command-line flag has an environment variable. Add the variables you need, such as PG_EXPORTER_DISABLE_INTRO.
The file uses the noreplace package option. Upgrades do not overwrite your changes.
Common operations:
Docker Deployment
Docker Compose:
The official image is built from scratch. It contains no system CA certificates.
To connect to remote PostgreSQL with sslmode=verify-ca / verify-full, mount a CA certificate and set sslrootcert to its path.
Without the certificate, TLS verification cannot complete.
Kubernetes Deployment
To keep the connection string out of environment variables, set PG_EXPORTER_URL_FILE to a Secret-mounted file.
Auto-Discovery
Auto-discovery is enabled by default. It lets one exporter instance monitor every database in the target PostgreSQL instance:
Behavior:
- Cluster-level collectors (
tags: [cluster]) run once on the primary connection - Database-level collectors run on every discovered database, with metrics distinguished by the
datnamelabel - Newly created or dropped databases are added or removed during subsequent prechecks
- v1.5.0 uses
lib/pqto resolve the actual database identity. Replacing the database name removes conflictingdbname/databasequery parameters and preserves special characters, preventing discovered connections from scraping the same database - Database scrapes remain sequential. Allow enough scrape timeout for prechecks and queries across all databases
Monitoring pgBouncer
Set the database name in the URL to pgbouncer. This name activates pgBouncer mode:
In pgBouncer mode, the exporter uses the pgbouncer metric prefix.
It runs only pgBouncer collectors, such as SHOW STATS / SHOW POOLS.
Usually, you run one exporter for PostgreSQL and another for pgBouncer. Give each exporter a different port.
Prometheus Scraping and Alerting
Scrape Configuration
Kubernetes service discovery:
Set the scrape interval equal to or greater than the common collector ttl.
Most collectors in the default bundle use 10 seconds.
More frequent scrapes return cached results until the TTL expires.
Alert Rules
The following rules use available metrics:
Role-Based Traffic Routing
Use /primary, /replica, and /read as health probes for HAProxy or similar load balancers.
These endpoints support primary/replica routing to separate read and write traffic.
For endpoint behavior and a complete HAProxy example, see the API Reference.
Open-source Nginx does not support active HTTP health checks outside the traffic connection.
The health_check directive requires NGINX Plus.
To route PostgreSQL traffic by role, use HAProxy or a solution such as Patroni + vip-manager.
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)