$npx -y skills add jackspace/ClaudeSkillz --skill cloudflare-hyperdriveComplete knowledge domain for Cloudflare Hyperdrive - connecting Cloudflare Workers to existing PostgreSQL and MySQL databases with global connection pooling, query caching, and reduced latency. Use when: connecting Workers to existing databases, migrating PostgreSQL/MySQL to Clo
| 1 | # Cloudflare Hyperdrive |
| 2 | |
| 3 | **Status**: Production Ready ✅ |
| 4 | **Last Updated**: 2025-10-22 |
| 5 | **Dependencies**: cloudflare-worker-base (recommended for Worker setup) |
| 6 | **Latest Versions**: wrangler@4.43.0+, pg@8.13.0+, postgres@3.4.5+, mysql2@3.13.0+ |
| 7 | |
| 8 | --- |
| 9 | |
| 10 | ## Quick Start (5 Minutes) |
| 11 | |
| 12 | ### 1. Create Hyperdrive Configuration |
| 13 | |
| 14 | ```bash |
| 15 | # For PostgreSQL |
| 16 | npx wrangler hyperdrive create my-postgres-db \ |
| 17 | --connection-string="postgres://user:password@db-host.cloud:5432/database" |
| 18 | |
| 19 | # For MySQL |
| 20 | npx wrangler hyperdrive create my-mysql-db \ |
| 21 | --connection-string="mysql://user:password@db-host.cloud:3306/database" |
| 22 | |
| 23 | # Output: |
| 24 | # ✅ Successfully created Hyperdrive configuration |
| 25 | # |
| 26 | # [[hyperdrive]] |
| 27 | # binding = "HYPERDRIVE" |
| 28 | # id = "a76a99bc-7901-48c9-9c15-c4b11b559606" |
| 29 | ``` |
| 30 | |
| 31 | **Save the `id` value** - you'll need it in the next step! |
| 32 | |
| 33 | --- |
| 34 | |
| 35 | ### 2. Configure Bindings in wrangler.jsonc |
| 36 | |
| 37 | Add to your `wrangler.jsonc`: |
| 38 | |
| 39 | ```jsonc |
| 40 | { |
| 41 | "name": "my-worker", |
| 42 | "main": "src/index.ts", |
| 43 | "compatibility_date": "2024-09-23", |
| 44 | "compatibility_flags": ["nodejs_compat"], // REQUIRED for database drivers |
| 45 | "hyperdrive": [ |
| 46 | { |
| 47 | "binding": "HYPERDRIVE", // Available as env.HYPERDRIVE |
| 48 | "id": "a76a99bc-7901-48c9-9c15-c4b11b559606" // From wrangler hyperdrive create |
| 49 | } |
| 50 | ] |
| 51 | } |
| 52 | ``` |
| 53 | |
| 54 | **CRITICAL:** |
| 55 | - `nodejs_compat` flag is **REQUIRED** for all database drivers |
| 56 | - `binding` is how you access Hyperdrive in code (`env.HYPERDRIVE`) |
| 57 | - `id` is the Hyperdrive configuration ID (NOT your database ID) |
| 58 | |
| 59 | --- |
| 60 | |
| 61 | ### 3. Install Database Driver |
| 62 | |
| 63 | ```bash |
| 64 | # For PostgreSQL (choose one) |
| 65 | npm install pg # node-postgres (most common) |
| 66 | npm install postgres # postgres.js (modern, minimum v3.4.5) |
| 67 | |
| 68 | # For MySQL |
| 69 | npm install mysql2 # mysql2 (minimum v3.13.0) |
| 70 | ``` |
| 71 | |
| 72 | --- |
| 73 | |
| 74 | ### 4. Query Your Database |
| 75 | |
| 76 | **PostgreSQL with node-postgres (pg):** |
| 77 | ```typescript |
| 78 | import { Client } from "pg"; |
| 79 | |
| 80 | type Bindings = { |
| 81 | HYPERDRIVE: Hyperdrive; |
| 82 | }; |
| 83 | |
| 84 | export default { |
| 85 | async fetch(request: Request, env: Bindings, ctx: ExecutionContext) { |
| 86 | const client = new Client({ |
| 87 | connectionString: env.HYPERDRIVE.connectionString |
| 88 | }); |
| 89 | |
| 90 | await client.connect(); |
| 91 | |
| 92 | try { |
| 93 | const result = await client.query('SELECT * FROM users LIMIT 10'); |
| 94 | return Response.json({ users: result.rows }); |
| 95 | } finally { |
| 96 | // Clean up connection AFTER response is sent |
| 97 | ctx.waitUntil(client.end()); |
| 98 | } |
| 99 | } |
| 100 | }; |
| 101 | ``` |
| 102 | |
| 103 | **MySQL with mysql2:** |
| 104 | ```typescript |
| 105 | import { createConnection } from "mysql2/promise"; |
| 106 | |
| 107 | export default { |
| 108 | async fetch(request: Request, env: Bindings, ctx: ExecutionContext) { |
| 109 | const connection = await createConnection({ |
| 110 | host: env.HYPERDRIVE.host, |
| 111 | user: env.HYPERDRIVE.user, |
| 112 | password: env.HYPERDRIVE.password, |
| 113 | database: env.HYPERDRIVE.database, |
| 114 | port: env.HYPERDRIVE.port, |
| 115 | disableEval: true // REQUIRED for Workers (eval() not supported) |
| 116 | }); |
| 117 | |
| 118 | try { |
| 119 | const [rows] = await connection.query('SELECT * FROM users LIMIT 10'); |
| 120 | return Response.json({ users: rows }); |
| 121 | } finally { |
| 122 | ctx.waitUntil(connection.end()); |
| 123 | } |
| 124 | } |
| 125 | }; |
| 126 | ``` |
| 127 | |
| 128 | --- |
| 129 | |
| 130 | ### 5. Deploy |
| 131 | |
| 132 | ```bash |
| 133 | npx wrangler deploy |
| 134 | ``` |
| 135 | |
| 136 | **That's it!** Your Worker now connects to your existing database via Hyperdrive with: |
| 137 | - ✅ Global connection pooling |
| 138 | - ✅ Automatic query caching |
| 139 | - ✅ Reduced latency (eliminates 7 round trips) |
| 140 | |
| 141 | --- |
| 142 | |
| 143 | ## How Hyperdrive Works |
| 144 | |
| 145 | ### The Problem |
| 146 | Connecting to traditional databases from Cloudflare's 300+ global locations presents challenges: |
| 147 | |
| 148 | 1. **High Latency** - Multiple round trips for each connection: |
| 149 | - TCP handshake (1 round trip) |
| 150 | - TLS negotiation (3 round trips) |
| 151 | - Database authentication (3 round trips) |
| 152 | - **Total: 7 round trips before you can even send a query** |
| 153 | |
| 154 | 2. **Connection Limits** - Traditional databases handle limited concurrent connections, easily exhausted by distributed traffi |