Get PostgreSQL connection strings for your database projects. Supports direct connections, connection pooling, and branch-specific connections.
Get connection string
TypeScript
Python
C#
Java
PHP
cURL
Go
CLI
// Default: main branch, direct connection
const conn = await client.database.v1.projects. getConnection ( 'proj_abc123' );
console. log (conn);
// {
// connectionUri: "postgresql://user:pass@ep-xxx.us-east-1.aws.neon.tech/neondb?sslmode=require",
// branch: "main",
// pooled: false
// }
Parameters
Parameter Type Required Description branchstring No Branch name (defaults to main) pooledboolean No Use pooled connection via PgBouncer
Response
Field Type Description connectionUristring PostgreSQL connection string (includes credentials) branchstring Branch name for this connection pooledboolean Whether this is a pooled connection
Direct vs pooled connections
Direct connections
Best for:
Long-running processes
Migration scripts
Administrative tasks
Applications with few connections
TypeScript
Python
C#
Java
PHP
Go
CLI
const conn = await client.database.v1.projects. getConnection (projectId);
// postgresql://user:pass@ep-xxx.neon.tech/neondb
Pooled connections (PgBouncer)
Best for:
Serverless functions
High-concurrency applications
Short-lived connections
TypeScript
Python
C#
Java
PHP
Go
CLI
const conn = await client.database.v1.projects. getConnection (projectId, {
pooled: true
});
// postgresql://user:pass@ep-xxx-pooler.neon.tech/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:
TypeScript
Python
C#
Java
PHP
Go
CLI
// 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
postgresql://[user]:[password]@[host]/[database]?sslmode=require
Component Description userDatabase username passwordDatabase password hostNeon endpoint hostname databaseDatabase name (default: neondb) sslmodeSSL mode (always require)
Example:
postgresql://neondb_owner:abc123xyz@ep-quiet-meadow-123456.us-east-1.aws.neon.tech/neondb?sslmode=require