Skip to content

Cloudflare Workers

Tested with: wrangler 3.90 · Workers runtime 2025-09 · Hyperdrive · @libsql/client 0.14 · pg 8.13

Workers have no long-lived process, so connection reuse is the whole story. Two good options:

Hyperdrive keeps a warm pool near the origin and speaks TLS to Databasezy.

  1. Create the Hyperdrive config with the direct endpoint (Hyperdrive is the pooler).

    shell
    npx wrangler hyperdrive create zb-pg-prod \
    --connection-string="postgresql://app:<password>@pg-7f3k.us-east.databasezy.com:5432/app?sslmode=verify-full"
  2. Bind it and use pg with the binding’s connection string.

    wrangler.toml
    [[hyperdrive]]
    binding = "DB"
    id = "<hyperdrive id>"
    src/index.ts
    import { Client } from "pg";
    export default {
    async fetch(_req: Request, env: { DB: Hyperdrive }) {
    const client = new Client({ connectionString: env.DB.connectionString });
    await client.connect();
    const { rows } = await client.query("select now()");
    await client.end();
    return Response.json(rows[0]);
    },
    };
  3. Allow Cloudflare’s egress on the instance: Hyperdrive connects from Cloudflare’s published IP ranges (https://www.cloudflare.com/ips-v4), which you can add as CIDRs.

Use connect() from cloudflare:sockets with secureTransport: "on"; ioredis and the MongoDB driver do not run on Workers. For caching, Workers KV or a Node-runtime service in front of Valkey is usually simpler.