Hyperdrive makes connecting to your regional database from Cloudflare Workers fast. This project demonstrates a Worker connecting to a PostgreSQL database using Hyperdrive.
Upon loading your Worker, your will see the list of PostgreSQL tables in your database, as obtained with the following query:
SELECT * FROM pg_tables LIMIT 10;
Important
When using C3 to create this project, select "no" when it asks if you want to deploy. You need to follow this project's setup steps before deploying.
Outside of this repo, you can start a new project with this template using C3 (the create-cloudflare
CLI):
npm create cloudflare@latest -- --template=cloudflare/templates/postgres-hyperdrive-template
A live public deployment of this template is available at https://postgres-hyperdrive-template.templates.workers.dev
- Install the project dependencies with a package manager of your choice:
npm install
- Create a Hyperdrive configuration with the name "hyperdrive-configuration":
...and update the
npx wrangler hyperdrive create hyperdrive-configuration --connection-string="postgres://<DB_USER>:<DB_PASSWORD>@<DB_HOSTNAME_OR_IP_ADDRESS>:5432/<DATABASE_NAME>"
hyperdrive
id
field inwrangler.json
with the new Hyperdrive ID. You can also specify a connection string for a local PostgreSQL database used for development using thehyperdrive
localConnectionString
field. - Deploy the project!
npx wrangler deploy