Loading...
Loading...
Complete 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 Cloudflare, setting up connection pooling, configuring Hyperdrive bindings, using node-postgres/postgres.js/mysql2 drivers, integrating Drizzle ORM or Prisma ORM, or encountering "Failed to acquire a connection from the pool", "TLS not supported by the database", "connection refused", "nodejs_compat missing", "Code generation from strings disallowed", or Hyperdrive configuration errors. Keywords: hyperdrive, cloudflare hyperdrive, workers hyperdrive, postgres workers, mysql workers, connection pooling, query caching, node-postgres, pg, postgres.js, mysql2, drizzle hyperdrive, prisma hyperdrive, workers rds, workers aurora, workers neon, workers supabase, database acceleration, hybrid architecture, cloudflare tunnel database, wrangler hyperdrive, hyperdrive bindings, local development hyperdrive
npx skill4agent add jackspace/claudeskillz cloudflare-hyperdrive# For PostgreSQL
npx wrangler hyperdrive create my-postgres-db \
--connection-string="postgres://user:password@db-host.cloud:5432/database"
# For MySQL
npx wrangler hyperdrive create my-mysql-db \
--connection-string="mysql://user:password@db-host.cloud:3306/database"
# Output:
# ✅ Successfully created Hyperdrive configuration
#
# [[hyperdrive]]
# binding = "HYPERDRIVE"
# id = "a76a99bc-7901-48c9-9c15-c4b11b559606"idwrangler.jsonc{
"name": "my-worker",
"main": "src/index.ts",
"compatibility_date": "2024-09-23",
"compatibility_flags": ["nodejs_compat"], // REQUIRED for database drivers
"hyperdrive": [
{
"binding": "HYPERDRIVE", // Available as env.HYPERDRIVE
"id": "a76a99bc-7901-48c9-9c15-c4b11b559606" // From wrangler hyperdrive create
}
]
}nodejs_compatbindingenv.HYPERDRIVEid# For PostgreSQL (choose one)
npm install pg # node-postgres (most common)
npm install postgres # postgres.js (modern, minimum v3.4.5)
# For MySQL
npm install mysql2 # mysql2 (minimum v3.13.0)import { Client } from "pg";
type Bindings = {
HYPERDRIVE: Hyperdrive;
};
export default {
async fetch(request: Request, env: Bindings, ctx: ExecutionContext) {
const client = new Client({
connectionString: env.HYPERDRIVE.connectionString
});
await client.connect();
try {
const result = await client.query('SELECT * FROM users LIMIT 10');
return Response.json({ users: result.rows });
} finally {
// Clean up connection AFTER response is sent
ctx.waitUntil(client.end());
}
}
};import { createConnection } from "mysql2/promise";
export default {
async fetch(request: Request, env: Bindings, ctx: ExecutionContext) {
const connection = await createConnection({
host: env.HYPERDRIVE.host,
user: env.HYPERDRIVE.user,
password: env.HYPERDRIVE.password,
database: env.HYPERDRIVE.database,
port: env.HYPERDRIVE.port,
disableEval: true // REQUIRED for Workers (eval() not supported)
});
try {
const [rows] = await connection.query('SELECT * FROM users LIMIT 10');
return Response.json({ users: rows });
} finally {
ctx.waitUntil(connection.end());
}
}
};npx wrangler deploy# PostgreSQL connection string format:
# postgres://username:password@hostname:port/database_name
npx wrangler hyperdrive create my-hyperdrive \
--connection-string="postgres://myuser:mypassword@db.example.com:5432/mydb"
# MySQL connection string format:
# mysql://username:password@hostname:port/database_name
npx wrangler hyperdrive create my-hyperdrive \
--connection-string="mysql://myuser:mypassword@db.example.com:3306/mydb"my-hyperdrivedb.example.com54323306mydbmyusermypassword# PostgreSQL (standard)
postgres://user:password@host:5432/database
# PostgreSQL with SSL mode
postgres://user:password@host:5432/database?sslmode=require
# MySQL
mysql://user:password@host:3306/database
# With special characters in password (URL encode)
postgres://user:p%40ssw%24rd@host:5432/database # p@ssw$rdwrangler.jsonc{
"name": "my-worker",
"main": "src/index.ts",
"compatibility_date": "2024-09-23",
"compatibility_flags": ["nodejs_compat"],
"hyperdrive": [
{
"binding": "HYPERDRIVE",
"id": "<your-hyperdrive-id-here>"
}
]
}{
"hyperdrive": [
{
"binding": "POSTGRES_DB",
"id": "postgres-hyperdrive-id"
},
{
"binding": "MYSQL_DB",
"id": "mysql-hyperdrive-id"
}
]
}type Bindings = {
POSTGRES_DB: Hyperdrive;
MYSQL_DB: Hyperdrive;
};
export default {
async fetch(request, env: Bindings, ctx) {
// Access different databases
const pgClient = new Client({ connectionString: env.POSTGRES_DB.connectionString });
const mysqlConn = await createConnection({ host: env.MYSQL_DB.host, ... });
}
};# Option 1: node-postgres (pg) - Most popular
npm install pg
npm install @types/pg # TypeScript types
# Option 2: postgres.js - Modern, faster (minimum v3.4.5)
npm install postgres@^3.4.5# mysql2 (minimum v3.13.0)
npm install mysql2| Driver | Database | Pros | Cons | Min Version |
|---|---|---|---|---|
| pg | PostgreSQL | Most popular, stable, well-documented | Slightly slower than postgres.js | 8.13.0+ |
| postgres | PostgreSQL | Faster, modern API, streaming support | Newer (less community examples) | 3.4.5+ |
| mysql2 | MySQL | Promises, prepared statements, fast | Requires | 3.13.0+ |
import { Client } from "pg";
export default {
async fetch(request: Request, env: { HYPERDRIVE: Hyperdrive }, ctx: ExecutionContext) {
// Create client for this request
const client = new Client({
connectionString: env.HYPERDRIVE.connectionString
});
await client.connect();
try {
// Run query
const result = await client.query('SELECT $1::text as message', ['Hello from Hyperdrive!']);
return Response.json(result.rows);
} catch (error) {
return new Response(`Database error: ${error.message}`, { status: 500 });
} finally {
// CRITICAL: Clean up connection after response
ctx.waitUntil(client.end());
}
}
};import { Pool } from "pg";
export default {
async fetch(request: Request, env: { HYPERDRIVE: Hyperdrive }, ctx: ExecutionContext) {
// Create pool (max 5 to stay within Workers' 6 connection limit)
const pool = new Pool({
connectionString: env.HYPERDRIVE.connectionString,
max: 5 // CRITICAL: Workers limit is 6 concurrent external connections
});
try {
// Run parallel queries
const [users, posts] = await Promise.all([
pool.query('SELECT * FROM users LIMIT 10'),
pool.query('SELECT * FROM posts LIMIT 10')
]);
return Response.json({
users: users.rows,
posts: posts.rows
});
} finally {
ctx.waitUntil(pool.end());
}
}
};import postgres from "postgres";
export default {
async fetch(request: Request, env: { HYPERDRIVE: Hyperdrive }, ctx: ExecutionContext) {
const sql = postgres(env.HYPERDRIVE.connectionString, {
max: 5, // Max 5 connections (Workers limit: 6)
fetch_types: false, // Disable if not using array types (reduces latency)
prepare: true // CRITICAL: Enable prepared statements for caching
});
try {
const users = await sql`SELECT * FROM users LIMIT 10`;
return Response.json({ users });
} finally {
ctx.waitUntil(sql.end({ timeout: 5 }));
}
}
};import { createConnection } from "mysql2/promise";
export default {
async fetch(request: Request, env: { HYPERDRIVE: Hyperdrive }, ctx: ExecutionContext) {
const connection = await createConnection({
host: env.HYPERDRIVE.host,
user: env.HYPERDRIVE.user,
password: env.HYPERDRIVE.password,
database: env.HYPERDRIVE.database,
port: env.HYPERDRIVE.port,
disableEval: true // REQUIRED: eval() not supported in Workers
});
try {
const [rows] = await connection.query('SELECT * FROM users LIMIT 10');
return Response.json({ users: rows });
} finally {
ctx.waitUntil(connection.end());
}
}
};import { Client } from "pg";
const client = new Client({ connectionString: env.HYPERDRIVE.connectionString });
await client.connect();
const result = await client.query('SELECT ...');
ctx.waitUntil(client.end());import { Pool } from "pg";
const pool = new Pool({
connectionString: env.HYPERDRIVE.connectionString,
max: 5 // CRITICAL: Stay within Workers' 6 connection limit
});
const [result1, result2] = await Promise.all([
pool.query('SELECT ...'),
pool.query('SELECT ...')
]);
ctx.waitUntil(pool.end());ctx.waitUntil()export default {
async fetch(request, env, ctx) {
const client = new Client({ connectionString: env.HYPERDRIVE.connectionString });
await client.connect();
try {
const result = await client.query('SELECT ...');
return Response.json(result.rows); // Response sent here
} finally {
// This runs AFTER response is sent (non-blocking)
ctx.waitUntil(client.end());
}
}
};ctx.waitUntil()await client.end(); // ❌ Blocks response, adds latencynpm install drizzle-orm postgres dotenv
npm install -D drizzle-kitsrc/db/schema.tsimport { pgTable, serial, varchar, timestamp } from "drizzle-orm/pg-core";
export const users = pgTable("users", {
id: serial("id").primaryKey(),
name: varchar("name", { length: 255 }).notNull(),
email: varchar("email", { length: 255 }).notNull().unique(),
createdAt: timestamp("created_at").defaultNow(),
});import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { users } from "./db/schema";
export default {
async fetch(request, env: { HYPERDRIVE: Hyperdrive }, ctx) {
const sql = postgres(env.HYPERDRIVE.connectionString, { max: 5 });
const db = drizzle(sql);
const allUsers = await db.select().from(users);
ctx.waitUntil(sql.end());
return Response.json({ users: allUsers });
}
};npm install prisma @prisma/client
npm install pg @prisma/adapter-pgnpx prisma initprisma/schema.prismagenerator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
name String
email String @unique
createdAt DateTime @default(now())
}npx prisma generate --no-engineimport { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "@prisma/client";
import { Pool } from "pg";
export default {
async fetch(request, env: { HYPERDRIVE: Hyperdrive }, ctx) {
// Create driver adapter with Hyperdrive connection
const pool = new Pool({ connectionString: env.HYPERDRIVE.connectionString, max: 5 });
const adapter = new PrismaPg(pool);
const prisma = new PrismaClient({ adapter });
const users = await prisma.user.findMany();
ctx.waitUntil(pool.end());
return Response.json({ users });
}
};@prisma/adapter-pgCLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING># If your binding is named "HYPERDRIVE"
export CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE="postgres://user:password@localhost:5432/local_db"
# Start local dev server
npx wrangler dev{
"hyperdrive": [
{
"binding": "HYPERDRIVE",
"id": "production-hyperdrive-id",
"localConnectionString": "postgres://user:password@localhost:5432/local_db"
}
]
}npx wrangler dev --remote-- ✅ Cached
SELECT * FROM articles WHERE published = true ORDER BY date DESC LIMIT 50;
SELECT COUNT(*) FROM users;
SELECT * FROM products WHERE category = 'electronics';
-- ❌ NOT Cached
INSERT INTO users (name, email) VALUES ('John', 'john@example.com');
UPDATE posts SET published = true WHERE id = 123;
DELETE FROM sessions WHERE expired = true;
SELECT LASTVAL(); -- PostgreSQL volatile function
SELECT LAST_INSERT_ID(); -- MySQL volatile functionconst sql = postgres(env.HYPERDRIVE.connectionString, {
prepare: true // CRITICAL for caching
});prepare: trueconst response = await fetch('https://your-worker.dev/api/users');
const cacheStatus = response.headers.get('cf-cache-status');
// Values: HIT, MISS, BYPASS, EXPIREDrequireverify-caverify-fullnpx wrangler cert upload certificate-authority \
--ca-cert root-ca.pem \
--name my-ca-certnpx wrangler hyperdrive create my-db \
--connection-string="postgres://..." \
--ca-certificate-id <CA_CERT_ID> \
--sslmode verify-fullnpx wrangler cert upload mtls-certificate \
--cert client-cert.pem \
--key client-key.pem \
--name my-client-certnpx wrangler hyperdrive create my-db \
--connection-string="postgres://..." \
--mtls-certificate-id <CERT_PAIR_ID># macOS
brew install cloudflare/cloudflare/cloudflared
# Linux
wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64cloudflared tunnel create my-db-tunnelconfig.ymltunnel: <TUNNEL_ID>
credentials-file: /path/to/credentials.json
ingress:
- hostname: db.example.com
service: tcp://localhost:5432 # Your private database
- service: http_status:404cloudflared tunnel run my-db-tunnelnpx wrangler hyperdrive create my-private-db \
--connection-string="postgres://user:password@db.example.com:5432/database"nodejs_compatcompatibility_flagsctx.waitUntil(client.end())max: 5prepare: truedisableEval: truewrangler devnodejs_compatawait client.end()ctx.waitUntil()# Create Hyperdrive configuration
wrangler hyperdrive create <name> --connection-string="postgres://..."
# List all Hyperdrive configurations
wrangler hyperdrive list
# Get details of a configuration
wrangler hyperdrive get <hyperdrive-id>
# Update connection string
wrangler hyperdrive update <hyperdrive-id> --connection-string="postgres://..."
# Delete configuration
wrangler hyperdrive delete <hyperdrive-id>
# Upload CA certificate
wrangler cert upload certificate-authority --ca-cert <file>.pem --name <name>
# Upload client certificate pair
wrangler cert upload mtls-certificate --cert <cert>.pem --key <key>.pem --name <name>PREPAREEXECUTEDEALLOCATELISTENNOTIFYUSECOM_STMT_PREPARECOM_INIT_DBcaching_sha2_passwordmysql_native_passwordprepare: truereferences/troubleshooting.md| Error | Solution |
|---|---|
| "No such module 'node:*'" | Add |
| "TLS not supported by database" | Enable SSL/TLS on your database |
| "Connection refused" | Check firewall rules, allow public internet or use Tunnel |
| "Failed to acquire connection" | Use |
| "Code generation from strings disallowed" | Set |
| "Bad hostname" | Verify DNS resolves, check for typos |
| "Invalid database credentials" | Check username/password (case-sensitive) |
const client = new Client({
host: 'db.example.com',
user: 'myuser',
password: 'mypassword',
database: 'mydb',
port: 5432
});const client = new Client({
connectionString: env.HYPERDRIVE.connectionString
});# Create new config with new credentials
wrangler hyperdrive create my-db-v2 --connection-string="postgres://..."
# Update wrangler.jsonc to use new ID
# Deploy gradually (no downtime)
# Delete old config when migration completewrangler hyperdrive update <id> --connection-string="postgres://new-credentials@..."templates/postgres-basic.tspostgres-pool.tspostgres-js.tsmysql2-basic.tsdrizzle-postgres.tsdrizzle-mysql.tsprisma-postgres.ts