You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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`.
Copy file name to clipboardExpand all lines: src/content/docs/hyperdrive/configuration/local-development.mdx
+65-43Lines changed: 65 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,87 +7,109 @@ sidebar:
7
7
8
8
import { WranglerConfig } from"~/components";
9
9
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:
11
11
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.
13
14
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.
15
23
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.
17
27
18
28
:::
19
29
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:
21
33
22
-
To specify a database to connect to when developing locally, you can:
34
+
```sh
35
+
# Your configured Hyperdrive binding is "HYPERDRIVE"
-**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/).
26
41
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 environmentvariable: `unset CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_<BINDING_NAME>`
28
43
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:
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/):
If both an environment variable and `localConnectionString` in the Wrangler configuration file are set, the environment variable takes precedence.
52
67
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`
54
69
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:
59
71
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
63
76
64
-
```sh
65
-
# Set your environmental variable: your configured Hyperdrive binding is "TEST_DB".
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
+
}
67
92
```
68
93
94
+
</WranglerConfig>
95
+
96
+
To start a remote development session:
97
+
69
98
```sh
70
-
# Start a local dev session:
71
-
npx wrangler dev
99
+
npx wrangler dev --remote
72
100
```
73
101
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
78
103
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.
85
105
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
+
:::
89
107
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
+
:::
91
113
92
114
Refer to the [`wrangler dev` documentation](/workers/wrangler/commands/#dev) to learn more about how to configure a local development session.
Copy file name to clipboardExpand all lines: src/content/docs/hyperdrive/get-started.mdx
+51-12Lines changed: 51 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,10 +33,7 @@ Before you begin, ensure you have completed the following:
33
33
34
34
1. Sign up for a [Cloudflare account](https://dash.cloudflare.com/sign-up/workers-and-pages) if you have not already.
35
35
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.
40
37
41
38
## 1. Log in
42
39
@@ -111,7 +108,7 @@ Hyperdrive accepts the combination of these parameters in the common connection
@@ -346,7 +347,45 @@ Upon receiving a request, the code above does the following:
346
347
347
348
### Run in development mode (optional)
348
349
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`:
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
+
:::
350
389
351
390
## 6. Deploy your Worker
352
391
@@ -369,4 +408,4 @@ By finishing this tutorial, you have created a Hyperdrive configuration, a Worke
369
408
- How to [configure query caching](/hyperdrive/concepts/query-caching/).
370
409
-[Troubleshooting common issues](/hyperdrive/observability/troubleshooting/) when connecting a database to Hyperdrive.
371
410
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).
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.
6
14
- publish_date: "2025-07-03"
7
15
title: Hyperdrive now supports configurable connection counts
0 commit comments