# PostgreSQL Use application instrumentation for PostgreSQL query spans and a separate `postgresql` receiver for database statistics. Contrib 0.160.0 marks metrics beta and its optional query-event logs development. This guide starts with metrics; it does not enable query text or execution-plan collection. ## Instrument application queries For a JVM application using JDBC, attach the agent from the [Java guide](../languages/java.md), keeping the application's service identity. For other runtimes use the matching PostgreSQL driver instrumentation from the [language guides](README.md#instrument-the-calling-application). Initialize it before the application's connection pool or driver is created. Run a read-only operation through the application. A CLI `SELECT 1` checks the connection but does not exercise your application's tracing. Client span time can include network and driver work; it is not a server execution plan. ## Create a monitoring role Have a database administrator create a login in the appropriate cluster. This example uses PostgreSQL's built-in monitoring role without superuser access: ```sql CREATE ROLE otel_monitor LOGIN; GRANT pg_monitor TO otel_monitor; GRANT CONNECT ON DATABASE postgres TO otel_monitor; GRANT CONNECT ON DATABASE appdb TO otel_monitor; ``` Set its password using your secret-management workflow; in an interactive `psql` administrator session, `\password otel_monitor` prompts without putting the password into SQL history. Allow its source address in the server's authentication/network policy. `pg_monitor` exposes monitoring information: grant it only to this dedicated identity, not the normal application login. The receiver connects to `postgres` for discovery and server queries even when you restrict per-database collection. Managed-service internal databases may need `exclude_databases`; use names appropriate to your provider. ## Add server metrics Merge this **partial fragment** into [common setup](../../common.md), retaining all existing pipelines. Requires Contrib's `postgresql` and `resource` components. ```yaml receivers: postgresql/app: endpoint: postgres.internal:5432 username: otel_monitor password: ${env:POSTGRESQL_MONITOR_PASSWORD} databases: [appdb] collection_interval: 30s tls: insecure: false insecure_skip_verify: false ca_file: /etc/otel/certs/postgres-ca.pem processors: resource/postgresql: attributes: - key: service.name value: postgresql-app action: upsert - key: deployment.environment.name value: production action: upsert service: pipelines: metrics/postgresql: receivers: [postgresql/app] processors: [memory_limiter, resource/postgresql, batch] exporters: [otlp_http/corridora] ``` Supply the password and CA inside the Collector runtime. Add a separate receiver instance for another cluster. Do not run several identical scrapers against the same instance unless you intend to collect duplicate measurements. ## Verify Use a protected password file or an interactive password prompt with `psql`: ```sh psql 'host=postgres.internal port=5432 user=otel_monitor dbname=postgres sslmode=verify-full sslrootcert=/etc/otel/certs/postgres-ca.pem' \ -c 'SELECT datname, numbackends FROM pg_stat_database;' ``` Repeat against `appdb` to check access. Validate and run the merged Collector configuration. After two collection intervals, inspect service `postgresql-app` in Corridora Metrics and search the `postgresql.` prefix. Compare a backend connection count with `pg_stat_database`, allowing for collection timing. Then exercise an application request and verify its PostgreSQL client span. Permission errors for another database are not proof that all collection failed. Limit `databases`, exclude managed internal databases as needed, and inspect which individual scrape failed. For missing traces, check the application SDK. Continue with [verification](../../verification.md). Optional query samples/top-query events need separate privacy review, receiver event configuration, and a **logs pipeline containing the same receiver**. Enabling events in a metrics-only pipeline does not route logs to Corridora. `pg_stat_statements` and additional grants are needed for relevant query features; ordinary metrics do not imply that they are enabled. Source basis: PostgreSQL receiver README/configuration, Contrib 0.160.0; PostgreSQL 18 Predefined Roles; OpenTelemetry Java JDBC support. Reviewed 2026-09-28.