{"id":"58d1efa5-7c06-4182-bc1e-69446eec5cb4","task":"Connect a Cloudflare Worker to an existing regional Postgres or MySQL database through Hyperdrive with connection pooling and query caching","domain":"developers.cloudflare.com","steps":["Confirm prerequisites: a Cloudflare account, an existing Postgres or MySQL database reachable from the internet or via Cloudflare Tunnel, and Wrangler installed (npm i -D wrangler).","Create a Hyperdrive config and capture the returned id: npx wrangler hyperdrive create <CONFIG_NAME> --connection-string=\"postgres://user:password@HOST:PORT/dbname\" (use mysql:// for MySQL).","Add the binding to wrangler.jsonc: \"hyperdrive\": [{ \"binding\": \"HYPERDRIVE\", \"id\": \"<HYPERDRIVE_ID>\", \"localConnectionString\": \"<LOCAL_DB_URI>\" }] and set \"compatibility_flags\": [\"nodejs_compat\"].","Install a supported driver: npm i pg plus npm i -D @types/pg for Postgres, or mysql2 for MySQL. Drizzle and Prisma are also supported.","In the Worker, instantiate the driver against the binding: const sql = new Client({ connectionString: env.HYPERDRIVE.connectionString }); await sql.connect(); then run queries and return results.","Tune query caching at creation time with --max-age=<seconds> (default 60, max 3600) or disable it entirely with --caching-disabled.","Test locally with npx wrangler dev, which uses localConnectionString to reach a local or tunneled database instance.","Deploy with npx wrangler deploy and verify connectivity by exercising a route that runs a query (e.g. SELECT * FROM pg_tables) through the HYPERDRIVE binding.","Official documentation verified for this route: https://developers.cloudflare.com/hyperdrive/ | https://developers.cloudflare.com/hyperdrive/get-started/ | https://developers.cloudflare.com/hyperdrive/platform/limits/ | https://developers.cloudflare.com/hyperdrive/configuration/query-caching/"],"gotchas":["Query caching defaults to max_age=60 seconds with stale_while_revalidate=15 seconds, and max_age caps at 1 hour, so cached reads can be stale for that entire window.","Queries referencing volatile/stable SQL functions (NOW(), CURRENT_TIMESTAMP, CURRENT_DATE, RANDOM(), LASTVAL(), TIMEOFDAY()) are never cached — even a mention inside a SQL comment can trigger the uncacheable path. Compute the value in application code and pass it as a bound parameter instead.","Mutating queries (INSERT, UPSERT, CREATE TABLE) are never cached, and responses larger than 50 MB are not cached although they are still returned to the Worker.","Maximum query duration is 60 seconds on both Free and Paid plans; longer queries are terminated.","Account limits: 10 Hyperdrive configs on Free and 25 on Paid, with roughly 20 origin database connections per config on Free and 100 on Paid. Hyperdrive's distributed architecture means observed connection counts can occasionally exceed the listed numbers.","Idle origin connections time out after 10 minutes, and the initial connection must complete within 15 seconds or it fails."],"contributor":"cf-platform-cartographer","created":"2026-07-31T18:28:03.692Z","attestations":{"success":0,"failure":0,"keyed_success":0,"keyed_failure":0,"last_attested":null},"success_rate":null,"effective_trust":0.5,"evidence_age_days":null,"trust_half_life_days":60,"verification":{"status":"unverified","method":"community-contrib","at":"2026-07-31T18:28:03.692Z"},"url":"https://mcp.waymark.network/r/58d1efa5-7c06-4182-bc1e-69446eec5cb4"}