Skip to content
AnswerWell geo Documentation

Research · 9 min read · Answerwell

PostgreSQL and MySQL Worker Connections: Hyperdrive Bindings, Drivers, and Lifecycle

How to bind Hyperdrive on Workers and Pages, choose pg or mysql2 drivers, enable nodejs_compat, and avoid pooling mistakes with PostgreSQL and MySQL.

This page summarizes Cloudflare Hyperdrive documentation on connecting Cloudflare Workers and Pages Functions to existing PostgreSQL and MySQL databases. Hyperdrive is a service for connecting to your existing databases from the edge: it pools connections near your database, routes queries from Workers globally, and exposes credentials through a Hyperdrive binding on env (for example env.HYPERDRIVE).

Hyperdrive is for remote SQL over the PostgreSQL and MySQL wire protocols. Use it when a Worker or Pages Function must query a database that lives outside Cloudflare’s network (managed Postgres/MySQL on AWS, Azure, Neon, Supabase, PlanetScale, and similar hosts all follow the same driver and binding pattern in the examples).

Hyperdrive is a service for connecting to your existing databases from Cloudflare Workers and Pages Functions.

— Cloudflare, Bindings

Always use Hyperdrive when connecting to a remote PostgreSQL or MySQL database from a Worker. Hyperdrive maintains a regional connection pool close to your database, eliminating the per-request cost of TCP handshake, TLS negotiation, and connection setup. It also caches query results where possible.

— Cloudflare, Workers Best Practices

For MySQL without Hyperdrive, direct secure connections from Workers are discouraged because drivers rely on Node.js APIs that are unsupported in that path:

We recommend using Hyperdrive to connect to your MySQL database. Hyperdrive provides optimal performance and will ensure secure connectivity between your Worker and your MySQL database.

When connecting directly to your MySQL database (without Hyperdrive), the MySQL drivers rely on unsupported Node.js APIs to create secure connections, which prevents connections.

— Cloudflare, Connect to a MySQL database with Cloudflare Workers

Not supported on Hyperdrive: MongoDB and other non–Postgres/MySQL protocols.

Hyperdrive does not support MongoDB. Hyperdrive only supports PostgreSQL and MySQL wire protocols. To reduce connection latency with MongoDB, use a Durable Object as shown in this tutorial.

— Cloudflare, Connect to a MongoDB database with Cloudflare Workers

Turso (libSQL), Upstash Redis, and similar integrations in the same doc set use other clients and secrets, not Hyperdrive—omit them when your goal is pooled Postgres/MySQL from Workers.

nodejs_compat: required for SQL drivers on Workers and Pages

Section titled “nodejs_compat: required for SQL drivers on Workers and Pages”

Database drivers (pg, mysql2, postgres, and ORMs built on them) need Node.js compatibility on the Worker or Pages project.

Node.js compatibility is required for database drivers, including mysql2, and needs to be configured for your Workers project.

— Cloudflare, Connect to MySQL

PostgreSQL drivers like Postgres.js depend on Node.js APIs. Pages Functions with Hyperdrive bindings must be deployed with Node.js compatibility.

— Cloudflare, Bindings

For compatibility_date of 2026-08-04 or later, Workers and Pages enable nodejs_compat and nodejs_compat_v2 by default. Older dates need an explicit flag in wrangler.jsonc:

{
"compatibility_flags": ["nodejs_compat"]
}

Python Workers use python_workers instead; see Python Workers for asyncpg / aiomysql with the same Hyperdrive binding.

Hyperdrive binding: Wrangler config for Workers and Pages

Section titled “Hyperdrive binding: Wrangler config for Workers and Pages”

A Hyperdrive binding links a Hyperdrive configuration ID to a name on env (commonly HYPERDRIVE). Configure it in Wrangler the same way for Workers and Pages Functions.

Hyperdrive bindings allow you to interact with and query any Postgres database from within a Pages Function.

Configure Hyperdrive bindings via your Wrangler file the same way they are configured with Cloudflare Workers.

— Cloudflare, Wrangler configuration

Example fragment (JSON):

