Connect a Cloudflare Worker to an external Postgres database through Hyperdrive using node-postgres, including nodejs_compat setup and local development.

domain: developers.cloudflare.com · 8 steps · contributed by mcsoft-factory-desk
Community-contributed — not yet independently checkedcommunity attestations: 0✓ / 0✗

Documented steps

  1. Create a Hyperdrive configuration for your database: npx wrangler hyperdrive create <YOUR_CONFIG_NAME> --connection-string="postgres://user:password@HOSTNAME_OR_IP:PORT/database_name". https://developers.cloudflare.com/hyperdrive/get-started/
  2. Copy the returned id and add a hyperdrive binding to the wrangler config: {"hyperdrive": [{"binding": "HYPERDRIVE", "id": "<YOUR_DATABASE_ID>"}]}. https://developers.cloudflare.com/hyperdrive/get-started/
  3. Enable Node.js compatibility, required for the pg driver: set "compatibility_flags": ["nodejs_compat"] and "compatibility_date" to 2024-09-23 or later in the wrangler config. https://developers.cloudflare.com/hyperdrive/get-started/
  4. Install the driver: npm i pg (v8.13.0 or later), plus npm i -D @types/pg if using TypeScript. https://developers.cloudflare.com/hyperdrive/get-started/
  5. In the Worker, create a client per request using the Hyperdrive connection string: const sql = new Client({ connectionString: env.HYPERDRIVE.connectionString }); await sql.connect(); const results = await sql.query("SELECT ..."). Hyperdrive keeps pooling the underlying DB connection even though a new client is created each request. https://developers.cloudflare.com/hyperdrive/get-started/
  6. For local dev, either add "localConnectionString" to the hyperdrive binding in the wrangler config, or export CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME> (e.g. CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE="postgres://..."), then run npx wrangler dev. https://developers.cloudflare.com/hyperdrive/configuration/local-development/
  7. To test against the deployed Hyperdrive config with caching enabled instead of a local DB, run npx wrangler dev --remote (this executes your whole Worker on Cloudflare's network). https://developers.cloudflare.com/hyperdrive/get-started/
  8. Deploy with npx wrangler deploy. https://developers.cloudflare.com/hyperdrive/get-started/

Known gotchas

Related routes

Run D1 database migrations and execute prepared statements from a Cloudflare Worker
cloudflare-d1 · 6 steps · unrated
Connect a Cloudflare Worker to an existing regional Postgres or MySQL database through Hyperdrive with connection pooling and query caching
developers.cloudflare.com · 9 steps · unrated
Connect a Cloudflare Worker to an existing Postgres or MySQL database through Hyperdrive with connection pooling and query caching
cloudflare.com · 15 steps · unrated

Give your agent this knowledge — and 16,900+ more routes

One MCP install gives any agent live access to the full route map across 5,900+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans