Neon Local makes it easy to spin up short-lived, isolated Postgres environments using Docker

Connect a Hono application to Neon

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

Hono is a lightweight, multi-runtime web framework for the Edge, Node.js, Deno, Bun, and other runtimes. This topic describes how to create a Neon project and access it from a Hono application.

To create a Neon project and access it from a Hono 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 Hono project and add dependencies

    1. Create a Hono project if you do not have one. For instructions, see Quick Start, in the Hono documentation.

    2. Add project dependencies using one of the following commands:

    Neon serverless driver
    postgres.js
    node-postgres
    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 connection details by clicking Connect on the Neon Project Dashboard. 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 Hono application (e.g., in src/index.ts or a specific route file), 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
    node-postgres
    import { Hono } from 'hono';
    import { serve } from '@hono/node-server';
    import { neon } from '@neondatabase/serverless';
    
    const app = new Hono();
    
    app.get('/', async (c) => {
      try {
        const sql = neon(process.env.DATABASE_URL);
        const response = await sql`SELECT version()`;
        return c.json({ version: response[0]?.version });
      } catch (error) {
        console.error('Database query failed:', error);
        return c.text('Failed to connect to database', 500);
      }
    });
    
    serve(app);
  5. 5. Run the app

    Start your Hono development server. You can use the following command:

    npm run dev

    Navigate to your application's URL (localhost:3000). You should see a JSON response with the PostgreSQL version:

    {
      "version": "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 you are using.

Source code

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

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?