Skip to content

Hyperdrive

Hyperdrive provides connection pooling for external PostgreSQL and MySQL databases. Unlike D1, it requires a publicly reachable database.

import { defineConfig, hyperdrive, workerEnv } from "wrangler-deploy";
const db = hyperdrive("payments-db");
export const api = workerEnv({
DB: db,
});
export default defineConfig({
version: 1,
workers: ["."],
resources: {
"payments-db": {
type: "hyperdrive",
bindings: { ".": "DB" },
},
},
});
{
"hyperdrive": [
{
"binding": "DB",
"id": "placeholder",
"localConnectionString": "postgresql://user:pass@localhost:5432/mydb",
},
],
}

The localConnectionString is used by wrangler dev for local development. wrangler-deploy replaces the id with the real Hyperdrive config ID.

Hyperdrive needs a connection string on first apply:

Terminal window
wd apply --stage staging --database-url "postgresql://user:pass@db.example.com:5432/mydb"
import type { api } from "../wrangler-deploy.config.ts";
import pg from "pg";
type Env = typeof api.Env;
// Env.DB is typed as Hyperdrive
export default {
async fetch(req: Request, env: Env) {
const client = new pg.Client(env.DB.connectionString);
await client.connect();
const { rows } = await client.query("SELECT * FROM users LIMIT 10");
await client.end();
return Response.json(rows);
},
};
D1Hyperdrive
External database neededNoYes
Database provisioningAutomaticManual
Ephemeral environmentsFull isolationShared database (or Neon branching)
SQL dialectSQLitePostgreSQL / MySQL
Best forNew projects, simple dataExisting databases, complex queries