Skip to main content
Neon Postgres Docs
current

Search documentation

Type to search this documentation.

On this pageOverview

Connect a RedwoodSDK application to Neon

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...

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. For instructions, see RedwoodSDK Quickstart.
    2. Navigate into your new project directory and install the RedwoodSDK dependencies:
      Bash
      cd my-redwood-app
      npm install
    3. Add project dependencies depending on the PostgreSQL driver you wish to use (postgres.js or @neondatabase/serverless):npm install @neondatabase/serverlessnpm install postgres
  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.

    Bash
    DATABASE_URL="postgresql://<user>:<password>@<endpoint_hostname>.neon.tech:<port>/<dbname>?sslmode=require&channel_binding=require"
  4. Configure the Postgres client

    In your RedwoodSDK application (for example, in src/app/pages/Home.tsx), import the driver and use it within your route handlers.

    Here's how you can set up a simple route to query the database:

    Neon serverless driver
    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}</>;
    }
    postgres.js
    import { RequestInfo } from "rwsdk/worker";
    import postgres from 'postgres';
    import { env } from "cloudflare:workers";
    
    async function getData() {
      const sql = postgres(env.DATABASE_URL, { ssl: 'require' });
      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:

    Bash
    npx wrangler types

    Start the development server:

    Bash
    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 17.5 (6bc9ef8) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14+deb12u1) 12.2.0, 64-bit

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

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

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu