PostgreSQL: read-only user setup

What we scrape: Connection counts, transaction rates, lock contention, replication lag, autovacuum / bloat health, WAL throughput, and per-database statistics.

The official [`postgres_exporter` upstream](https://github.com/prometheus-community/postgres_exporter#running-as-non-superuser) recommends `pg_monitor` for PostgreSQL ≥ 10 — a predefined role that grants exactly the SELECTs the exporter needs across `pg_stat_*`, `pg_stat_activity`, and replication views (and nothing else: no table data, no DDL). The explicit `GRANT CONNECT` is the second half of that recommendation — `pg_monitor` does NOT grant database connect, and PostgreSQL installs that have revoked `CONNECT` from `PUBLIC` will otherwise refuse the agent at login time. Run the SQL below as a superuser (typically `postgres`), then paste the DSN into the Add Exporter form.

  1. 1. Create the login role, grant pg_monitor, allow CONNECT

    Replace `CHANGE_ME_STRONG_PASSWORD` with a freshly generated value. `CONNECTION LIMIT 5` keeps a runaway agent from exhausting your `max_connections` budget. The explicit `GRANT CONNECT` matches the upstream README — `pg_monitor` itself does not include the database-level `CONNECT` privilege, so hardened installs that have revoked `CONNECT` from `PUBLIC` need this line.

    SQL
    CREATE ROLE prometheus_exporter
      LOGIN
      PASSWORD 'CHANGE_ME_STRONG_PASSWORD'
      CONNECTION LIMIT 5;
    
    GRANT pg_monitor TO prometheus_exporter;
    GRANT CONNECT ON DATABASE postgres TO prometheus_exporter;
  2. 2. (Optional) Enable per-query stats

    If you want the `pg_stat_statements_*` dashboards to populate, enable the extension on every database the agent will scrape. `pg_monitor` already grants SELECT on the view, so no extra GRANT is needed.

    SQL
    CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
  3. 3. Verify the user can read stats but cannot read data

    The first query must return a non-empty result. The second must fail with `permission denied` — proof that the role can see metrics and nothing more.

    bash
    psql "postgresql://prometheus_exporter:CHANGE_ME_STRONG_PASSWORD@HOST:5432/postgres?sslmode=require" \
      -c 'SELECT datname, numbackends FROM pg_stat_database LIMIT 5;'
    
    psql "postgresql://prometheus_exporter:CHANGE_ME_STRONG_PASSWORD@HOST:5432/postgres?sslmode=require" \
      -c 'SELECT * FROM pg_authid LIMIT 1;'

The agent connects with the DSN you paste into the form. We strip the password from any UI display and store it encrypted at rest.