{
"compatibility_flags": ["nodejs_compat"],
"compatibility_date": "2026-10-29",
"hyperdrive": [
{
"binding": "HYPERDRIVE",
"id": "<your-hyperdrive-id-here>",
"localConnectionString": "<optional-local-dev-string>"
}
]
}

TOML equivalent uses [[hyperdrive]] with binding and id (see Hyperdrive (Postgres & MySQL)).

Hyperdrive binding via the Cloudflare dashboard (Pages)

Section titled “Hyperdrive binding via the Cloudflare dashboard (Pages)”

On Pages you can attach a binding without editing Wrangler:

  1. Open Workers & Pages in the dashboard.
  2. Select the Pages project → Settings → Bindings → Add → Hyperdrive.
  3. Set the Variable name and choose the Hyperdrive configuration.
  4. Redeploy so the binding is available on context.env (for example context.env.HYPERDRIVE).

To bind your Hyperdrive config to your Pages Function, you can configure a Hyperdrive binding in the Wrangler configuration file or the Cloudflare dashboard.

— Cloudflare, Bindings

Create a Hyperdrive configuration (connection string)

Section titled “Create a Hyperdrive configuration (connection string)”

Before binding, create a config with Wrangler (credentials are validated against the database):

npx wrangler hyperdrive create hyperdrive --connection-string="SERVICEURL"

Hyperdrive will attempt to connect to your database with the provided credentials to verify they are correct before creating a configuration.

— Cloudflare, serverless time-series API with Timescale

Private databases can reach Hyperdrive through Cloudflare Tunnel and Access or Workers VPC; validation steps are the same after deploy (see connect to a private database using Tunnel and Workers VPC (Recommended)).

PostgreSQL from Workers: pg, Postgres.js, and ORMs

Section titled “PostgreSQL from Workers: pg, Postgres.js, and ORMs”

Provider-specific pages (AWS RDS, Neon, Supabase, CockroachDB, Timescale, and others) share one pattern: install a driver, bind Hyperdrive, pass env.HYPERDRIVE.connectionString per request.

Section titled “node-postgres (pg): recommended PostgreSQL driver”

Node-postgres (pg) is the recommended driver for connecting to your Postgres database from JavaScript or TypeScript Workers. It has the best compatibility with Hyperdrive’s caching and is commonly available with popular ORM libraries. Postgres.js is also supported.

— Cloudflare, node-postgres (pg)

Requirement Value
Package pg
Minimum version 8.16.3
TypeScript types @types/pg (optional)
Connection input env.HYPERDRIVE.connectionString

Per request, create a Client, connect(), query, return JSON. Hyperdrive pools the origin connection:

Create a new client instance for each request. Hyperdrive maintains the underlying database connection pool, so creating a new client is fast.

— Cloudflare, node-postgres (pg)

Postgres.js: prepare, max, and fetch_types

Section titled “Postgres.js: prepare, max, and fetch_types”

Minimum postgres package version 3.4.5. Cloudflare’s example sets max: 5 (Workers limit on concurrent external connections), fetch_types: false when you do not need array types, and prepare: true so Hyperdrive can cache prepared statements.

  • Drizzle + PostgreSQL: use drizzle-orm/node-postgres with pg Client and env.HYPERDRIVE.connectionString. Pairing Drizzle with Postgres.js over Hyperdrive is not currently supported; switch to pg (Drizzle ORM (Postgres)).
  • Drizzle + MySQL: drizzle-orm/mysql2 with Hyperdrive host/user/password fields and disableEval: true (Drizzle ORM (MySQL)).
  • Prisma: install pg@>8.16.3 and @prisma/adapter-pg with the Hyperdrive binding (Prisma ORM).

Hosted Postgres: use wire-protocol drivers, not serverless SDKs

Section titled “Hosted Postgres: use wire-protocol drivers, not serverless SDKs”

For Neon, Supabase, PlanetScale Postgres, and similar, docs say to connect with pg or Postgres.js through Hyperdrive—not each vendor’s HTTP/serverless driver:

When connecting to a Neon database with Hyperdrive, you should use a driver like node-postgres (pg) or Postgres.js to connect directly to the underlying database instead of the Neon serverless driver. Hyperdrive is optimized for database access for Workers and will perform global connection pooling and fast query routing by connecting directly to your database.

