Get started
Hyperdrive accelerates access to your existing databases from Cloudflare Workers, making even single-region databases feel globally distributed.
By maintaining a connection pool to your database within Cloudflare’s network, Hyperdrive reduces seven round-trips to your database before you can even send a query: the TCP handshake (1x), TLS negotiation (3x), and database authentication (3x).
Hyperdrive understands the difference between read and write queries to your database, and can cache the most common read queries, improving performance and reducing load on your origin database.
This guide will instruct you through:
- Creating your first Hyperdrive configuration.
- Creating a Cloudflare Worker and binding it to your Hyperdrive configuration.
- Establishing a database connection from your Worker to a public database.
To continue:
- Sign up for a Cloudflare account ↗ if you have not already.
- Install
Node.js
↗. Use a Node version manager like Volta ↗ or nvm ↗ to avoid permission issues and change Node.js versions. Wrangler requires a Node version of16.17.0
or later. - Have a publicly accessible PostgreSQL (or PostgreSQL compatible) database. Cloudflare recommends Neon ↗ if you do not have an existing database. Read the Neon documentation ↗ to create your first database.
Before creating your Hyperdrive binding, log in with your Cloudflare account by running:
You will be directed to a web page asking you to log in to the Cloudflare dashboard. After you have logged in, you will be asked if Wrangler can make changes to your Cloudflare account. Scroll down and select Allow to continue.
Create a new project named hyperdrive-tutorial
by running:
For setup, select the following options:
- For What would you like to start with?, choose
Hello World example
. - For Which template would you like to use?, choose
Hello World Worker
. - For Which language do you want to use?, choose
TypeScript
. - For Do you want to use git for version control?, choose
Yes
. - For Do you want to deploy your application?, choose
No
(we will be making some changes before deploying).
This will create a new hyperdrive-tutorial
directory. Your new hyperdrive-tutorial
directory will include:
- A
"Hello World"
Worker atsrc/index.ts
. - A
wrangler.toml
configuration file.wrangler.toml
is how yourhyperdrive-tutorial
Worker will connect to Hyperdrive.
Hyperdrive works by connecting to your database.
To create your first Hyperdrive database configuration, change into the directory you just created for your Workers project:
To create your first Hyperdrive, you will need:
- The IP address (or hostname) and port of your database.
- The database username (for example,
hyperdrive-demo
). - The password associated with that username.
- The name of the database you want Hyperdrive to connect to. For example,
postgres
.
Hyperdrive accepts the combination of these parameters in the common connection string format used by database drivers:
Most database providers will provide a connection string you can directly copy-and-paste directly into Hyperdrive.
To create a Hyperdrive connection, run the wrangler
command, replacing the placeholder values passed to the --connection-string
flag with the values of your existing database:
If successful, the command will output your new Hyperdrive configuration:
Copy the id
field: you will use this in the next step to make Hyperdrive accessible from your Worker script.
You must create a binding for your Worker to connect to your Hyperdrive configuration. Bindings allow your Workers to access resources, like D1, on the Cloudflare developer platform. You create bindings by updating your wrangler.toml
file.
To bind your Hyperdrive configuration to your Worker, add the following to the end of your wrangler.toml
file:
Specifically:
- The value (string) you set for the
name
(binding name) will be used to reference this database in your Worker. In this tutorial, name your bindingHYPERDRIVE
. - The binding must be a valid JavaScript variable name ↗. For example,
binding = "hyperdrive"
orbinding = "productionDB"
would both be valid names for the binding. - Your binding is available in your Worker at
env.<BINDING_NAME>
.
To connect to your database, you will need a database driver which allows you to authenticate and query your database. For this tutorial, you will use Postgres.js ↗, one of the most widely used PostgreSQL drivers.
To install postgres
, ensure you are in the hyperdrive-tutorial
directory. Open your terminal and run the following command:
With the driver installed, you can now create a Worker script that queries your database.
After you have set up your database, you will run a SQL query from within your Worker.
Go to your hyperdrive-tutorial
Worker and open the index.ts
file.
The index.ts
file is where you configure your Worker’s interactions with Hyperdrive.
Populate your index.ts
file with the following code:
Upon receiving a request, the code above does the following:
- Creates a new database client configured to connect to your database via Hyperdrive, using the Hyperdrive connection string.
- Initiates a query via
await sql
that outputs all tables (user and system created) in the database (as an example query). - Returns the response as JSON to the client.
You can now deploy your Worker to make your project accessible on the Internet. To deploy your Worker, run:
You can now visit the URL for your newly created project to query your live database.
For example, if the URL of your new Worker is hyperdrive-tutorial.<YOUR_SUBDOMAIN>.workers.dev
, accessing https://hyperdrive-tutorial.<YOUR_SUBDOMAIN>.workers.dev/
will send a request to your Worker that queries your database directly.
By finishing this tutorial, you have created a Hyperdrive configuration, a Worker to access that database and deployed your project globally.
- Learn more about how Hyperdrive works.
- How to configure query caching.
- Troubleshooting common issues when connecting a database to Hyperdrive.
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 ↗.