aws-xray-sdk-postgres is the official AWS X-Ray patcher for Postgres (pg) Node.js driver, version 3.12.0. It automatically records SQL queries, request/response data, and errors as subsegments under the X-Ray trace. Compatible with Postgres 6.1.0+. Requires aws-xray-sdk-core as a peer dependency. Supports both automatic (via cls-hooked) and manual segment tracking. Ships TypeScript definitions. Maintenance is tied to the SDK core; release cadence is periodic alongside core updates. Key differentiator: seamless integration with AWS X-Ray for tracing database calls with minimal code changes.
npm install aws-xray-sdk-postgresNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Patches the pg driver to automatically record query traces with X-Ray. Connects, runs a SELECT, and logs the result.
Use Node >=14 and ensure aws-xray-sdk-core ^3.12.0 is installed.
Prefer manual mode by passing segment or use AWSXRay.getSegment() in async contexts.
Call capturePostgres(require('pg')) before any pg.Client or pg.Pool is created.Set 'esModuleInterop': true in tsconfig.json or use import * as capturePostgres from 'aws-xray-sdk-postgres'.
Ensure errors in callbacks are handled properly (e.g., use try-catch around the callback body).
npm install aws-xray-sdk-postgres
Use const capturePostgres = require('aws-xray-sdk-postgres') (no braces)npm install aws-xray-sdk-core
Call capturePostgres(require('pg')) directly on the default pg module