Skip to content

Commit cb2af36

Browse files
authored
add hyperdrive wrangler dev support for remote db's (#26545)
* add hyperdrive wrangler dev support for remote db's * update date on wrangler dev with remote databases
1 parent 1fe9193 commit cb2af36

File tree

4 files changed

+150
-55
lines changed

4 files changed

+150
-55
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: Connect to remote databases during local development with wrangler dev
3+
description: You can now connect directly to remote databases from wrangler dev using localConnectionString, with TLS support enabling secure connections while keeping your Worker code running locally
4+
products:
5+
- hyperdrive
6+
date: 2025-12-04
7+
---
8+
9+
You can now connect directly to remote databases and databases requiring TLS with `wrangler dev`.
10+
This lets you run your Worker code locally while connecting to remote databases, without needing to use `wrangler dev --remote`.
11+
12+
The `localConnectionString` field and `CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME>` environment variable can be used to configure the connection string used by `wrangler dev`.
13+
14+
```jsonc
15+
{
16+
"hyperdrive": [
17+
{
18+
"binding": "HYPERDRIVE",
19+
"id": "your-hyperdrive-id",
20+
"localConnectionString": "postgres://user:[email protected]:5432/database?sslmode=require"
21+
}
22+
]
23+
}
24+
```
25+
26+
Learn more about [local development with Hyperdrive](/hyperdrive/configuration/local-development/).

src/content/docs/hyperdrive/configuration/local-development.mdx

Lines changed: 65 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,87 +7,109 @@ sidebar:
77

88
import { WranglerConfig } from "~/components";
99

10-
Hyperdrive can be used when developing and testing your Workers locally by connecting to a local database instance running on your machine directly, or a remote database instance. Local development uses [Wrangler](/workers/wrangler/install-and-update/), the command-line interface for Workers, to manage local development sessions and state.
10+
Hyperdrive can be used when developing and testing your Workers locally. [Wrangler](/workers/wrangler/install-and-update/), the command-line interface for Workers, provides two options for local development:
1111

12-
:::note[Note]
12+
- **`wrangler dev`** (default): Runs your Worker code locally on your machine. You configure a `localConnectionString` to connect directly to a database (either local or remote). Hyperdrive query caching does not take effect in this mode.
13+
- **`wrangler dev --remote`**: Runs your Worker on Cloudflare's using your deployed Hyperdrive configuration. This is useful for testing with Hyperdrive's connection pooling and query caching enabled.
1314

14-
You can connect to a local database for local development using the local connection string configurations for Wrangler, as detailed below. This allows you to connect to a local database instance running on your machine directly.
15+
## Use `wrangler dev`
16+
17+
By default, `wrangler dev` runs your Worker code locally on your machine. To connect to a database during local development, configure a `localConnectionString` that points directly to your database.
18+
19+
The `localConnectionString` works with both local and remote databases:
20+
21+
- **Local databases**: Connect to a database instance running on your machine (for example, `postgres://user:password@localhost:5432/database`)
22+
- **Remote databases**: Connect directly to remote databases over TLS (for example, `postgres://user:[email protected]:5432/database?sslmode=require` or `mysql://user:[email protected]:3306/database?sslMode=required`). You must specify the SSL/TLS mode if required.
1523

16-
You can also connect to a remote database for remote development using the `npx wrangler dev --remote` command, which will use the remote database configured for your Hyperdrive configuration.
24+
:::note
25+
26+
When using `localConnectionString`, Hyperdrive's connection pooling and query caching do not take effect. Your Worker connects directly to the database without going through Hyperdrive.
1727

1828
:::
1929

20-
## Configure local development
30+
### Configure with environment variable
31+
32+
The recommended approach is to use an environment variable to avoid committing credentials to source control:
2133

22-
To specify a database to connect to when developing locally, you can:
34+
```sh
35+
# Your configured Hyperdrive binding is "HYPERDRIVE"
36+
export CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE="postgres://user:password@your-database-host:5432/database"
37+
npx wrangler dev
38+
```
2339

