# @opentelemetry/instrumentation-pg

> OpenTelemetry instrumentation for `pg` and `pg-pool` database client for PostgreSQL

Latest version **0.74.0** (published 2026-08-31) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @opentelemetry/instrumentation-pg
pnpm add @opentelemetry/instrumentation-pg
yarn add @opentelemetry/instrumentation-pg
bun add @opentelemetry/instrumentation-pg
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.74.0 |
| Published | 2026-08-31 |
| First published | 2021-03-05 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | ^18.19.0 \|\| >=20.6.0 |
| Dependencies | 6 |
| Unpacked size | 147.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 922 |
| Author | OpenTelemetry Authors |
| Maintainers | dyladan, pichlermarc, overbalance, npmjs-account, trentm, martinkuba |
| Keywords | instrumentation, nodejs, opentelemetry, pg, plugin, postgres, postgresql, profiling, tracing |

## Links

- npm: https://www.npmjs.com/package/@opentelemetry/instrumentation-pg
- Repository: https://github.com/open-telemetry/opentelemetry-js-contrib
- Homepage: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/instrumentation-pg#readme
- Issues: https://github.com/open-telemetry/opentelemetry-js-contrib/issues
- npm.io page: https://npm.io/package/@opentelemetry/instrumentation-pg

## Dependencies (6)

