Skip to main content
Cloudflare Workers is a serverless platform that allows you to run your code at the edge, close to your users. Hyperdrive accelerates queries you make to existing databases.
Already created a PlanetScale Postgres database? Jump straight to integration instructions.
We’ll cover:
  • Creating a new Postgres database
  • Cluster configuration options
  • Connecting to your database

Prerequisites

Before you begin, make sure you have a PlanetScale account. After you create an account, you’ll be prompted to create a new organization, which is essentially a container for your databases, settings, and members. After creating your organization, it’s important to understand the relationship between databases, branches, and clusters.
  • Database: Your overall project (e.g., “my-ecommerce-app”)
  • Branch: Isolated database deployments that provide you with separate environments for development and testing, as well as restoring from backups - learn more about branching
  • Cluster: The underlying compute and storage infrastructure that powers each branch
PlanetScale Postgres clusters use real Postgres in a high-availability architecture with one primary and two replicas.

Create a new database

Step 1: Navigate to database creation

1
Log in to your PlanetScale dashboard
2
Select your organization from the dropdown
3
Click “New database” button or navigate to /new

Step 2: Choose database engine

1
On the database creation form, you’ll see two engine options:
  • Vitess (MySQL-compatible)
  • Postgres (PostgreSQL-compatible)
2
Select Postgres to create a PostgreSQL database

Step 3: Configure your database cluster

1
Database name: Enter a unique name for your database
This “name” is referenced in the PlanetScale Dashboard and APIs and not created as a logical database inside of Postgres.
2
Region: Choose the primary region where your database will be hosted. For the lowest latency, select a region near you or your application’s hosting location.
3
Cluster configuration: Select your preferred cluster size and CPU architecture

Step 4: Create the database cluster

1
Review your configuration settings
2
Click “Create database” to provision your Postgres database
3
Your database will be created with a main branch by default

What happens during creation

When you create a Postgres database cluster, PlanetScale automatically:
  • Provisions a PostgreSQL cluster in your selected region
  • Creates the initial main branch
  • Prepopulates Postgres with required default databases
  • Sets up monitoring and metrics collection
  • Configures backup and high availability settings

Integrate with Cloudflare Workers

Don’t have a Workers project yet? Create a Workers project from the Postgres Hyperdrive template.
Terminal
npm create cloudflare@latest -- --template=cloudflare/templates/postgres-hyperdrive-template

Step 1: Create a Hyperdrive connection

You can automatically create a connection from the PlanetScale dashboard when creating a new role, or use one of the methods below.
1
Log into the Cloudflare dashboard and navigate to “Compute & AI” > “Workers & Pages”. You should see your Worker in the list.
PlanetScale Cloudflare integration wizard - step 1
2
Select the “Storage & databases” tab, then “Hyperdrive”, and finally “Create configuration” in the top right corner.
PlanetScale Cloudflare integration wizard - step 2
3
Click “Connect to PlanetScale database” then click Next.
PlanetScale Cloudflare integration wizard - step 3
4
Click Login to PlanetScale to log in to your PlanetScale account.
PlanetScale Cloudflare integration wizard - step 4
5
The next screen will allow you to grant access to your organization, database, and branch. Start by selecting your organization from the list.
PlanetScale Cloudflare integration wizard - step 5
6
Select the database you created, the “main” branch and then click Next.
PlanetScale Cloudflare integration wizard - step 6
7
You should see a success screen with the connection binding details to add to your wrangler.json file.
PlanetScale Cloudflare integration wizard - step 7
8
You should now have metrics within your Cloudflare dashboard in the Hyperdrive page.
PlanetScale Cloudflare integration wizard - step 8

Configure Worker placement

By default, Workers run at the edge close to your users. For database-heavy workloads with multiple round trips, you can use placement hints to run your Worker closer to your PlanetScale database and reduce latency. Add a placement configuration to your wrangler.jsonc file with the region that matches your PlanetScale database region:
wrangler.json
{
  "placement": {
    "region": "aws:us-east-1"
  }
}
PlanetScale RegionPlacement Hint
AWS ap-northeast-1 (Tokyo)aws:ap-northeast-1
AWS ap-south-1 (Mumbai)aws:ap-south-1
AWS ap-southeast-1 (Singapore)aws:ap-southeast-1
AWS ap-southeast-2 (Sydney)aws:ap-southeast-2
AWS ca-central-1 (Montreal)aws:ca-central-1
AWS eu-central-1 (Frankfurt)aws:eu-central-1
AWS eu-west-1 (Dublin)aws:eu-west-1
AWS eu-west-2 (London)aws:eu-west-2
AWS sa-east-1 (Sao Paulo)aws:sa-east-1
AWS us-east-1 (N. Virginia)aws:us-east-1
AWS us-east-2 (Ohio)aws:us-east-2
AWS us-west-2 (Oregon)aws:us-west-2
GCP asia-northeast3 (Seoul)gcp:asia-northeast3
GCP europe-west1 (Belgium)gcp:europe-west1
GCP northamerica-northeast1 (Montreal)gcp:northamerica-northeast1
GCP us-central1 (Iowa)gcp:us-central1
GCP us-east4 (Virginia)gcp:us-east4
Your Worker will run in the Cloudflare data center with the lowest latency to the specified region. If your Worker connects to multiple back-end services and you’re unsure which region to specify, use "mode": "smart" for automatic placement based on measured latency:
wrangler.json
{
  "placement": {
    "mode": "smart"
  }
}

Step 2: Deploy your Worker

Run the following to deploy your Worker.
Terminal
npx wrangler deploy
For more information on using Cloudflare Workers and Hyperdrive, refer to the Cloudflare documentation.

What’s next?

Once you’re done with development, consider these next steps:
  • Create additional roles: Set up application-specific roles with appropriate permissions for your production workloads. The “Default role” is meant for administrative purposes and has significant privileges.
  • Use branching for development: Create database branches to safely test schema changes before deploying to production.
  • Set up monitoring: Configure query insights to monitor database performance and identify slow queries.

Need help?

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