Skip to main content
Lakebase Postgres is built on object storage, but 99.99999% of reads are served from high-performance local caches
/Frameworks/RedwoodSDK

Connect a RedwoodSDK application to Neon

Set up a project on Neon in seconds and connect from a Redwood application

View as Markdown
Pre-built prompt for connecting RedwoodSDK applications to Lakebase Postgres

RedwoodSDK is a framework for building full-stack applications on Cloudflare. This guide describes how to create a Neon project and access it from a RedwoodSDK application.

To create a project on Neon and access it from a RedwoodSDK application:

  1. Create a Neon project

    If you do not have one already, create a Neon project. Save your connection details including your password. They are required when defining connection settings.

    1. Navigate to the Projects page in the Neon Console.
    2. Click New Project.
    3. Specify your project settings and click Create Project.
  2. Create a RedwoodSDK project and add dependencies

    1. Create a RedwoodSDK project if you do not have one. Run the following command and follow the prompts:

      npx create-rwsdk@latest my-redwood-app

      For more information, see the RedwoodSDK Quickstart.

    2. Navigate into your new project directory and install the RedwoodSDK dependencies:

      cd my-redwood-app
      npm install
    3. Install the Neon serverless driver. It connects over HTTP or WebSockets, so it works in Cloudflare Workers:

      npm install @neondatabase/serverless
  3. Store your Neon credentials

    Add a .env file to your project directory and add your Neon connection string to it. You can find your Neon database connection string by clicking the Connect button in the Console nav to open the Connect to your branch modal. For more information, see Connect from any application.

    DATABASE_URL="postgresql://[user]:[password]@[neon_hostname]/[dbname]?sslmode=require&channel_binding=require"

    RedwoodSDK reads this file during local development. Don't commit .env to version control.

  4. Configure the Postgres client

    In your RedwoodSDK application, open src/app/pages/home.tsx. This is the starter home page. Replace its contents with the following code. It imports the driver, reads DATABASE_URL from the Workers env object, and queries the database inside the route handler:

    src/app/pages/home.tsx
    import { RequestInfo } from "rwsdk/worker";
    import { neon } from '@neondatabase/serverless';
    import { env } from "cloudflare:workers";
    
    async function getData() {
      const sql = neon(env.DATABASE_URL);
      const response = await sql`SELECT version()`;
      return response[0].version;
    }
    
    export async function Home({ ctx }: RequestInfo) {
      const data = await getData();
      return <>{data}</>;
    }
  5. Run your RedwoodSDK application

    Generate the required Wrangler types for RedwoodSDK to detect environment variables:

    npx wrangler types

    Start the development server:

    npm run dev

    Navigate to localhost:5173 in your browser. You should see a response similar to the following, indicating a successful connection to your Neon database:

    PostgreSQL 18.6 (ac70818) on aarch64-unknown-linux-gnu, compiled by gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0, 64-bit

    The specific version may vary depending on the PostgreSQL version of your Neon project.

Resources

Next steps

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?
Edit on GitHub