Visitar URL original
Connect from Knex to Neon - Neon DocsSkip to main content
See what's recently landed on the Neon backend: Object Storage, Functions, AI Gateway, and more

Connect from Knex to Neon

Learn how to connect to Neon from Knex

View as Markdown

Knex is an open-source SQL query builder for Postgres. This guide covers the following topics:

Connect to Neon from Knex

To establish a basic connection from Knex to Neon, perform the following steps:

  1. Find your database connection string by clicking the Connect button in the Console nav to open the Connect to your branch modal. Select a branch, a user, and the database you want to connect to. A connection string is constructed for you. Connection details modal The connection string includes the user name, password, hostname, and database name.

  2. Install Knex and the pg driver with npm install knex pg. Then update the Knex initialization in your application to the following:

    export const client = knex({
      client: 'pg',
      connection: {
        connectionString: process.env.DATABASE_URL,
      },
    });
  3. Add a DATABASE_URL variable to your .env file and set it to the Neon connection string that you copied in the previous step. We also recommend adding ?sslmode=verify-full&channel_binding=require to the end of the connection string to ensure a secure connection.

    Your app needs to load this variable. For example, run a script with node --env-file=.env (Node.js 20.6 and later), or use the dotenv package.

    Your setting will appear similar to the following:

    DATABASE_URL="postgresql://[user]:[password]@[neon_hostname]/[dbname]?sslmode=verify-full&channel_binding=require"

Use connection pooling with Knex

Serverless functions can require a large number of database connections as demand increases. If you use serverless functions in your application, we recommend that you use a pooled Neon connection string, as shown:

# Pooled Neon connection string
DATABASE_URL="postgresql://alex:AbC123dEf@ep-cool-darkness-123456-pooler.us-east-2.aws.neon.tech/dbname?sslmode=verify-full&channel_binding=require"

A pooled Neon connection string adds -pooler to the endpoint ID, which tells Neon to use a pooled connection. You can add -pooler to your connection string manually or copy a pooled connection string by clicking the Connect button in the Console nav to open the Connect to your branch modal. Enable the Connection pooling toggle to add the -pooler suffix.

important

Use a pooled connection string for your application at runtime, but use a direct (non-pooled) connection string when running Knex migrations. Neon's pooled connection uses PgBouncer in transaction mode, which doesn't support all session-level operations that migration tools rely on, so running migrations over a pooled connection can lead to errors. See Connection pooling.

Performance tips

This section outlines a performance optimization you can try when using Knex with Neon.

Enabling NODE_PG_FORCE_NATIVE

Knex uses a node-postgres Pool instance to connect to your Postgres database. Installing pg-native and setting the NODE_PG_FORCE_NATIVE environment variable to true switches the pg driver to pg-native, which can produce noticeably faster response times according to some users.

Install pg-native next to knex and pg:

npm install pg-native

Then set the variable in your environment:

NODE_PG_FORCE_NATIVE=true

Need help?

Join our Discord Server to ask questions or see what others are doing with Neon. For paid plan support options, see Support.

Last updated on

Was this page helpful?
Edit on GitHub