---
title: "Postgres.js"
description: "Adds instrumentation for the postgres (postgres.js) library."
url: https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/integrations/postgresjs/
---

# Postgres.js for Cloudflare

Available since: `v10.41.0`

*Import name: `Sentry.instrumentPostgresJsSql`*

The `instrumentPostgresJsSql` helper adds instrumentation for the [`postgres`](https://github.com/porsager/postgres) (postgres.js) library to capture spans by wrapping a postgres.js `sql` tagged-template instance.

## [Usage](https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/integrations/postgresjs.md#usage)

If you build your Worker with the [Sentry Cloudflare Vite plugin](https://docs.sentry.io/platforms/javascript/guides/cloudflare/install/vite-plugin.md), you don't need this helper. The plugin traces postgres.js automatically, as long as `postgres` is bundled and not marked as external. If you deploy with `wrangler` directly, wrap your `sql` instance with the helper:

**Vite**

```javascript
import postgres from "postgres";

export default {
  async fetch(request, env, ctx) {
    const sql = postgres(env.DATABASE_URL);

    // All queries create Sentry spans, no helper needed
    const users = await sql`SELECT * FROM users WHERE id = ${userId}`;
    return Response.json(users);
  },
};
```

**Wrangler**

```javascript
import postgres from "postgres";
import * as Sentry from "@sentry/cloudflare";

export default Sentry.withSentry(
  (env) => ({
    dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
    tracesSampleRate: 1.0,
  }),
  {
    async fetch(request, env, ctx) {
      const sql = Sentry.instrumentPostgresJsSql(
        postgres(env.DATABASE_URL),
      );

      // All queries now create Sentry spans
      const users = await sql`SELECT * FROM users WHERE id = ${userId}`;
      return Response.json(users);
    },
  },
);
```

## [Options](https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/integrations/postgresjs.md#options)

### [`requireParentSpan`](https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/integrations/postgresjs.md#requireparentspan)

*Type: `boolean`*

Whether the instrumentation requires a parent span to create child spans. When set to `true`, spans are only created if there is an active parent span in the current scope.

Default: `true`

### [`requestHook`](https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/integrations/postgresjs.md#requesthook)

*Type: `(span: Span, sanitizedSqlQuery: string, postgresConnectionContext?: PostgresConnectionContext) => void`*

A hook called before each span is started. Use it to set additional attributes or modify the span.

```javascript
const sql = Sentry.instrumentPostgresJsSql(postgres(env.DATABASE_URL), {
  requestHook(span, query) {
    span.setAttribute("custom.query", query);
  },
});
```
