Hyperdrive: make an outside Postgres or MySQL database fast from Workers
Hyperdrive (connection pooling and query caching for existing databases)
What it is
Hyperdrive is a bridge between a Worker and a database that lives somewhere else, such as a Postgres database at Supabase, Neon, or AWS, or a MySQL database on a VPS. It does not store any data itself. You give it the database's connection string once, and it hands your Worker a safe connection that only that Worker can use.
It speeds things up in three ways. First, the slow part of opening a database connection (several round trips to agree on encryption and log in) happens close to the Worker instead of across the world. Second, Hyperdrive keeps a pool of already-open connections near the database and reuses them, so a burst of visitors does not open hundreds of new connections. Third, it caches the results of read queries (SELECT) for a short time, 60 seconds by default, and answers repeat reads without touching the database.
You create a Hyperdrive configuration in the dashboard (under Storage & databases, then Hyperdrive; the wording may differ slightly) or with wrangler hyperdrive create <name> --connection-string="postgres://user:password@host:5432/dbname". That gives you an id, which you add to wrangler.toml as a [[hyperdrive]] block with binding = "HYPERDRIVE". In the Worker you then pass env.HYPERDRIVE.connectionString to an ordinary driver such as postgres.js or node-postgres, with the nodejs_compat flag turned on. Hyperdrive is available on the Workers Free plan.
Why it matters
D1 (lesson on 5 October) is the right home for a new small app's data. Hyperdrive is for the other case: the data already lives in a bigger database you do not want to move. It lets a fast Worker front end read and write that database without the usual lag of a Worker talking to a far-away server.
The caching has one catch worth remembering. Hyperdrive does not clear its cached reads when you write, so for up to the cache time a page can show the old value right after someone saves a change. For anything that must be fresh, such as logins, permissions, or a booking you just made, Cloudflare suggests a second Hyperdrive configuration created with --caching-disabled and used only for those reads.
It also keeps the database password out of the Worker code. The connection string is stored in the Hyperdrive configuration, not in worker.js or wrangler.toml, which fits the secrets habit from the env vars lesson. As always, never point a learning setup at a database that holds real patient records.
On your account
From your own setup: you have a Supabase account, and every Supabase project is a Postgres database underneath. If one of your apps kept its data there, a Worker on a kosan.xyz or drkotha.com subdomain could reach it through a Hyperdrive configuration named something like supabase-notes, with the Supabase connection string stored in Hyperdrive and only the Hyperdrive id in wrangler.toml.
The same pattern would work for a Postgres or MySQL database running on one of your Hostinger VPS servers, as long as the database accepts outside connections (or sits behind a Cloudflare Tunnel, from the 10 October lesson).
This tutor does not use Hyperdrive: cloudflare.kosan.xyz is fixed text inside worker.js with no database at all. Nothing in this lesson creates a Hyperdrive configuration, stores a connection string, or changes cloudflare-tutor, ainews, aiweekly, or any other Worker or DNS record.
Do this yourself
These stay in the dashboard, on your side. This site does not change DNS, routes, or other Workers.
- In the Cloudflare dashboard, open Storage & databases, then Hyperdrive (wording may differ slightly), note whether any Hyperdrive configurations already exist, then click Create and write down the fields the form asks for (name, database type, host, port, database name, user, password, and any caching option) before pressing Cancel. Only look; do not create a configuration, paste any real connection string or password, or change any Worker.