Skip to content
AnswerWell geo Documentation

Research · 8 min read · Answerwell

Worker Database Connection Pooling, Networking, and Private Origin Access

How to connect Cloudflare Workers to MySQL and PostgreSQL with Hyperdrive: firewall rules, configuration, bindings, private networks, and caching.

This page summarizes Cloudflare documentation on Hyperdrive: connecting Cloudflare Workers (and Pages Functions) to existing MySQL or PostgreSQL databases with global connection pooling, optional query caching, and a Worker-only connection string you use with familiar drivers and ORMs.

Hyperdrive: what it does for Worker database queries

Section titled “Hyperdrive: what it does for Worker database queries”

Hyperdrive sits between your Worker and the origin database. It pools connections on Cloudflare’s network and can cache eligible read queries so Workers spend less time on connection setup and round trips.

Hyperdrive works by connecting to your database, pooling database connections globally, and speeding up your database access through Cloudflare’s network.

— Cloudflare, Getting started

It will provide a secure connection string that is only accessible from your Worker which you can use to connect to your database through Hyperdrive. This means that you can use the Hyperdrive connection string with your existing drivers or ORM libraries without needing significant changes to your code.

— Cloudflare, Getting started

Workers reach the database through a Hyperdrive binding (env.HYPERDRIVE), typically using env.HYPERDRIVE.connectionString with drivers such as pg or MySQL clients. Enable nodejs_compat (or nodejs_compat_v2) in Wrangler so database drivers work in the Worker runtime.

Network access: how Hyperdrive reaches a public database

Section titled “Network access: how Hyperdrive reaches a public database”

Before Hyperdrive can verify credentials at configuration time, the origin must accept traffic from Cloudflare and expose host, port, user, password, and database name.

Hyperdrive originates connections from Cloudflare IP address ranges, shared across Hyperdrive and other Cloudflare products.

Hyperdrive connects to your database using Cloudflare’s IP address ranges. These are shared by all Hyperdrive configurations and other Cloudflare products.

— Cloudflare, FAQ

You can connect to your database from Hyperdrive using any of the 3 following networking configurations:

Configure your database to allow inbound connectivity from the public Internet (all IP address ranges). Configure your database to allow inbound connectivity from the public Internet, with only the IP address ranges used by Hyperdrive allow-listed in an IP access control list (ACL). Configure your database to allow inbound connectivity from a private network, and run a Cloudflare Tunnel instance in your private network to enable Hyperdrive to connect from the Cloudflare network to your private network.

— Cloudflare, Firewall and networking configuration

Provider docs repeat a practical shortcut when tightening rules is awkward: allow Cloudflare IPs specifically, or temporarily use broad ranges such as 0.0.0.0/0 on the database port (MySQL default 3306, PostgreSQL 5432), as described for AWS RDS and Aurora (MySQL) and Azure Database for MySQL. For private origins, use Workers VPC or Cloudflare Tunnel instead of exposing the database publicly—covered below.

Credentials: dedicated database user for Hyperdrive

Section titled “Credentials: dedicated database user for Hyperdrive”

Use a dedicated, least-privileged user rather than the master account when possible. AWS RDS and Aurora (PostgreSQL) shows role-based setup; most hosted Postgres guides (Neon, Supabase) emphasize copying a direct connection string—not the provider’s pooler—because Hyperdrive already pools connections.

Hyperdrive configuration: connection string and Wrangler create

Section titled “Hyperdrive configuration: connection string and Wrangler create”

A Hyperdrive configuration stores origin connection parameters. You create it in the Cloudflare dashboard or with Wrangler; Wrangler tests the connection before saving.

To allow Hyperdrive to connect to your database, you will need to ensure that Hyperdrive has valid user credentials and network access.

— Cloudflare, AWS RDS and Aurora (MySQL)

You need: hostname or IP, port, username, password, and database name. Formats:

  • PostgreSQL: postgres://USERNAME:PASSWORD@HOSTNAME_OR_IP_ADDRESS:PORT/database_name
  • MySQL: mysql://USERNAME:PASSWORD@HOSTNAME_OR_IP_ADDRESS:PORT/database_name