24-
- **Recommended:** Create a `CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME>` [environmental variable](/workers/configuration/environment-variables/) with the connection string of your database. `<BINDING_NAME>` is the name of the binding assigned to your Hyperdrive in your [Wrangler configuration file](/workers/wrangler/configuration/) or Pages configuration. This allows you to avoid committing potentially sensitive credentials to source control in your Wrangler configuration file, if your test/development database is not ephemeral. If you have configured multiple Hyperdrive bindings, replace `<BINDING_NAME>` with the unique binding name for each.
25-
- Set `localConnectionString` in the Wrangler configuration file.
40+
The environment variable format is `CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME>`, where `<BINDING_NAME>` is the name of the binding assigned to your Hyperdrive in your [Wrangler configuration file](/workers/wrangler/configuration/).
2641

27-
If both the `CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME>` [environmental variable](/workers/configuration/environment-variables/) and `localConnectionString` in the Wrangler configuration file are set, `wrangler dev` will use the environmental variable instead. Use `unset CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME>` to unset any existing environmental variables.
42+
To unset an environment variable: `unset CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME>`
2843

29-
For example, to use the environmental variable, export the environmental variable before running `wrangler dev`:
44+
For example, to set the connection string for a local database:
3045

3146
```sh
32-
# Your configured Hyperdrive binding is "TEST_DB"
33-
export CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_TEST_DB="postgres://user:password@localhost:5432/databasename"
34-
# Start a local development session referencing this local instance
47+
export CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE="postgres://user:password@localhost:5432/databasename"
3548
npx wrangler dev
3649
```
3750

38-
To configure a `localConnectionString` in the [Wrangler configuration file](/workers/wrangler/configuration/), ensure your Hyperdrive bindings have a `localConnectionString` property set:
51+
### Configure in wrangler.toml
52+
53+
Alternatively, you can set `localConnectionString` in your [Wrangler configuration file](/workers/wrangler/configuration/):
3954

4055
<WranglerConfig>
4156

4257
```toml
4358
[[hyperdrive]]
44-
binding = "TEST_DB"
59+
binding = "HYPERDRIVE"
4560
id = "c020574a-5623-407b-be0c-cd192bab9545"
4661
localConnectionString = "postgres://user:password@localhost:5432/databasename"
4762
```
4863

4964
</WranglerConfig>
5065

51-
## Use `wrangler dev`
66+
If both an environment variable and `localConnectionString` in the Wrangler configuration file are set, the environment variable takes precedence.
5267

53-
The following example shows you how to check your wrangler version, set a `CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_TEST_DB` [environmental variable](/workers/configuration/environment-variables/), and run a `wrangler dev` session:
68+
## Use `wrangler dev --remote`
5469

55-
```sh
56-
# Confirm you are using wrangler v3.0+
57-
npx wrangler --version
58-
```
70+
When you run `wrangler dev --remote`, your Worker runs in Cloudflare's network and uses your deployed Hyperdrive configuration. This means:
5971

60-
```sh output
61-
⛅️ wrangler 3.27.0
62-
```
72+
- Your Worker code executes in Cloudflare's production environment, not locally
73+
- Hyperdrive's connection pooling and query caching are active
74+
- You connect to the database configured in your Hyperdrive configuration (created with `wrangler hyperdrive create`)
75+
- Changes made during the session interact with remote resources
6376

64-
```sh
65-
# Set your environmental variable: your configured Hyperdrive binding is "TEST_DB".
66-
export CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_TEST_DB="postgres://user:password@localhost:5432/databasename"
77+
This mode is useful for testing how your Worker behaves with Hyperdrive's features enabled before deploying.
78+
79+
Configure your Hyperdrive binding in `wrangler.jsonc`:
80+
81+
<WranglerConfig>
82+
83+
```jsonc
84+
{
85+
"hyperdrive": [
86+
{
87+
"binding": "HYPERDRIVE",
88+
"id": "your-hyperdrive-id",
89+
},
90+
],
91+
}
6792
```
6893

94+
</WranglerConfig>
95+
96+
To start a remote development session:
97+
6998
```sh
70-
# Start a local dev session:
71-
npx wrangler dev
99+
npx wrangler dev --remote
72100
```
73101

74-
```sh output
75-
------------------
76-
Found a non-empty CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_TEST_DB variable. Hyperdrive will connect to this database
77-
during local development.
102+
:::note
78103

