Skip to main content
Get PostgreSQL connection strings for your database projects. Supports direct connections, connection pooling, and branch-specific connections.

Get connection string

// Default: main branch, direct connection
const conn = await client.database.v1.projects.getConnection('proj_abc123');

console.log(conn);
// {
//   connectionUri: "postgresql://user:[email protected]/neondb?sslmode=require",
//   branch: "main",
//   pooled: false
// }

Parameters

ParameterTypeRequiredDescription
branchstringNoBranch name (defaults to main)
pooledbooleanNoUse pooled connection via PgBouncer

Response

FieldTypeDescription
connectionUristringPostgreSQL connection string (includes credentials)
branchstringBranch name for this connection
pooledbooleanWhether this is a pooled connection

Direct vs pooled connections

Direct connections

Best for:
  • Long-running processes
  • Migration scripts
  • Administrative tasks
  • Applications with few connections
const conn = await client.database.v1.projects.getConnection(projectId);
// postgresql://user:[email protected]/neondb

Pooled connections (PgBouncer)

Best for:
  • Serverless functions
  • High-concurrency applications
  • Short-lived connections
const conn = await client.database.v1.projects.getConnection(projectId, {
  pooled: true
});
// postgresql://user:[email protected]/neondb
Pooled connections go through PgBouncer, which maintains a pool of database connections. This is essential for serverless environments where you might have many short-lived function invocations.

Branch-specific connections

Get a connection string for a specific branch:
// Get staging branch connection
const stagingConn = await client.database.v1.projects.getConnection(projectId, {
  branch: 'staging'
});

// Get feature branch connection (pooled)
const featureConn = await client.database.v1.projects.getConnection(projectId, {
  branch: 'feature-auth',
  pooled: true
});

Integration examples

Drizzle ORM

import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';

const conn = await client.database.v1.projects.getConnection(projectId);
const sql = postgres(conn.connectionUri);
const db = drizzle(sql);

// Query
const users = await db.select().from(usersTable);

Prisma

// Get connection string
const conn = await client.database.v1.projects.getConnection(projectId, {
  pooled: true  // Recommended for Prisma
});

// Set in .env or environment
process.env.DATABASE_URL = conn.connectionUri;

// prisma/schema.prisma
// datasource db {
//   provider = "postgresql"
//   url      = env("DATABASE_URL")
// }

Raw pg (node-postgres)

import { Pool } from 'pg';

const conn = await client.database.v1.projects.getConnection(projectId, {
  pooled: true
});

const pool = new Pool({
  connectionString: conn.connectionUri
});

const result = await pool.query('SELECT * FROM users WHERE id = $1', [userId]);

Python psycopg2

import psycopg2

conn = client.database.v1.projects.get_connection(project_id)

db = psycopg2.connect(conn.connection_uri)
cursor = db.cursor()
cursor.execute("SELECT * FROM users WHERE id = %s", (user_id,))

Python SQLAlchemy

from sqlalchemy import create_engine

conn = client.database.v1.projects.get_connection(project_id, pooled=True)

engine = create_engine(conn.connection_uri)
with engine.connect() as connection:
    result = connection.execute("SELECT * FROM users")

Security

Connection strings include database credentials. Never expose them in client-side code, logs, or version control.
Best practices:
  • Store connection strings in environment variables
  • Use secret management services (AWS Secrets Manager, Vault)
  • Rotate credentials periodically
  • Use pooled connections for serverless to limit open connections
// Good: Environment variable
const databaseUrl = process.env.DATABASE_URL;

// Bad: Hardcoded
const databaseUrl = "postgresql://user:password@host/db"; // Never do this

Connection string format

postgresql://[user]:[password]@[host]/[database]?sslmode=require
ComponentDescription
userDatabase username
passwordDatabase password
hostNeon endpoint hostname
databaseDatabase name (default: neondb)
sslmodeSSL mode (always require)
Example:
postgresql://neondb_owner:[email protected]/neondb?sslmode=require