Powering next gen AI apps with Postgres 🚀 Learn More
Guides/Languages/Javascript

Connect a Node.js application to Neon

Set up a Neon project in seconds and connect from a Node.js application

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

note

The same configuration steps can be used for Express and Next.js applications.

To connect to Neon from a Node.js application:

  1. Create a Neon Project
  2. Create a NodeJS project and add dependencies
  3. Store your Neon credentials
  4. Configure the app.js file
  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 a NodeJS project and add dependencies

  1. Create a NodeJS project and change to the newly created directory.

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

    node-postgres
    postgres.js

Store your Neon credentials

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

note

A special endpoint connection option is appended to the connection string above: options=endpoint%3Dep-snowy-unit-550577. This option is used with Postgres clients such as node-postgres and Postgres.js that do not support Server Name Indication (SNI), which Neon relies on to route incoming connections. For more information, see [connection errors](/docs/connect/connection errors#a-pass-the-endpoint-id-as-an-option).

important

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

Configure the app.js file

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

node-postgres
postgres.js

Run app.js

Run node app.js to view the result.

Need help?

Send a request to support@neon.tech, or join the Neon community forum.

Edit this page
Was this page helpful?