Create Hyperdrive with wrangler hyperdrive create

Section titled “Create Hyperdrive with wrangler hyperdrive create”
  1. Log in: npx wrangler login (Getting started).

  2. Create the configuration (PostgreSQL example):

    npx wrangler hyperdrive create my-first-hyperdrive --connection-string="postgres://user:password@database.host.example.com:5432/databasenamehere"

    MySQL uses the mysql:// scheme (Connect to MySQL, Connect to PostgreSQL).

  3. Copy the id from the command output for your Wrangler binding.

  4. If verification fails, fix firewall or credentials first—Hyperdrive rejects invalid setups at create time.

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

— Cloudflare, Connect to PostgreSQL

The dashboard path: Storage & Databases → Hyperdrive → Create Configuration, then paste the connection string (see Neon and related provider pages).

Optional flags: caching, TLS, and origin limits

Section titled “Optional flags: caching, TLS, and origin limits”

wrangler hyperdrive create supports flags that matter in production:

Flag Purpose
--caching-disabled Turn off SQL response caching for this config
--max-age / --swr Cache TTL and stale-while-revalidate (not with caching disabled)
--ca-certificate-id + --sslmode Verify server TLS (verify-full for Postgres, VERIFY_IDENTITY for MySQL)
--mtls-certificate-id Client certificate authentication to the origin
--service-id Route through Workers VPC instead of public host/port
--access-client-id / --access-client-secret Authenticate to Cloudflare Tunnel via Access

ClickHouse Managed Postgres uploads a CA with wrangler cert upload certificate-authority, then creates Hyperdrive with --sslmode verify-full and --caching-disabled when reads must hit Postgres directly while pooling remains enabled.

Worker Hyperdrive binding: Wrangler config and runtime use

Section titled “Worker Hyperdrive binding: Wrangler config and runtime use”

A binding maps a name in your Worker (for example HYPERDRIVE) to a Hyperdrive configuration id.

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

— Cloudflare, Wrangler configuration

{
"compatibility_flags": ["nodejs_compat"],
"hyperdrive": [
{
"binding": "HYPERDRIVE",
"id": "<ID OF THE CREATED HYPERDRIVE CONFIGURATION>"
}
]
}

The binding name must be a valid JavaScript identifier; access it as env.HYPERDRIVE (connect to a private database using Tunnel).

In application code, pass the dynamic connection string to your driver—for example after migrating from a raw secret URL (Connect to a PostgreSQL database with Cloudflare Workers):

const sql = new Client({ connectionString: env.HYPERDRIVE.connectionString });

Prisma ORM requires a driver adapter (@prisma/adapter-pg) so Prisma uses Hyperdrive’s pooled connection string correctly.

For Pages Functions, local dev needs a direct database URL via CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME> or localConnectionString in Wrangler (Pages Functions bindings, Local development).

Private database access: Workers VPC vs Cloudflare Tunnel

Section titled “Private database access: Workers VPC vs Cloudflare Tunnel”

When the database has no public Internet endpoint, Hyperdrive still reaches it through private networking—choose one path:

Approach When to use How Hyperdrive is aimed
Workers VPC (recommended) Private DB reachable via a VPC Service --service-id only; no --connection-string / --origin-host / Tunnel Access headers (Workers VPC)
Cloudflare Tunnel + Access Legacy or Tunnel-based private routing Public hostname on the tunnel; Hyperdrive uses Access service token headers (Tunnel private database)

You can also connect Hyperdrive to a private database using Workers VPC (Recommended), which does not require configuring Access applications or service tokens.

— Cloudflare, Connect to a private database using Tunnel

Workers VPC example:

npx wrangler hyperdrive create my-vpc-database \
--service-id <YOUR_VPC_SERVICE_ID> \
--database <DATABASE_NAME> \
--user <DATABASE_USER> \
--password <DATABASE_PASSWORD> \
--scheme postgresql