- [@types/pg](https://npm.io/package/@types/pg.md) 8.15.6
- [@types/pg-pool](https://npm.io/package/@types/pg-pool.md) 2.0.7
- [@opentelemetry/core](https://npm.io/package/@opentelemetry/core.md) ^2.9.0
- [@opentelemetry/sql-common](https://npm.io/package/@opentelemetry/sql-common.md) ^0.42.0
- [@opentelemetry/instrumentation](https://npm.io/package/@opentelemetry/instrumentation.md) ^0.222.0
- [@opentelemetry/semantic-conventions](https://npm.io/package/@opentelemetry/semantic-conventions.md) ^1.34.0

## Recent versions

- 0.74.0 (latest) — 2026-08-31
- 0.73.0 — 2026-07-23
- 0.72.0 — 2026-07-03
- 0.71.0 — 2026-06-11
- 0.70.0 — 2026-05-13
- 0.69.0 — 2026-05-06
- 0.68.0 — 2026-04-29
- 0.67.0 — 2026-04-17
- 0.66.0 — 2026-03-26
- 0.65.0 — 2026-03-04
- 0.64.0 — 2026-02-17
- 0.63.0 — 2026-01-22
- 0.62.0 — 2026-01-15
- 0.61.2 — 2025-12-17
- 0.61.1 — 2025-11-24
- … 62 more at https://npm.io/package/@opentelemetry/instrumentation-pg/versions

## README

# OpenTelemetry Postgres Instrumentation for Node.js

[![NPM Published Version][npm-img]][npm-url]
[![Apache License][license-image]][license-image]

This module provides automatic instrumentation for the [`pg`](https://github.com/brianc/node-postgres)module.

If total installation size is not constrained, it is recommended to use the [`@opentelemetry/auto-instrumentations-node`](https://www.npmjs.com/package/@opentelemetry/auto-instrumentations-node) bundle with [@opentelemetry/sdk-node](https://www.npmjs.com/package/@opentelemetry/sdk-node) for the most seamless instrumentation experience.

Compatible with OpenTelemetry JS API and SDK `1.0+`.

## Installation

```bash
npm install --save @opentelemetry/instrumentation-pg
```

### Supported Versions

- [`pg`](https://www.npmjs.com/package/pg) versions `>=8.0.3 <9`
- [`pg-pool`](https://www.npmjs.com/package/pg-pool) versions `>=2.0.0 <4`

## Usage

```js
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { PgInstrumentation } = require('@opentelemetry/instrumentation-pg');

const sdk = new NodeSDK({
  instrumentations: [
    new PgInstrumentation(),
  ],
});
sdk.start();
process.once('beforeExit', async () => { await sdk.shutdown(); });
```

PgInstrumentation contains both pg and [`pg.Pool`](https://node-postgres.com/api/pool) so it will be instrumented automatically.

### Span Types Created

This instrumentation creates the following span types:

| Span Name | Description | When Created |
| --------- | ----------- | ------------ |
| `pg.query:<OPERATION> <database>` | Database query execution | When `client.query()` is called |
| `pg.connect` | Client connection to database | When `new Client().connect()` is called directly |
| `pg-pool.connect` | Pool connection acquisition wait time | When acquiring a connection from `pg-pool` |

The `pg-pool.connect` spans measure the time spent waiting to acquire a connection from the pool. This can be valuable for identifying connection pool exhaustion or sizing issues. However, in high-throughput scenarios where connections are readily available, these spans may add noise with minimal diagnostic value. Consider using the `requireParentSpan` option or sampling strategies if pool connect spans become excessive.

### PostgreSQL Instrumentation Options

PostgreSQL instrumentation has few options available to choose from. You can set the following:

| Options | Type | Description |
| ------- | ---- | ----------- |
| [`enhancedDatabaseReporting`](./src/types.ts#L30) | `boolean` | If true, additional information about query parameters and results will be attached (as `attributes`) to spans representing database operations |
| `requestHook` | `PgInstrumentationExecutionRequestHook` (function) | Function for adding custom span attributes using information about the query being issued and the db to which it's directed |
| `responseHook` | `PgInstrumentationExecutionResponseHook` (function) | Function for adding custom span attributes from db response |
| `requireParentSpan` | `boolean` | If true, requires a parent span to create new spans (default false) |
| `addSqlCommenterCommentToQueries` | `boolean` | If true, adds [sqlcommenter](https://github.com/open-telemetry/opentelemetry-sqlcommenter) specification compliant comment to queries with tracing context (default false). _NOTE: A comment will not be added to queries that already contain `--` or `/* ... */` in them, even if these are not actually part of comments_ |
| `ignoreConnectSpans` | `boolean` | If true, `pg.connect` and `pg-pool.connect` spans will not be created. Query spans and pool metrics are still recorded (default false) |

## Semantic Conventions

The `@opentelemetry/instrumentation-pg` versions 0.72.0 and later emit the stable v1.34.0+ semantic conventions.

### Attributes collected

| Attribute             | Short Description                                                                          |
| --------------------- | ------------------------------------------------------------------------------------------ |
| `db.system.name`      | The database management system (DBMS) product as identified by the client instrumentation. |
| `db.namespace`        | The name of the database, fully qualified within the server address and port.              |
| `db.query.text`       | The database query being executed.                                                         |
| `db.operation.name`   | The name of the operation or command being executed.                                       |
| `server.address`      | Remote hostname or similar.                                                                |
| `server.port`         | Remote port number.                                                                        |
| `error.type`          | Describes a class of error the operation ended with.                                       |

> [!NOTE]
> `db.collection.name` is not collected. The `pg` driver does not expose the table
> name separately, and the OpenTelemetry specification advises against parsing
> `db.query.text` when the database supports queries touching multiple
> collections in non-batch operations, which is the case for PostgreSQL.

Metrics Exported:

- [`db.client.operation.duration`](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-metrics.md#metric-dbclientoperationduration)
- [`db.client.connection.count`](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-metrics.md#metric-dbclientconnectioncount)
- [`db.client.connection.max`](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-metrics.md#metric-dbclientconnectionmax)
- [`db.client.connection.pending_requests`](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/database-metrics.md#metric-dbclientconnectionpending_requests)

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
- For more about OpenTelemetry JavaScript: <https://github.com/open-telemetry/opentelemetry-js>
- For help or feedback on this project, join us in [GitHub Discussions][discussions-url]

## License

Apache 2.0 - See [LICENSE][license-url] for more information.

[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions
[license-url]: https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/LICENSE
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
[npm-url]: https://www.npmjs.com/package/@opentelemetry/instrumentation-pg
[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Finstrumentation-pg.svg

---
_Source: https://npm.io/package/@opentelemetry/instrumentation-pg · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
