We just shipped add-mcp: think npx skills but for MCPs. One command to install MCPs across all your editors and agents
/Integrations (3rd party)/Deploy/Cloudflare/Cloudflare Workers

Use Neon with Cloudflare Workers

Connect a Neon Postgres database to your Cloudflare Workers application

Cloudflare Workers is a serverless platform allowing you to deploy your applications globally across Cloudflare's network. It supports running JavaScript, TypeScript, and WebAssembly, making it a great choice for high-performance, low-latency web applications.

This guide demonstrates how to connect to a Neon Postgres database from your Cloudflare Workers application using two approaches:

  • Hyperdrive (recommended): Cloudflare's connection pooling service that provides the lowest possible latencies by performing database connection setup and connection pooling across Cloudflare's network. Hyperdrive is included in all Workers plans and supports native PostgreSQL drivers like node-postgres.
  • Neon serverless driver: A low-latency Postgres driver designed for serverless environments that connects over HTTP or WebSockets.

note

Hyperdrive is the recommended approach as it provides optimized connection pooling and fast query routing by connecting directly to your database. When using Hyperdrive with Neon, use native PostgreSQL drivers like node-postgres or Postgres.js instead of the Neon serverless driver.

Prerequisites

To follow along with this guide, you will need:

  • A Neon account. If you do not have one, sign up at Neon. Your Neon project comes with a ready-to-use Postgres database named neondb. We'll use this database in the following examples.
  • A Cloudflare account. If you do not have one, sign up at Cloudflare to get started.
  • Node.js and npm installed on your local machine. We'll use Node.js to build and deploy the Workers application.

Setting up your Neon database

Initialize a new project

Log in to the Neon Console and navigate to the Projects section.

  1. Click the New Project button to create a new project.

  2. From the Neon Dashboard, navigate to the SQL Editor from the sidebar, and run the following SQL command to create a new table in your database:

    CREATE TABLE books_to_read (
        id SERIAL PRIMARY KEY,
        title TEXT,
        author TEXT
    );

    Next, insert some sample data into the books_to_read table so that you can query it later:

    INSERT INTO books_to_read (title, author)
    VALUES
        ('The Way of Kings', 'Brandon Sanderson'),
        ('The Name of the Wind', 'Patrick Rothfuss'),
        ('Coders at Work', 'Peter Seibel'),
        ('1984', 'George Orwell');

Setting up your Cloudflare Workers project

Create a Hyperdrive user in Neon

To use Hyperdrive with Neon, you'll need to create a dedicated database role for Hyperdrive to use:

  1. In the Neon Console, navigate to your project.
  2. Select Branches from the sidebar, then select the branch you want to use.
  3. Navigate to the Roles & Databases section.
  4. Click New Role and enter hyperdrive-user as the name (or your preferred name).
  5. Copy the password that is generated. You'll use this password in the connection string in the next step.

Get your Neon connection string for Hyperdrive

  1. In the Neon Console, select Dashboard from the sidebar.

  2. Go to the Connection Details pane.

  3. Select the branch, database, and role (for example, hyperdrive-user) that Hyperdrive will connect through.

  4. Select Connection String from the dropdown menu.

  5. Important: Uncheck the Pooled connection checkbox. Hyperdrive manages connection pooling, so you need the direct connection string.

  6. Copy the connection string, which should look like this:

    postgres://hyperdrive-user:PASSWORD@ep-cool-darkness-123456.us-east-2.aws.neon.tech/dbname

Create a new Worker project

Run the following command in a terminal window to set up a new Cloudflare Workers project:

npm create cloudflare@latest

This initiates an interactive CLI prompt to generate a new project. To follow along with this guide, you can use the following settings:

 In which directory do you want to create your application?
 type my-neon-worker

 What would you like to start with?
 select "Hello World example"

 Which template would you like to use?
 select "Worker only"

 Which language do you want to use?
 select "TypeScript"

 Do you want to use git for version control?
 select "Yes"

 Do you want to deploy your application?
 select "No"

For the purpose of demonstration, we will use the Worker only template and will deploy the application later.

Navigate to your project directory:

cd my-neon-worker

Create a Hyperdrive configuration

If you are not already authenticated with your Cloudflare account, use the following command:

npx wrangler login

This command will open a browser window and prompt you to log into your Cloudflare account. After logging in, you can close the browser window and return to your terminal.

Now, create a Hyperdrive configuration with your Neon connection string:

npx wrangler hyperdrive create my-neon-hyperdrive --connection-string="postgres://USERNAME:PASSWORD@HOSTNAME:PORT/DATABASE"

Replace the placeholder values with your actual connection details from the previous step. You will then be prompted with "Would you like Wrangler to add it on your behalf?". Enter Y, and continue with the default name. This will add the required Hyperdrive configuration (bindings) to your project.

Install the node-postgres driver

For Hyperdrive, we'll use the native PostgreSQL driver node-postgres:

