Duplicate entire environments in seconds, with masked data - Use PostgreSQL Anonymizer and Neon branches

Connect a RedwoodSDK application to Neon

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

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 Neon project 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 Minimal Starter. To create a new project, run the following command:

      npx degit redwoodjs/sdk/starters/minimal my-redwood-app
    2. Navigate into your new project directory and install the RedwoodSDK dependencies:

      cd my-redwood-app
      npm install
    3. Add project dependencies using one of the following commands:

      Neon serverless driver
      postgres.js
      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 on your Project Dashboard to open the Connect to your database modal. For more information, see Connect from any application.

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

    In your RedwoodSDK application (e.g., 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
    postgres.js
    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

    Start the development server:

    npm dev

    Navigate to your application's URL (localhost:5173) in your browser. You should see the output of your database query.

    PostgreSQL 17.4 on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit

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

Source code

You can find a sample RedwoodSDK application configured for Neon on GitHub:

Resources

Need help?

Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more details, see Getting Support.

Last updated on

Was this page helpful?