The –service-id flag conflicts with –origin-host, –origin-port, –connection-string, –access-client-id, and –access-client-secret. You cannot combine these options.

— Cloudflare, Connect to a private database using Workers VPC

Tunnel flow: cloudflared in the private network, TCP public hostname to host:port, Access policy requiring a Service Auth token, then create Hyperdrive with --host=<TUNNEL_HOSTNAME>, database user/password, and omit port—Hyperdrive uses the tunnel hostname (Tunnel private database). The dashboard can auto-create Access app, service token, and policy when you choose Private database (release notes).

Query caching: defaults, staleness, and separate configs

Section titled “Query caching: defaults, staleness, and separate configs”

By default, Hyperdrive caches eligible read results (Getting started).

Hyperdrive does not invalidate cached read results when your application writes to your database. If your application needs read-after-write consistency, use a separate cache-disabled Hyperdrive configuration for the reads that must return fresh data.

— Cloudflare, Getting started

Disable caching at create or update time:

npx wrangler hyperdrive create my-database-fresh --connection-string="<DATABASE_CONNECTION_STRING>" --caching-disabled
npx wrangler hyperdrive update <HYPERDRIVE_CONFIG_ID> --caching-disabled

Bind two configurations—HYPERDRIVE and HYPERDRIVE_CACHE_DISABLED—and route auth, permissions, or post-write reads through the cache-disabled binding (Query caching).

Local development with Hyperdrive bindings

Section titled “Local development with Hyperdrive bindings”

wrangler dev runs Worker code locally; localConnectionString (or the CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME> environment variable) points straight at your database so you can develop without deploying.

When using localConnectionString, Hyperdrive’s connection pooling and query caching do not take effect. Your Worker connects directly to the database without going through Hyperdrive.

— Cloudflare, Local development

If both Wrangler localConnectionString and the environment variable are set, the environment variable wins.

Hosted database providers: where to find Hyperdrive walkthroughs

Section titled “Hosted database providers: where to find Hyperdrive walkthroughs”

After network access and a connection string, every provider follows the same create Hyperdrive → bind Worker pattern. Cloudflare maintains per-provider steps:

Engine Examples in Cloudflare docs
PostgreSQL Neon, Supabase, AWS RDS/Aurora, PlanetScale Postgres, Timescale, CockroachDB
MySQL PlanetScale, AWS RDS/Aurora, Google Cloud SQL

For PlanetScale databases billed through Cloudflare, wrangler hyperdrive planetscale signature produces a signed payload for pscale database create --cloudflare-billing (PlanetScale Postgres & MySQL).

When the Worker issues multiple queries per request, a placement hint near the database region can reduce compounded latency (Getting started); a single query per request sees little benefit from placement alone.

Rotate credentials by creating a new Hyperdrive configuration and switching bindings, or wrangler hyperdrive update on the existing id (Rotating database credentials). Updating credentials does not flush the query cache or tear down the origin pool immediately—plan for gradual connection turnover.

  • Blocking Hyperdrive at the database firewall while the Worker “works” only after overly broad 0.0.0.0/0 rules—prefer Cloudflare IP ranges in the ACL.
  • Using a provider connection pooler URL where docs require a direct endpoint (Neon, Supabase).
  • Mixing --service-id with --connection-string or Tunnel Access flags on one config.
  • Expecting cached reads immediately after writes without a --caching-disabled binding.
  • Assuming local wrangler dev exercises Hyperdrive pooling or cache—it uses the local connection string path only.
  1. Confirm the origin firewall allows Cloudflare IP ranges (or your chosen private path via Workers VPC or Tunnel).
  2. Run npx wrangler hyperdrive create ... or create the config in the Cloudflare dashboard (Getting started) and ensure create-time verification succeeds.
  3. Add the hyperdrive binding with the returned id in Wrangler configuration, enable nodejs_compat, and deploy with npx wrangler deploy.
  4. Hit a Worker route that runs a read query through env.HYPERDRIVE.connectionString and confirm expected data; if reads must be fresh after writes, test via a --caching-disabled configuration (Query caching).
  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.