Once you have a connection string from the Connect widget on your Neon Project Dashboard, save it as an environment variable (commonly DATABASE_URL) and pass it to a Postgres driver in your code. Neon speaks the standard Postgres wire protocol, so anything that talks to Postgres works: pg, psycopg2, psql, Prisma, Drizzle, SQLAlchemy, and so on. For serverless and edge runtimes, the Neon serverless driver adds HTTP and WebSocket access.

1. Save the connection string in.env

.env
DATABASE_URL="postgresql://alex:AbC123dEf@ep-cool-darkness-a1b2c3d4-pooler.us-east-2.aws.neon.tech/dbname?sslmode=require&channel_binding=require"

Add .env to .gitignore. Keep the sslmode=require and channel_binding=require parameters in place. Neon requires TLS.

2. Connect from your code

// Best for serverless and edge runtimes
import { neon } from '@neondatabase/serverless';

const sql = neon(process.env.DATABASE_URL);
const rows = await sql`SELECT * FROM users WHERE id = ${1}`;
console.log(rows);

For Prisma, Drizzle, SQLAlchemy, and other ORMs, see the framework guides.

3. Pick the right connection type

Neon offers two flavors of the same connection string:

  • Pooled (hostname has -pooler): routes through PgBouncer in transaction mode and supports up to 10,000 concurrent client connections per compute. Use it for serverless functions, web apps, and high-concurrency clients.
  • Direct: opens a session straight to Postgres. Use it for migrations, pg_dump, LISTEN/NOTIFY, logical replication, and any session-level features. The number of direct connections you can hold open scales with compute size.

The Connect widget gives you either by toggling Connection pooling. Most apps set DATABASE_URL to the pooled URL and DIRECT_URL to the direct URL for migrations. See Connection pooling for when to use which.

Always read the string from the environment

Hardcoding the URL in source code is a common source of credential leaks. Read it from process.env.DATABASE_URL, os.environ, or your secret manager. If you're deploying to Vercel, Render, Fly, or similar, set DATABASE_URL in the platform's env settings. The Neon-Vercel integration sets it automatically.

Choose your connection method

Compare drivers and connection types based on where you're deploying.