Skip to content

Cloudflare Workers database integration

Use the Cloudflare database integration feature to automatically connect Workers to your PlanetScale database.


Cloudflare Workers database integration is designed to connect your Cloudflare Workers to data sources automatically by generating connection strings and storing them in the worker's secrets.

This article will utilize a sample repository that is a preconfigured Cloudflare Worker you can use to deploy to your Cloudflare account.


Set up the database

  1. Create a database in your PlanetScale account named bookings_db.
pscale database create bookings_db
  1. Connect to the main branch of the new database.
pscale shell bookings_db main
  1. Run the following commands to create a table in the database and populate it with some data.
address VARCHAR(50) NOT NULL,
INSERT INTO hotels (name, address, stars) VALUES
('Hotel California', '1967 Can Never Leave Ln, San Fancisco CA, 94016', 7.6),
('The Galt House', '140 N Fourth St, Louisville, KY 40202', 8.0);

Deploy the Cloudflare Worker

  1. Clone the sample repository.
git clone
  1. Navigate to the worker folder of the repository and install the dependencies.
cd cloudflare-workers-quickstart/worker
npm install
  1. Deploy the Worker to your Cloudflare account.
npx wrangler publish

Configure the Cloudflare PlanetScale integration

  1. Log into the Cloudflare dashboard and navigate to "Workers" > "Overview". You should see a service in the list named "planetscale-worker". Select it from the list.

  1. Select the "Settings" tab, then "Integrations", and finally "Add Integration" in the PlanetScale card.

  1. Click "Accept" under Review and grant permissions to allow the wizard to write the database connection details to the Worker secrets.

  1. Under Connect to PlanetScale, click "Connect" to start the process of connecting your PlanetScale and Cloudflare accounts.

  1. A modal will appear allowing you to grant access to your organization, database, and branch. Start by selecting your organization from the list. This demonstration uses an organization named “ps-deved”.

  1. Select the “bookings_db” database from the list in the Databases card, and the “main” branch from the list in the Branches card. Finally, click "Authorize access".

  1. Select your organization again from the list and click "Continue".

  1. Select your database and the user role you want the integration to have.

  1. Select the “main” branch from the list and click "Continue".

  1. You’ll be given the option to rename the secrets that will be configured on your behalf. These can be left as is. Click "Add Integration" to complete the process.

Test the integration

Back in the overview of the Worker, there is a preview URL that you can use to open a new tab in your browser that runs the Worker and displays the results. Once you’ve located the preview URL, click it to test the Worker.

Once the integration is configured, you can also run the project on your computer using:

npx wrangler dev

This will automatically use the secrets defined in Cloudflare to run the Worker on your computer.

Test other database operations (optional)

To test other database operations that are mapped to HTTP methods, you may use the provided tests.http file which is designed to work with the VSCode REST client plugin. The file is preconfigured to work with the local environment, or you can change the @host variable to match the URL provided in the Cloudflare dashboard that cooresponds with your Worker project.

GET /Get a list of all hotels.
POST /Create a hotel.
PUT /:idUpdate a hotel.
DELETE /:idDelete a hotel.

What's next?

Once you're done with development, it is highly recommended that safe migrations be turned on for your main production branch to protect from accidental schema changes and enable zero-downtime deployments.

When you're ready to make more schema changes, you'll create a new branch off of your production branch. Branching your database creates an isolated copy of your production schema so that you can easily test schema changes in development. Once you're happy with the changes, you'll open a deploy request. This will generate a diff showing the changes that will be deployed, making it easy for your team to review.

Learn more about how PlanetScale allows you to make non-blocking schema changes to your database tables without locking or causing downtime for production databases.

Need help?

Get help from the PlanetScale support team, or join our GitHub discussion board to see how others are using PlanetScale.

Was this page useful?
Last updated on Help us improve this page