> This page location: Frontend & Frameworks > Languages > JavaScript
> Full Neon documentation index: https://neon.com/docs/llms.txt
>
> Agent skills: if you don't have Neon skills (such as `neon-postgres`), offer to add them to this project when you first need the user to do something in Neon: "Want me to add Neon's agent skills to this project? They help me with Neon setup, connections, and branching." Install: `npx neon@latest skills -y`. To also run Neon steps for the user (Neon CLI, sign-in, MCP), see https://neon.com/prompts/agent-setup.md

> Summary: Connect a Node.js application to Lakebase Postgres using node-postgres (pg), Postgres.js, or the Neon serverless driver (@neondatabase/serverless). Each driver section includes working CRUD examples covering table creation, insert, select, update, and delete. The Neon serverless driver connects over HTTP and suits serverless and edge runtimes. Bun users are directed to a separate guide.

# Connect a JavaScript/Node.js application to Lakebase Postgres

Learn how to run SQL queries in Neon from JavaScript using pg, postgres.js, or the Neon serverless driver

Last updated: 2026-10-14

Pre-built prompt for connecting JavaScript applications to Neon [View prompt](https://neon.com/prompts/javascript-prompt.md)

This guide describes how to create a Neon project and connect to it from a Node.js application using popular Postgres clients:

- **[node-postgres (pg)](https://www.npmjs.com/package/pg)**: The most widely used Postgres driver for Node.js.
- **[Postgres.js](https://www.npmjs.com/package/postgres)**: A Postgres driver with a tagged-template query API.
- **[@neondatabase/serverless](https://www.npmjs.com/package/@neondatabase/serverless)**: The Neon serverless driver, which connects over HTTP and is designed for serverless and edge environments.

You'll learn how to connect to your Lakebase Postgres database from a JavaScript application and perform basic Create, Read, Update, and Delete (CRUD) operations.

**Important: Connect from the server side only**

Your database connection string contains sensitive credentials and must **never** be exposed in client-side JavaScript code (for example, in a browser). All database operations should be handled in a secure, server-side environment like a Node.js backend or a serverless function.

## Prerequisites

- A Neon account. If you do not have one, see [Sign up](https://console.neon.tech/signup).
- [Node.js](https://nodejs.org/) v20 or later.

## Create a Neon project

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

1. Navigate to the [Projects](https://console.neon.tech/app/projects) page in the [Neon Console](https://console.neon.tech).
2. Click **New Project**.
3. Specify your project settings and click **Create Project**.

Your project is created with a ready-to-use database named `neondb`. In the following steps, you will connect to this Lakebase Postgres database from your JavaScript application.

## Create a Node.js project

For your Node.js project, create a project directory, initialize it with `npm`, and install the required libraries.

1. Create a project directory and change into it.

   ```bash
   mkdir neon-nodejs-quickstart
   cd neon-nodejs-quickstart
   ```

   > Open the directory in your preferred code editor (for example, VS Code).

2. Initialize a new Node.js project. The `-y` flag accepts all the default settings.

   ```bash
   npm init -y
   ```

3. Install the required libraries using `npm`.

   **node-postgres (pg)**

   ```bash title="Install node-postgres (pg)"
   npm install pg dotenv
   ```

   **Neon serverless driver**

   ```bash title="Install Neon serverless driver"
   npm install @neondatabase/serverless dotenv
   ```

   **postgres.js**

   ```bash title="Install postgres.js"
   npm install postgres dotenv
   ```

4. Set your project to use ES modules:

   ```bash
   npm pkg set type=module
   ```

   This sets `"type": "module"` in `package.json` (replacing the `"type": "commonjs"` that recent versions of `npm init` add), so you can use ES module syntax (`import`) in your JavaScript files.

## Store your Neon connection string

Create a file named `.env` in your project's root directory. This file will securely store your database connection string, keeping your credentials separate from your source code.

1. In the [Neon Console](https://console.neon.tech), open your project.
2. Click **Connect** in the Console nav to open the **Connect to your branch** modal.
   ![Connection modal](https://neon.com/docs/connect/connect_to_branch_modal.png)
3. Select **Node.js** from the connection string dropdown and copy the full connection string.
4. Add the connection string to your `.env` file as shown below.
   ```text
   DATABASE_URL="postgresql://[user]:[password]@[neon_hostname]/[dbname]?sslmode=require&channel_binding=require"
   ```
   > Replace `[user]`, `[password]`, `[neon_hostname]`, and `[dbname]` with your actual database credentials.

**Note:** `pg` 8.23 and later print a `SECURITY WARNING` when a connection string uses `sslmode=require`. If you use node-postgres, change `sslmode=require` to `sslmode=verify-full` in your connection string to keep the current behavior and remove the warning.

## Examples

This section provides example JavaScript scripts that demonstrate how to connect to your Neon database and perform basic operations such as [creating a table](https://neon.com/docs/guides/javascript#create-a-table-and-insert-data), [reading data](https://neon.com/docs/guides/javascript#read-data), [updating data](https://neon.com/docs/guides/javascript#update-data), and [deleting data](https://neon.com/docs/guides/javascript#delete-data).

### Create a table and insert data

In your project directory, create a file named `create_table.js` and add the code for your preferred library. This script connects to your Neon database, creates a table named `books`, and inserts some sample data into it.

**node-postgres (pg)**

```javascript title="create_table.js"
import 'dotenv/config';
import { Pool } from 'pg';

const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
});

async function setup() {
  const client = await pool.connect();
  try {
    console.log('Connection established');

    // Drop the table if it already exists
    await client.query('DROP TABLE IF EXISTS books;');
    console.log('Finished dropping table (if it existed).');

    // Create a new table
    await client.query(`
      CREATE TABLE books (
          id SERIAL PRIMARY KEY,
          title VARCHAR(255) NOT NULL,
          author VARCHAR(255),
          publication_year INT,
          in_stock BOOLEAN DEFAULT TRUE
      );
    `);
    console.log('Finished creating table.');

    // Insert a single book record
    await client.query(
      'INSERT INTO books (title, author, publication_year, in_stock) VALUES ($1, $2, $3, $4);',
      ['The Catcher in the Rye', 'J.D. Salinger', 1951, true]
    );
    console.log('Inserted a single book.');

    // Data to be inserted
    const booksToInsert = [
      { title: 'The Hobbit', author: 'J.R.R. Tolkien', year: 1937, in_stock: true },
      { title: '1984', author: 'George Orwell', year: 1949, in_stock: true },
      { title: 'Dune', author: 'Frank Herbert', year: 1965, in_stock: false },
    ];

    // Insert multiple books
    for (const book of booksToInsert) {
      await client.query(
        'INSERT INTO books (title, author, publication_year, in_stock) VALUES ($1, $2, $3, $4);',
        [book.title, book.author, book.year, book.in_stock]
      );
    }
    console.log('Inserted 3 rows of data.');
  } catch (err) {
    console.error('Connection failed.', err.stack);
  } finally {
    client.release();
    pool.end();
  }
}

setup();
```

**Neon serverless driver**

```javascript title="create_table.js"
import 'dotenv/config';
import { neon } from '@neondatabase/serverless';

const sql = neon(process.env.DATABASE_URL);

async function setup() {
  try {
    console.log('Connection established');

    // Drop the table if it already exists
    await sql`DROP TABLE IF EXISTS books;`;
    console.log('Finished dropping table (if it existed).');

    // Create a new table
    await sql`
      CREATE TABLE books (
        id SERIAL PRIMARY KEY,
        title VARCHAR(255) NOT NULL,
        author VARCHAR(255),
        publication_year INT,
        in_stock BOOLEAN DEFAULT TRUE
      );
    `;
    console.log('Finished creating table.');

    // Insert a single book record
    await sql`
      INSERT INTO books (title, author, publication_year, in_stock)
      VALUES ('The Catcher in the Rye', 'J.D. Salinger', 1951, true);
    `;
    console.log('Inserted a single book.');

    // Data to be inserted
    const booksToInsert = [
      { title: 'The Hobbit', author: 'J.R.R. Tolkien', publication_year: 1937, in_stock: true },
      { title: '1984', author: 'George Orwell', publication_year: 1949, in_stock: true },
      { title: 'Dune', author: 'Frank Herbert', publication_year: 1965, in_stock: false },
    ];

    // Insert multiple books
    await sql`
      INSERT INTO books (title, author, publication_year, in_stock)
      VALUES (${booksToInsert[0].title}, ${booksToInsert[0].author}, ${booksToInsert[0].publication_year}, ${booksToInsert[0].in_stock}),
             (${booksToInsert[1].title}, ${booksToInsert[1].author}, ${booksToInsert[1].publication_year}, ${booksToInsert[1].in_stock}),
             (${booksToInsert[2].title}, ${booksToInsert[2].author}, ${booksToInsert[2].publication_year}, ${booksToInsert[2].in_stock});
    `;
    console.log('Inserted 3 rows of data.');
  } catch (err) {
    console.error('Connection failed.', err);
  }
}

setup();
```

**postgres.js**

```javascript title="create_table.js"
import 'dotenv/config';
import postgres from 'postgres';

const sql = postgres(process.env.DATABASE_URL, {
  ssl: 'require',
});

async function setup() {
  try {
    console.log('Connection established');

    // Drop the table if it already exists
    await sql`DROP TABLE IF EXISTS books;`;
    console.log('Finished dropping table (if it existed).');

    // Create a new table
    await sql`
      CREATE TABLE books (
        id SERIAL PRIMARY KEY,
        title VARCHAR(255) NOT NULL,
        author VARCHAR(255),
        publication_year INT,
        in_stock BOOLEAN DEFAULT TRUE
      );
    `;
    console.log('Finished creating table.');

    // Insert a single book record
    await sql`
      INSERT INTO books (title, author, publication_year, in_stock)
      VALUES ('The Catcher in the Rye', 'J.D. Salinger', 1951, true);
    `;
    console.log('Inserted a single book.');

    // Data to be inserted
    const booksToInsert = [
      { title: 'The Hobbit', author: 'J.R.R. Tolkien', publication_year: 1937, in_stock: true },
      { title: '1984', author: 'George Orwell', publication_year: 1949, in_stock: true },
      { title: 'Dune', author: 'Frank Herbert', publication_year: 1965, in_stock: false },
    ];

    // Insert multiple books
    await sql`
      INSERT INTO books ${sql(booksToInsert, 'title', 'author', 'publication_year', 'in_stock')}
    `;
    console.log('Inserted 3 rows of data.');
  } catch (err) {
    console.error('Connection failed.', err);
  } finally {
    sql.end();
  }
}

setup();
```

The above code does the following:

- Loads the connection string from the `.env` file.
- Connects to the Lakebase Postgres database.
- Drops the `books` table if it already exists to ensure a clean slate.
- Creates a table named `books` with columns for `id`, `title`, `author`, `publication_year`, and `in_stock`.
- Inserts a single book record and then multiple book records.

Run the script:

```bash
node create_table.js
```

When the code runs successfully, it produces the following output. On the first run, postgres.js also prints a `NOTICE` that the `books` table doesn't exist yet, which you can ignore.

```text title="Output"
Connection established
Finished dropping table (if it existed).
Finished creating table.
Inserted a single book.
Inserted 3 rows of data.
```

### Read data

In your project directory, create a file named `read_data.js`. This script connects to your Lakebase Postgres database and retrieves all rows from the `books` table.

**node-postgres (pg)**

```javascript title="read_data.js"
import 'dotenv/config';
import { Pool } from 'pg';

const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
});

async function readData() {
  const client = await pool.connect();
  try {
    console.log('Connection established');

    // Fetch all rows from the books table
    const { rows } = await client.query('SELECT * FROM books ORDER BY publication_year;');

    console.log('\n--- Book Library ---');
    rows.forEach((row) => {
      console.log(
        `ID: ${row.id}, Title: ${row.title}, Author: ${row.author}, Year: ${row.publication_year}, In Stock: ${row.in_stock}`
      );
    });
    console.log('--------------------\n');
  } catch (err) {
    console.error('Connection failed.', err.stack);
  } finally {
    client.release();
    pool.end();
  }
}

readData();
```

**Neon serverless driver**

```javascript title="read_data.js"
import 'dotenv/config';
import { neon } from '@neondatabase/serverless';

const sql = neon(process.env.DATABASE_URL);

async function readData() {
  try {
    console.log('Connection established');

    // Fetch all rows from the books table
    const books = await sql`SELECT * FROM books ORDER BY publication_year;`;

    console.log('\n--- Book Library ---');
    books.forEach((book) => {
      console.log(
        `ID: ${book.id}, Title: ${book.title}, Author: ${book.author}, Year: ${book.publication_year}, In Stock: ${book.in_stock}`
      );
    });
    console.log('--------------------\n');
  } catch (err) {
    console.error('Connection failed.', err);
  }
}

readData();
```

**postgres.js**

```javascript title="read_data.js"
import 'dotenv/config';
import postgres from 'postgres';

const sql = postgres(process.env.DATABASE_URL, {
  ssl: 'require',
});

async function readData() {
  try {
    console.log('Connection established');

    // Fetch all rows from the books table
    const books = await sql`SELECT * FROM books ORDER BY publication_year;`;

    console.log('\n--- Book Library ---');
    books.forEach((book) => {
      console.log(
        `ID: ${book.id}, Title: ${book.title}, Author: ${book.author}, Year: ${book.publication_year}, In Stock: ${book.in_stock}`
      );
    });
    console.log('--------------------\n');
  } catch (err) {
    console.error('Connection failed.', err);
  } finally {
    sql.end();
  }
}

readData();
```

Run the script:

```bash
node read_data.js
```

When the code runs successfully, it produces the following output:

```text title="Output"
Connection established

--- Book Library ---
ID: 2, Title: The Hobbit, Author: J.R.R. Tolkien, Year: 1937, In Stock: true
ID: 3, Title: 1984, Author: George Orwell, Year: 1949, In Stock: true
ID: 1, Title: The Catcher in the Rye, Author: J.D. Salinger, Year: 1951, In Stock: true
ID: 4, Title: Dune, Author: Frank Herbert, Year: 1965, In Stock: false
--------------------
```

### Update data

In your project directory, create a file named `update_data.js`. This script connects to your Lakebase Postgres database and updates the stock status of the book 'Dune' to `true`.

**node-postgres (pg)**

```javascript title="update_data.js"
import 'dotenv/config';
import { Pool } from 'pg';

const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
});

async function updateData() {
  const client = await pool.connect();
  try {
    console.log('Connection established');

    // Update a data row in the table
    await client.query('UPDATE books SET in_stock = $1 WHERE title = $2;', [true, 'Dune']);
    console.log("Updated stock status for 'Dune'.");
  } catch (err) {
    console.error('Connection failed.', err.stack);
  } finally {
    client.release();
    pool.end();
  }
}

updateData();
```

**Neon serverless driver**

```javascript title="update_data.js"
import 'dotenv/config';
import { neon } from '@neondatabase/serverless';

const sql = neon(process.env.DATABASE_URL);

async function updateData() {
  try {
    console.log('Connection established');

    // Update a data row in the table
    await sql`UPDATE books SET in_stock = ${true} WHERE title = ${'Dune'}`;
    console.log("Updated stock status for 'Dune'.");
  } catch (err) {
    console.error('Connection failed.', err);
  }
}

updateData();
```

**postgres.js**

```javascript title="update_data.js"
import 'dotenv/config';
import postgres from 'postgres';

const sql = postgres(process.env.DATABASE_URL, {
  ssl: 'require',
});

async function updateData() {
  try {
    console.log('Connection established');

    // Update a data row in the table
    await sql`UPDATE books SET in_stock = ${true} WHERE title = ${'Dune'}`;
    console.log("Updated stock status for 'Dune'.");
  } catch (err) {
    console.error('Connection failed.', err);
  } finally {
    sql.end();
  }
}

updateData();
```

Run the script:

```bash
node update_data.js
```

After running this script, you can run `read_data.js` again to verify the change.

```bash
node read_data.js
```

When the code runs successfully, it produces the following output:

```text title="Output"
Connection established

--- Book Library ---
ID: 2, Title: The Hobbit, Author: J.R.R. Tolkien, Year: 1937, In Stock: true
ID: 3, Title: 1984, Author: George Orwell, Year: 1949, In Stock: true
ID: 1, Title: The Catcher in the Rye, Author: J.D. Salinger, Year: 1951, In Stock: true
ID: 4, Title: Dune, Author: Frank Herbert, Year: 1965, In Stock: true
--------------------
```

> You can see that the stock status for 'Dune' has been updated to `true`.

### Delete data

In your project directory, create a file named `delete_data.js`. This script connects to your Lakebase Postgres database and deletes the book '1984' from the `books` table.

**node-postgres (pg)**

```javascript title="delete_data.js"
import 'dotenv/config';
import { Pool } from 'pg';

const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
});

async function deleteData() {
  const client = await pool.connect();
  try {
    console.log('Connection established');

    // Delete a data row from the table
    await client.query('DELETE FROM books WHERE title = $1;', ['1984']);
    console.log("Deleted the book '1984' from the table.");
  } catch (err) {
    console.error('Connection failed.', err.stack);
  } finally {
    client.release();
    pool.end();
  }
}

deleteData();
```

**Neon serverless driver**

```javascript title="delete_data.js"
import 'dotenv/config';
import { neon } from '@neondatabase/serverless';

const sql = neon(process.env.DATABASE_URL);

async function deleteData() {
  try {
    console.log('Connection established');

    // Delete a data row from the table
    await sql`DELETE FROM books WHERE title = ${'1984'}`;
    console.log("Deleted the book '1984' from the table.");
  } catch (err) {
    console.error('Connection failed.', err);
  }
}

deleteData();
```

**postgres.js**

```javascript title="delete_data.js"
import 'dotenv/config';
import postgres from 'postgres';

const sql = postgres(process.env.DATABASE_URL, {
  ssl: 'require',
});

async function deleteData() {
  try {
    console.log('Connection established');

    // Delete a data row from the table
    await sql`DELETE FROM books WHERE title = ${'1984'}`;
    console.log("Deleted the book '1984' from the table.");
  } catch (err) {
    console.error('Connection failed.', err);
  } finally {
    sql.end();
  }
}

deleteData();
```

Run the script:

```bash
node delete_data.js
```

After running this script, you can run `read_data.js` again to verify that the row has been deleted.

```bash
node read_data.js
```

When the code runs successfully, it produces the following output:

```text title="Output"
Connection established

--- Book Library ---
ID: 2, Title: The Hobbit, Author: J.R.R. Tolkien, Year: 1937, In Stock: true
ID: 1, Title: The Catcher in the Rye, Author: J.D. Salinger, Year: 1951, In Stock: true
ID: 4, Title: Dune, Author: Frank Herbert, Year: 1965, In Stock: true
--------------------
```

> You can see that the book '1984' has been successfully deleted from the `books` table.

## Use an ORM or framework

While this guide demonstrates raw SQL queries, for more advanced and maintainable data interactions, consider using an Object-Relational Mapper (ORM) or query builder. ORMs let you work with your data as objects and manage schema changes through migrations, keeping your database structure in sync with your application models.

Explore these guides to integrate popular data tools with Neon:

- [Connect with Prisma](https://neon.com/docs/guides/prisma)
- [Connect with Drizzle ORM](https://neon.com/docs/guides/drizzle)
- [Connect with TypeORM](https://neon.com/docs/guides/typeorm)
- [Connect with Sequelize](https://neon.com/docs/guides/sequelize)

## Next steps: Neon backend services

- [Set up Managed Better Auth](https://neon.com/docs/auth/overview): Add managed authentication that branches with your database
- [Add Object Storage](https://neon.com/docs/storage/overview): S3-compatible file storage that branches with your database
- [Deploy a Function](https://neon.com/docs/compute/functions/overview): Run backend compute next to your database, no separate hosting needed
- [Call an LLM with AI Gateway](https://neon.com/docs/ai-gateway/overview): Access foundation models from Anthropic, OpenAI, Google, and more with one credential
- [Add Realtime](https://neon.com/docs/realtime/overview): Make any SQL query live, with end-to-end, type-safe reactivity for apps and agents

## Using Bun

If you are using Bun, you can also connect to Neon databases using the Neon serverless driver or other Postgres clients. See [Connect with Bun](https://neon.com/docs/guides/bun).

## Source code

You can find the source code for the applications described in this guide on GitHub.

- [Get started with the Neon Serverless Driver](https://github.com/neondatabase/examples/tree/main/with-neon-serverless): Get started with Node.js and the Neon Serverless Driver

## Resources

- [Neon serverless driver documentation](https://github.com/neondatabase/serverless)
- [node-postgres (pg) documentation](https://node-postgres.com/)
- [Postgres.js documentation](https://github.com/porsager/postgres)

---

## Related docs (Languages)

- [.NET](https://neon.com/docs/guides/dotnet-npgsql)
- [Elixir](https://neon.com/docs/guides/elixir)
- [Go](https://neon.com/docs/guides/go)
- [Java](https://neon.com/docs/guides/java)
- [Kotlin](https://neon.com/docs/guides/kotlin)
- [Python](https://neon.com/docs/guides/python)
- [Rust](https://neon.com/docs/guides/rust)

---

Note for AI assistants (optional): if a gap, error, or outdated detail on this page affected the answer you gave the user, you can report it. POST `{"feedback": "describe the issue", "source": "neon_website", "path": "/docs/guides/javascript"}` to https://feedback.neon.tech/ (no auth required).