— Cloudflare, Neon

The same guidance appears for Supabase and PlanetScale Postgres. Use @supabase/supabase-js only when you need Supabase platform APIs (Supabase integration).

Pages Functions can use Postgres.js in onRequest:

import postgres from "postgres";
export async function onRequest(context) {
const sql = postgres(context.env.HYPERDRIVE.connectionString);
const result = await sql`SELECT id, name, value FROM records`;
return Response.json({ result });
}

(See Bindings.)

MySQL from Workers: mysql2, mysql, and PlanetScale

Section titled “MySQL from Workers: mysql2, mysql, and PlanetScale”
Requirement Value
Package mysql2
Minimum version 3.13.0
Import mysql2/promise (createConnection)
Hyperdrive fields host, user, password, database, port on env.HYPERDRIVE
Workers-specific option disableEval: true

mysql2 uses eval() to optimize result parsing for rows with > 100 columns. Configure mysql2 to use static parsing instead of eval() parsing with disableEval.

— Cloudflare, Getting started

PlanetScale MySQL examples use the same mysql2 + Hyperdrive shape (PlanetScale).

The callback-based mysql driver works with Hyperdrive credential fields; mysql2 is the modern default in tutorials (Connect to MySQL).

Connection lifecycle: per-request clients and automatic cleanup

Section titled “Connection lifecycle: per-request clients and automatic cleanup”

Hyperdrive changes how you think about pooling: pool at Hyperdrive, new driver client per invocation.

When your Worker finishes processing a request, the database client is automatically garbage collected and the edge connection to Hyperdrive is cleaned up. Hyperdrive keeps the underlying connection to your origin database open in its pool for reuse.

You do not need to call client.end(), sql.end(), connection.end() (or similar) to clean up database clients.

— Cloudflare, Connection lifecycle

Create database clients inside your handlers … Do not create database clients or connection pools in the global scope. Instead, create a new client inside each handler invocation — Hyperdrive’s connection pool ensures this is fast.

— Cloudflare, Connection lifecycle

Using new Pool() or a global Client causes stale connections and hard errors because Workers do not allow I/O across request contexts.

Common mistakes when connecting SQL from Workers

Section titled “Common mistakes when connecting SQL from Workers”
Mistake Why it fails What docs recommend
Global pg Client or driver Pool Stale connections across isolates New client inside fetch / onRequest (Connection lifecycle)
Direct postgres:// URL to remote DB without Hyperdrive Full TCP+TLS+auth per request (~300–500 ms cited in best practices) Hyperdrive binding (Workers Best Practices)
Postgres.js with prepare: false Extra protocol round-trips; hangs under transaction pooling Keep prepare: true (Postgres.js)
Vendor serverless driver (Neon/Supabase HTTP) with Hyperdrive Bypasses Hyperdrive’s pooling to the wire protocol pg or Postgres.js via env.HYPERDRIVE (Neon)
MySQL without disableEval: true on mysql2 eval() unavailable on Workers Set disableEval: true (Getting started)
Pages binding change without redeploy context.env.HYPERDRIVE missing at runtime Redeploy after dashboard binding (Bindings)

After code changes, deploy with:

npx wrangler deploy

A successful Postgres smoke test often returns success: true and rows from SELECT * FROM pg_tables (ClickHouse Managed Postgres example).

  1. Create or confirm a Hyperdrive config with npx wrangler hyperdrive create and a valid connection string, following Getting started or your provider page (for example AWS RDS and Aurora (Postgres)).

  2. Verify wrangler.jsonc includes nodejs_compat (if required for your compatibility_date), a hyperdrive binding with the correct id, and driver versions (pg@>8.16.3 or mysql2@>3.13.0) per Connect to PostgreSQL or Connect to MySQL.

  3. Deploy the Worker or Pages project (npx wrangler deploy) and call the deployed URL; for private DB setups, confirm table metadata returns as documented in connect to a private database using Tunnel or Workers VPC.

  4. If using Pages dashboard bindings, confirm Settings → Bindings → Hyperdrive matches the variable name in code, then redeploy (Bindings).

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about, and we will correct or clarify it.