NestJS is a framework for building Node.js server-side applications. This guide explains how to connect a NestJS application to Neon.
To create a Neon project and access it from a NestJS application:
Create a Neon project
If you do not have one already, create a Neon project.
- Navigate to the Projects page in the Neon Console.
- Click New Project.
- Specify your project settings and click Create Project.
Create a NestJS project and add dependencies
-
Create a NestJS project if you do not have one. For instructions, see First steps in the NestJS documentation.
-
Add project dependencies using one of the following commands. Each installs the driver and
dotenv, which the database module uses to load your connection string.npm install @neondatabase/serverless dotenv
-
Store your Neon credentials
Add a
.envfile to your project directory and add your Neon connection string to it. You can find your connection details by clicking Connect in the Console nav. For more information, see Connect from any application.DATABASE_URL="postgresql://[user]:[password]@[neon_hostname]/[dbname]?sslmode=require&channel_binding=require"Create a sample table
The example endpoint reads from a table named
playing_with_neon. Create it and add a few rows by running the following SQL in the Neon SQL Editor or withpsql:CREATE TABLE playing_with_neon(id SERIAL PRIMARY KEY, name TEXT NOT NULL, value REAL); INSERT INTO playing_with_neon(name, value) SELECT LEFT(md5(i::TEXT), 10), random() FROM generate_series(1, 10) s(i);Configure the Postgres client
New NestJS projects use ES modules, so the relative imports below include the
.jsextension. The.jsextension also works in projects that compile to CommonJS.1. Create a database module
To manage the connection to your Neon database, create a
DatabaseModuleinsrc/database/database.module.ts. This module creates the Postgres client and provides it to the rest of your application.import { config } from 'dotenv'; import { Module } from '@nestjs/common'; import { neon } from '@neondatabase/serverless'; // Load Environment Variables config({ path: ['.env', '.env.production', '.env.local'], }); const sql = neon(process.env.DATABASE_URL!); const dbProvider = { provide: 'POSTGRES_POOL', useValue: sql, }; @Module({ providers: [dbProvider], exports: [dbProvider], }) export class DatabaseModule {}2. Create a service for database interaction
Next, replace
src/app.service.tswith a service that queries your database through the client provided byDatabaseModule.import { Injectable, Inject } from '@nestjs/common'; import type { NeonQueryFunction } from '@neondatabase/serverless'; @Injectable() export class AppService { constructor(@Inject('POSTGRES_POOL') private readonly sql: NeonQueryFunction<false, false>) {} async getRows() { return await this.sql`SELECT * FROM playing_with_neon`; } }3. Integrate the database module and service
Import
DatabaseModuleandAppServiceinsrc/app.module.ts. This makes the database client and service available throughout your application.import { Module } from '@nestjs/common'; import { AppController } from './app.controller.js'; import { AppService } from './app.service.js'; import { DatabaseModule } from './database/database.module.js'; @Module({ imports: [DatabaseModule], controllers: [AppController], providers: [AppService], }) export class AppModule {}4. Define a controller endpoint
Finally, replace
src/app.controller.tswith aGETendpoint that returns the rows from your table throughAppService.import { Controller, Get } from '@nestjs/common'; import { AppService } from './app.service.js'; @Controller('/') export class AppController { constructor(private readonly appService: AppService) {} @Get() async getRows() { return this.appService.getRows(); } }If your project has the default
src/app.controller.spec.ts, update or remove it: it testsgetHello(), which the new controller replaces.Run the app
When you run
npm run startyou can expect to see output similar to the following at localhost:3000:[{"id":1,"name":"c4ca4238a0","value":0.39330545},{"id":2,"name":"c81e728d9d","value":0.14468245}]
Next steps
- Set up Managed Better Auth: Add managed authentication that branches with your database
- Add Object Storage: S3-compatible file storage that branches with your database
- Deploy a Function: Run backend compute next to your database, no separate hosting needed
- Call an LLM with AI Gateway: Access foundation models from Anthropic, OpenAI, Google, and more with one credential
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. For paid plan support options, see Support.








