# aws-xray-sdk-postgres

> AWS X-Ray Patcher for Postgres (Javascript)

Latest version **3.12.0** (published 2025-11-20) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install aws-xray-sdk-postgres
pnpm add aws-xray-sdk-postgres
yarn add aws-xray-sdk-postgres
bun add aws-xray-sdk-postgres
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 3.12.0 |
| Published | 2025-11-20 |
| First published | 2017-03-30 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 14.x |
| Dependencies | 1 |
| Unpacked size | 35.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 280 |
| Author | Amazon Web Services |
| Maintainers | aws-sdk-team, nosovsky, amzn-oss, aws-sdk-bot, awsxray, aws-iot-sdk, aws-amplify-ops, zheallan, mohit0193, rishabh6788, weicongs, sayaligaikawad, peterzhuamazon, forrest-not-gump, cayblood, evertonfraga, osa-publish, amazon-q-cli-bot, yogiraj07, gkwicker.amazon, haotianw465, awssandra, jsq, armiros |
| Keywords | amazon, api, aws, postgres, xray, x-ray, x ray |

## Links

- npm: https://www.npmjs.com/package/aws-xray-sdk-postgres
- Repository: https://github.com/aws/aws-xray-sdk-node.git#master
- Homepage: https://github.com/aws/aws-xray-sdk-node/tree/master#readme
- Issues: https://github.com/aws/aws-xray-sdk-node/issues
- npm.io page: https://npm.io/package/aws-xray-sdk-postgres

## Dependencies (1)

- [@types/pg](https://npm.io/package/@types/pg.md) *

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 3.12.0 (latest) — 2025-11-20
- 3.0.0-alpha.2 (alpha) — 2020-03-23
- 2.5.0-experimental.1 (experimental) — 2019-11-13
- 3.11.0 — 2025-10-15
- 3.10.3 — 2025-01-24
- 3.10.2 — 2024-11-12
- 3.10.1 — 2024-09-19
- 3.10.0 — 2024-09-09
- 3.9.0 — 2024-06-10
- 3.8.0 — 2024-06-06
- 3.7.0 — 2024-06-05
- 3.6.0 — 2024-03-25
- 3.5.4 — 2024-02-22
- 3.5.3 — 2023-10-26
- 3.5.2 — 2023-09-25
- … 51 more at https://npm.io/package/aws-xray-sdk-postgres/versions

## README

## Requirements

* AWS X-Ray SDK Core
* Postgres 6.1.0 or greater

## AWS X-Ray and Postgres

The AWS X-Ray Postgres package automatically records query information and request
and response data. Simply patch the Postgres package via `capturePostgres` as shown below.

The AWS X-Ray SDK Core has two modes - `manual` and `automatic`.
Automatic mode uses the `cls-hooked` package and automatically
tracks the current segment and subsegment. This is the default mode.
Manual mode requires that you pass around the segment reference. See the examples below.

### Environment variables

    POSTGRES_DATABASE_VERSION        Sets additional data for the sql subsegment.
    POSTGRES_DRIVER_VERSION          Sets additional data for the sql subsegment.

### Lambda Example

```js
var AWSXRay = require('aws-xray-sdk');
var pg = AWSXRay.capturePostgres(require('pg'));

...

exports.handler = function (event, context, callback) {
  // Make postgres queries normally
}
```

### Automatic mode example

```js
var AWSXRay = require('aws-xray-sdk-core');
var capturePostgres = require('aws-xray-sdk-postgres');

var pg = capturePostgres(require('pg'));

...

var client = new pg.Client();

client.connect(function (err) {
  ...

  client.query({name: 'moop', text: 'SELECT $1::text as name'}, ['brianc'], function (err, result) {
    //Automatically captures query information and errors (if any)
  });
});

...

var pool = new pg.Pool(config);
pool.connect(function(err, client, done) {
  if(err) {
    return console.error('error fetching client from pool', err);
  }
  var query = client.query('SELECT * FROM mytable', function(err, result) {
    //Automatically captures query information and errors (if any)
  });
});
```

### Manual mode example

```js
var AWSXRay = require('aws-xray-sdk-core');
var capturePostgres = require('aws-xray-sdk-postgres');

var pg = capturePostgres(require('pg'));

...

var client = new pg.Client();

client.connect(function (err) {
  ...

  client.query({name: 'moop', text: 'SELECT $1::text as name'}, ['mcmuls'], function (err, result) {
    //Automatically captures query information and errors (if any)
  });
});

...

var pool = new pg.Pool(config);
pool.connect(function(err, client, done) {
  if(err) {
    return console.error('error fetching client from pool', err);
  }
  var query = client.query('SELECT * FROM mytable', function(err, result) {
    //Automatically captures query information and errors (if any)
  }, segment));
};
```

---
_Source: https://npm.io/package/aws-xray-sdk-postgres · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