npm install pg
npm install -D @types/pg

Create types

To generate the types for your Hyperdrive binding, use the following command:

npm run cf-typegen

Configure wrangler.jsonc

Update your wrangler.jsonc to add the compatibility_flags binding. We will also update the Hyperdrive binding and add the localConnectionString. This will allow our locally running application to connect to the Neon database via Hyperdrive. The complete wrangler.jsonc file should be as follows:

{
  "$schema": "./node_modules/wrangler/config-schema.json",
  "name": "my-neon-worker",
  "main": "src/index.ts",
  "compatibility_flags": [
    "nodejs_compat"
  ],
  "compatibility_date": "2025-09-27",
  "hyperdrive": [
    {
      "binding": "HYPERDRIVE",
      "id": "<your-hyperdrive-id-here>",
      "localConnectionString": "postgres://USERNAME:PASSWORD@HOSTNAME:PORT/DATABASE"
    }
  ]
}

Replace the following placeholders:

  • localConnectionString with your Neon connection string (the same one you used to create the Hyperdrive configuration). This is required for local development with wrangler dev.

Implement the Worker script

Update the src/index.ts file in your project directory with the following code:

import { Client } from 'pg';

export default {
  async fetch(request, env, ctx) {
    const client = new Client({
      connectionString: env.HYPERDRIVE.connectionString
    });

    await client.connect();
    const result = await client.query('SELECT * FROM books_to_read;');

    // Clean up the client connection in the background
    ctx.waitUntil(client.end());

    return Response.json(result.rows);
  },
} satisfies ExportedHandler<Env>;

The fetch handler uses the Hyperdrive binding to connect to your Neon database through Cloudflare's optimized connection pooling service.

important

When using Hyperdrive with Neon, always use native PostgreSQL drivers like node-postgres (pg) or Postgres.js instead of the Neon serverless driver. Hyperdrive already provides optimized connection pooling and query routing for Workers.

Test the worker application locally

To test the worker application locally, run:

npm run dev

This command starts a local server and simulates the Cloudflare Workers environment.

 npm run dev

> my-neon-worker@0.0.0 dev
> wrangler dev


 ⛅️ wrangler 4.61.0
───────────────────
Your Worker has access to the following bindings:
Binding                                                Resource               Mode
env.HYPERDRIVE (YOUR_HYPERDRIVE_ID)      Hyperdrive Config      local

 Your types might be out of date. Re-run `wrangler types` to ensure your types are correct.
╭──────────────────────────────────────────────────────────────────────╮
  [b] open a browser [d] open devtools [c] clear console [x] to exit  │
╰──────────────────────────────────────────────────────────────────────╯
 Starting local server...
[wrangler:info] Ready on http://localhost:8787

Visit http://localhost:8787 in your browser to test the worker application. It should return a JSON response with the list of books from the books_to_read table.

Deploying your application with Cloudflare Workers

Authenticate Wrangler with your Cloudflare account

If you aren't authenticated, run the following command to link the Wrangler tool to your Cloudflare account:

npx wrangler login

This command will open a browser window and prompt you to log into your Cloudflare account. After logging in and approving, you can close the browser window and return to your terminal.

Configure secrets

note

If you're using Hyperdrive, your connection is already configured in the wrangler.jsonc file, so you can skip this step and proceed directly to publishing your Worker.

If you're using the Neon serverless driver, you need to add your connection string as a secret.

For the Neon serverless driver approach, use Wrangler to add your Neon database connection string as a secret to your Worker:

npx wrangler secret put DATABASE_URL

When prompted, paste your pooled Neon connection string.

Publish your Worker application

Now, you can deploy your application to Cloudflare Workers by running the following command:

npm run deploy

The Wrangler CLI will output the URL of your Worker hosted on the Cloudflare platform. Visit this URL in your browser or use curl to verify the deployment works as expected.

❯ npm run deploy
 ⛅️ wrangler 3.28.1
-------------------
Total Upload: 189.98 KiB / gzip: 49.94 KiB
Uploaded my-neon-worker (4.03 sec)
Published my-neon-worker (5.99 sec)
  https://my-neon-worker.anandishan2.workers.dev
Current Deployment ID: de8841dd-46e4-436d-b2c4-569e91f54c72

Removing the example application and Neon project

To delete your Worker, you can use the Cloudflare dashboard or run wrangler delete from your project directory, specifying your project name. Refer to the Wrangler documentation for more details.

If you used Hyperdrive, you should also delete the Hyperdrive configuration:

npx wrangler hyperdrive delete my-neon-hyperdrive

To delete your Neon project, follow the steps outlined in the Neon documentation under Delete a project.

Source code

You can find the source code for the application described in this guide on GitHub.

Resources

Need help?

Join our Discord Server to ask questions or see what others are doing with Neon. For paid plan support options, see Support.

Last updated on

Was this page helpful?