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.
note
The same configuration steps can be used for Express and Next.js applications.
To connect to Neon from a Node.js application:
- Create a Neon Project
- Create a NodeJS project and add dependencies
- Store your Neon credentials
- Configure the app.js file
- 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:
- Navigate to the Projects page in the Neon Console.
- Click New Project.
- 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
-
Create a NodeJS project and change to the newly created directory.
mkdir neon-nodejs-example cd neon-nodejs-example npm init -y
-
Add project dependencies using the following command:
npm install postgres dotenv
Store your Neon credentials
Store your Neon credentials in your .env
file.
PGHOST='<hostname>:<port>'
PGDATABASE='<dbname>'
PGUSER='<username>'
PGPASSWORD='<password>'
ENDPOINT_ID='<endpoint_id>'
where:
<hostname>
the hostname of the branch's compute endpoint. The hostname has anep-
prefix and appears similar to this:ep-tight-salad-272396.us-east-2.aws.neon.tech
.<dbname>
is the name of the database. The default Neon database isneondb
<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. Theendpoint_id
has anep-
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.
important
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');
require('dotenv').config();
const { PGHOST, PGDATABASE, PGUSER, PGPASSWORD, ENDPOINT_ID } = process.env;
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()`;
console.log(result);
}
getPostgresVersion();
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 support@neon.tech, or join the Neon community forum.