Join us on March 29th for Developer Days!Learn More

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. You can find the below code sample on GitHub.


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. Save your connection details including your password. They are required when defining connection settings.

To create a Neon project:

  1. Navigate to the Projects page in the Neon Console.
  2. Click New Project.
  3. Specify a name, a PostgreSQL version, a region, and click Create Project.

For additional information about creating projects, see Set up a project.

Create a NodeJS project and add dependencies

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

    mkdir neon-nodejs-example
    cd neon-nodejs-example
    npm init -y
  2. Add project dependencies using the following command:

    npm install postgres dotenv

Store your Neon credentials

Store your Neon credentials in your .env file.



  • <hostname> the hostname of the branch's compute endpoint. The hostname has an ep- prefix and appears similar to this:
  • <dbname> is the name of the database. The default Neon database is neondb
  • <user> is the database user.
  • <password> is the database user's password, which is provided to you when you create a project.
  • <endpoint_id> is the ID of the compute endpoint that you are connecting to. The endpoint_id has an ep- prefix and appears similar to this: ep-tight-salad-272396.

You can find all of the connection details listed above, except for your password, in the Connection Details widget on the Neon Dashboard. For more information, see Connect from any application. If you have misplaced your password, see Reset a password.


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

Configure the app.js file

To connect to the database using the PostgreSQL client and your Neon credentials, add the following code to the app.js file:

const postgres = require('postgres');

const URL = `postgres://${PGUSER}:${PGPASSWORD}@${PGHOST}/${PGDATABASE}?options=project%3D${ENDPOINT_ID}`;

const sql = postgres(URL, { ssl: 'require' });

async function getPostgresVersion() {
  const result = await sql`select version()`;


Run app.js

Run node app.js to view the result.

node app.js

Result(1) [
    version: 'PostgreSQL 15.0 on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit'

Need help?

Send a request to, or join the Neon community forum.

Edit this page
Was this page helpful?