Neon Deploy: Learn how Neon helps developers ship faster with Postgres. Grab your ticket

Connect an Express application to Neon

Set up a Neon project in seconds and connect from an Express application

This guide describes how to create a Neon project and connect to it from an Express application. Examples are provided for using the Neon serverless driver, node-postgres and Postgres.js clients. Use the client you prefer.

To connect to Neon from an Express application:

  1. Create a Neon Project
  2. Create an Express project and add dependencies
  3. Store your Neon credentials
  4. Configure the Postgres client
  5. Run app.js

Create a Neon project

If you do not have one already, create a Neon project.

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

Create an Express project and add dependencies

  1. Create an Express project and change to the newly created directory.

    mkdir neon-express-example
    cd neon-express-example
    npm init -y
    npm install express
  2. Add project dependencies using one of the following commands:

    Neon serverless driver
    node-postgres
    postgres.js
    npm install @neondatabase/serverless dotenv

Store your Neon credentials

Add a .env file to your project directory and add your Neon connection details to it. You can find the connection details for your database in the Connection Details widget on the Neon Dashboard. Please select Node.js from the Connection string dropdown. For more information, see Connect from any application.

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

important

To ensure the security of your data, never expose your Neon credentials to the browser.

Configure the Postgres client

Add an index.js file to your project directory and add the following code snippet to connect to your Neon database:

Neon serverless driver
node-postgres
postgres.js
require('dotenv').config();

const express = require('express');
const { neon } = require('@neondatabase/serverless');

const app = express();
const PORT = process.env.PORT || 4242;

app.get('/', async (_, res) => {
  const sql = neon(`${process.env.DATABASE_URL}`);
  const response = await sql`SELECT version()`;
  const { version } = response[0];
  res.json({ version });
});

app.listen(PORT, () => {
  console.log(`Listening to http://localhost:${PORT}`);
});

Run index.js

Run node index.js to view the result on localhost:4242 as follows:

{ version: 'PostgreSQL 16.0 on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit' }

Source code

You can find the source code for the application described in this guide 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?