79-
wrangler dev now uses local mode by default, powered by 🔥 Miniflare and 👷 workerd.
80-
To run an edge preview session for your Worker, use wrangler dev --remote
81-
Your worker has access to the following bindings:
82-
- Hyperdrive configs:
83-
- TEST_DB: c020574a-5623-407b-be0c-cd192bab9545
84-
⎔ Starting local server...
104+
The `localConnectionString` field is not used with `wrangler dev --remote`. Instead, your Worker connects to the database configured in your deployed Hyperdrive configuration.
85105

86-
[mf:inf] Ready on http://127.0.0.1:8787/
87-
[b] open a browser, [d] open Devtools, [l] turn off local mode, [c] clear console, [x] to exit
88-
```
106+
:::
89107

90-
`wrangler dev` separates local and production (remote) data. A local session does not have access to your production data by default. To access your production (remote) Hyperdrive configuration, pass the `--remote` flag when calling `wrangler dev`. Any changes you make when running in `--remote` mode cannot be undone.
108+
:::caution
109+
110+
Use `wrangler dev --remote` with caution. Since your Worker runs in Cloudflare's production environment, any database writes or side effects will affect your production data.
111+
112+
:::
91113

92114
Refer to the [`wrangler dev` documentation](/workers/wrangler/commands/#dev) to learn more about how to configure a local development session.
93115

src/content/docs/hyperdrive/get-started.mdx

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ Before you begin, ensure you have completed the following:
3333

3434
1. Sign up for a [Cloudflare account](https://dash.cloudflare.com/sign-up/workers-and-pages) if you have not already.
3535
2. Install [`Node.js`](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm). Use a Node version manager like [nvm](https://github.com/nvm-sh/nvm) or [Volta](https://volta.sh/) to avoid permission issues and change Node.js versions. [Wrangler](/workers/wrangler/install-and-update/) requires a Node version of `16.17.0` or later.
36-
3. Have PostgreSQL/MySQL (or compatible) database, accessible in one of the following ways:
37-
* **Publicly Accessible**: Your database is directly reachable from the Internet.
38-
* **Private Network**: Your database is in a private network (like a VPC) and can be securely connected using [Cloudflare Tunnel](/hyperdrive/configuration/connect-to-private-database/).
39-
36+
3. Have a publicly accessible PostgreSQL or MySQL (or compatible) database. _If your database is in a private network (like a VPC)_, refer to [Connect to a private database](/hyperdrive/configuration/connect-to-private-database/) for instructions on using Cloudflare Tunnel with Hyperdrive.
4037

4138
## 1. Log in
4239

@@ -111,7 +108,7 @@ Hyperdrive accepts the combination of these parameters in the common connection
111108
<TabItem label="PostgreSQL">
112109
```txt
113110
114-
postgres://USERNAME:PASSWORD@HOSTNAME_OR_IP_ADDRESS:PORT/database_name
111+
postgres://USERNAME:PASSWORD@HOSTNAME_OR_IP_ADDRESS:PORT/database_name
115112
116113
```
117114

@@ -193,7 +190,11 @@ To install `pg`, ensure you are in the `hyperdrive-tutorial` directory. Open you
193190

194191
If you are using TypeScript, you should also install the type definitions for `pg`:
195192

196-
<PackageManagers pkg="@types/pg" dev comment="This should install v8.13.0 or later" />
193+
<PackageManagers
194+
pkg="@types/pg"
195+
dev
196+
comment="This should install v8.13.0 or later"
197+
/>
197198

198199
With the driver installed, you can now create a Worker script that queries your database.
199200

@@ -236,10 +237,10 @@ export interface Env {
236237
export default {
237238
async fetch(request, env, ctx): Promise<Response> {
238239
// Create a client using the pg driver (or any supported driver, ORM or query builder)
239-
// with the Hyperdrive credentials. These credentials are only accessible from your Worker.
240-
const sql = new Client({
241-
connectionString: env.HYPERDRIVE.connectionString,
242-
});
240+
// with the Hyperdrive credentials. These credentials are only accessible from your Worker.
241+
const sql = new Client({
242+
connectionString: env.HYPERDRIVE.connectionString,
243+
});
243244

244245
try {
245246
// Connect to the database
@@ -346,7 +347,45 @@ Upon receiving a request, the code above does the following:
346347

347348
### Run in development mode (optional)
348349

349-
You can start your project in development mode. If you want to connect to the local database configured in your `wrangler.jsonc` with `localConnectionString` (e.g. `localhost:5432`), you can now run `wrangler dev`. This will connect directly to your database from your local Worker project emulation (Hyperdrive caching will not take effect, and the local database must not require SSL). To develop using a remote database, you can connect to your Hyperdrive configuration by running `wrangler dev --remote`.
350+
You can test your Worker locally before deploying by running `wrangler dev`. This runs your Worker code on your machine while connecting to your database.
351+
352+
The `localConnectionString` field works with both local and remote databases and allows you to connect directly to your database from your Worker project running locally. You must specify the SSL/TLS mode if required (`sslmode=require` for Postgres, `sslMode=REQUIRED` for MySQL).
353+
354+
To connect to a database during local development, configure `localConnectionString` in your `wrangler.jsonc`:
355+
356+
```jsonc
357+
{
358+
"hyperdrive": [
359+
{
360+
"binding": "HYPERDRIVE",
361+
"id": "your-hyperdrive-id",
362+
"localConnectionString": "postgres://user:password@your-database-host:5432/database",
363+
},
364+
],
365+
}
366+
```
367+
368+
Or set an environment variable:
369+
370+
```sh
371+
export CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE="postgres://user:password@your-database-host:5432/database"
372+
```
373+
374+
Then start local development:
375+
376+
```sh
377+
npx wrangler dev
378+
```
379+
380+
:::note
381+
382+
When using `wrangler dev` with `localConnectionString` or `CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE`, Hyperdrive caching does not take effect locally.
383+
384+
Alternatively, you can run `wrangler dev --remote` to test against your deployed Hyperdrive configuration with caching enabled, but this runs your entire Worker in Cloudflare's network instead of locally.
385+
386+
Learn more about [local development with Hyperdrive](/hyperdrive/configuration/local-development/).
387+
388+
:::
350389

351390
## 6. Deploy your Worker
352391

@@ -369,4 +408,4 @@ By finishing this tutorial, you have created a Hyperdrive configuration, a Worke
369408
- How to [configure query caching](/hyperdrive/concepts/query-caching/).
370409
- [Troubleshooting common issues](/hyperdrive/observability/troubleshooting/) when connecting a database to Hyperdrive.
371410

372-
If you have any feature requests or notice any bugs, share your feedback directly with the Cloudflare team by joining the [Cloudflare Developers community on Discord](https://discord.cloudflare.com).
411+
If you have any feature requests or notice any bugs, share your feedback directly with the Cloudflare team by joining the [Cloudflare Developers community on Discord](https://discord.cloudflare.com).

src/content/release-notes/hyperdrive.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ link: "/hyperdrive/platform/release-notes/"
33
productName: Hyperdrive
44
productLink: "/hyperdrive/"
55
entries:
6+
- publish_date: "2025-12-04"
7+
title: Connect to remote databases during local development with wrangler dev
8+
description: |-
9+
The `localConnectionString` configuration field and `CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME>` environment variable now support connecting to remote databases over TLS during local development with `wrangler dev`.
10+
11+
When using a remote database connection string, your Worker code runs locally on your machine while connecting directly to the remote database. Hyperdrive caching does not take effect.
12+
13+
Refer to [Local development](/hyperdrive/configuration/local-development/) for instructions on how to configure remote database connections for local development.
614
- publish_date: "2025-07-03"
715
title: Hyperdrive now supports configurable connection counts
816
description: |-

0 commit comments

Comments
 (0)