node-postgres (pg), run it locally with wrangler dev, and deploy it to workers.dev.
Prerequisites
- A Cloudflare account. Hyperdrive is available on the Workers Free and Paid plans.
- Node.js 20 or later. This guide was tested with Node.js 24.21.0, Wrangler 4.145.0,
create-cloudflare2.73.1,pg8.23.1, and Postgres 18. - A ClickHouse Cloud account.
psql, to create the database and table. You can also run the SQL statements in the SQL console.
Create a ClickHouse Managed Postgres service
In the ClickHouse Cloud console, click New service and select Postgres. The instance is ready in a few minutes. See the quickstart for a walkthrough.Get your connection details
Click Connect in the left sidebar of your service. Keep Directly selected and turn on Use SSL. The URL now ends insslmode=verify-full&sslrootcert=<service-name>-ca-certificate.pem. Click Download CA certificate to download the CA certificate for your instance. You can also download it from Settings → CA Certificate.
This guide points Hyperdrive at the direct connection on port 5432, not at the bundled PgBouncer on port 6432:
- Hyperdrive is itself a connection pooler that runs in transaction mode. It keeps a small pool of long-lived connections to your database and shares them across all Worker requests, which is the job PgBouncer would otherwise do. Cloudflare recommends giving Hyperdrive a direct connection string rather than a pooled one.
- Pointing Hyperdrive at port
6432also works, but chaining two transaction poolers adds a network hop and a second place to tune pool sizes, without adding capacity. - Hyperdrive opens at most around 20 connections per configuration on the Workers Free plan and around 100 on the Paid plan. That fits within the default
max_connectionsof 500. If you have several Hyperdrive configurations or other clients on the same instance, add up their connections, and adjustmax_connectionsin Settings or the origin connection limit in Hyperdrive if needed.
If you use an IP access list, Hyperdrive connects from Cloudflare’s IP ranges, so they must be allowed.
Create the database
Rename the downloaded certificate toca-certificate.pem. Then create a database and a notes table. Replace <PASSWORD> and the host with the values from the Connect modal:
Create the Worker project
Create a TypeScript Worker from the “Hello World” template withcreate-cloudflare, and install pg:
ca-certificate.pem into your project directory. If you haven’t used Wrangler before, run npx wrangler login to authorize it with your Cloudflare account.
Create a Hyperdrive configuration
Hyperdrive always encrypts the connection to your database, but by default it doesn’t verify the certificate against your instance’s CA. To get the equivalent ofsslmode=verify-full, upload the CA certificate to your Cloudflare account first:
ID from the output to create the Hyperdrive configuration. The connection string uses port 5432 and the guide_cloudflare database, with no SSL parameters; --sslmode and --ca-certificate-id configure TLS instead:
--sslmode verify-full, it checks that the server certificate is signed by your uploaded CA and that it matches the hostname. If either check fails, the command fails with an error such as cert verification failed - unable to get local issuer certificate and no configuration is created. The password is stored in the Hyperdrive configuration in your Cloudflare account, not in your project.
--binding HYPERDRIVE --update-config adds the configuration to wrangler.jsonc, so your Worker can reach it as env.HYPERDRIVE. If your project uses wrangler.toml, Wrangler doesn’t change the file; paste the [[hyperdrive]] snippet that the command prints into it instead.
Configure the Worker
pg needs the Node.js compatibility layer. Open wrangler.jsonc and add the nodejs_compat flag next to the compatibility_date. The relevant part of the file then looks like this:
wrangler.jsonc
wrangler.toml file, add compatibility_flags = ["nodejs_compat"] below compatibility_date instead. If the file already has compatibility_flags, add nodejs_compat to the list.
Regenerate the TypeScript types so that env.HYPERDRIVE is typed:
@cloudflare/workers-types package, wrangler types prints how to switch to the generated types.
Query the database
Replacesrc/index.ts with a Worker that creates a note on POST /notes and lists all notes on GET /notes. In an existing Worker, add the same /notes handling to your fetch handler instead:
src/index.ts
env.HYPERDRIVE.connectionString points at Hyperdrive, not at your instance. The link between the Worker and Hyperdrive stays inside Cloudflare, so it doesn’t need SSL parameters. Hyperdrive handles the verified TLS connection to Postgres.
Hyperdrive runs in transaction mode, like PgBouncer: each transaction can use a different connection to Postgres. Parameterized queries, transactions, and named prepared statements from
pg all work. Don’t rely on session state, such as a session-level SET, across queries; use SET LOCAL inside a transaction instead.Run locally
wrangler dev runs the Worker on your machine. Locally, Hyperdrive doesn’t pool connections; the binding connects straight to the database in CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING>. Set it to the direct connection string with sslmode=verify-full and the path to the CA certificate, then start the Worker:
sslrootcert is resolved relative to the directory you start wrangler dev from.
In a second terminal, create a note:
pg returns bigint columns as strings, so id is "1", not 1.
Deploy
Deploy the Worker:workers.dev subdomain. Create a note through the deployed Worker:
https://hyperdrive-notes.<your-subdomain>.workers.dev/notes in your browser to see both notes. The response looks like this:
Verify in the console
Open SQL console in the left sidebar of your service, expandguide_cloudflare and then public, and click the notes table. It shows both notes:
Clean up
To remove what you created in your Cloudflare account, delete the Worker, the Hyperdrive configuration, and the CA certificate:wrangler cert delete reports Certificate cannot be deleted while in use, wait a minute after deleting the Hyperdrive configuration and run it again.
Next steps
- Connection: connection strings, PgBouncer, and TLS
- Settings: Postgres parameters such as
max_connections - Read replicas: scale read traffic
- Hyperdrive documentation: query caching, connection pool tuning, and observability