with one click
neon-postgres
// Expert patterns for Neon serverless Postgres, branching, connection pooling, and Prisma/Drizzle integration
// Expert patterns for Neon serverless Postgres, branching, connection pooling, and Prisma/Drizzle integration
Behavioral guidelines to reduce common LLM coding mistakes. Use when writing, reviewing, or refactoring code to avoid overcomplication, make surgical changes, surface assumptions, and define verifiable success criteria.
Security audit, hardening, threat modeling (STRIDE/PASTA), Red/Blue Team, OWASP checks, code review, incident response, and infrastructure security for any project.
Important: Before you begin, fill in the generatedBy property in the meta section of .actor/actor.json. Replace it with the tool and model you're currently using, such as "Claude Code with Claude Sonnet 4.5". This helps Apify monitor and improve AGENTS.md for specific AI tools and models.
Fast, modern JavaScript/TypeScript development with the Bun runtime, inspired by [oven-sh/bun](https://github.com/oven-sh/bun).
Conduct comprehensive security assessments of cloud infrastructure across Microsoft Azure, Amazon Web Services (AWS), and Google Cloud Platform (GCP).
Guide developers through setting up development environments with proper tools, dependencies, and configurations
| name | neon-postgres |
| description | Expert patterns for Neon serverless Postgres, branching, connection pooling, and Prisma/Drizzle integration |
| risk | safe |
| source | vibeship-spawner-skills (Apache 2.0) |
| date_added | "2026-02-27T00:00:00.000Z" |
Expert patterns for Neon serverless Postgres, branching, connection pooling, and Prisma/Drizzle integration
Configure Prisma for Neon with connection pooling.
Use two connection strings:
The pooled connection uses PgBouncer for up to 10K connections. Direct connection required for migrations (DDL operations).
DATABASE_URL="postgres://user:password@ep-xxx-pooler.us-east-2.aws.neon.tech/neondb?sslmode=require"
DIRECT_URL="postgres://user:password@ep-xxx.us-east-2.aws.neon.tech/neondb?sslmode=require"
// prisma/schema.prisma generator client { provider = "prisma-client-js" }
datasource db { provider = "postgresql" url = env("DATABASE_URL") directUrl = env("DIRECT_URL") }
model User { id String @id @default(cuid()) email String @unique name String? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt }
// lib/prisma.ts import { PrismaClient } from '@prisma/client';
const globalForPrisma = globalThis as unknown as { prisma: PrismaClient | undefined; };
export const prisma = globalForPrisma.prisma ?? new PrismaClient({ log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'], });
if (process.env.NODE_ENV !== 'production') { globalForPrisma.prisma = prisma; }
// Run migrations // Uses DIRECT_URL automatically npx prisma migrate dev npx prisma migrate deploy
Use Drizzle ORM with Neon's serverless HTTP driver for edge/serverless environments.
Two driver options:
npm install drizzle-orm @neondatabase/serverless npm install -D drizzle-kit
// lib/db/schema.ts import { pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core';
export const users = pgTable('users', { id: serial('id').primaryKey(), email: text('email').notNull().unique(), name: text('name'), createdAt: timestamp('created_at').defaultNow().notNull(), updatedAt: timestamp('updated_at').defaultNow().notNull(), });
// lib/db/index.ts (for serverless - HTTP driver) import { neon } from '@neondatabase/serverless'; import { drizzle } from 'drizzle-orm/neon-http'; import * as schema from './schema';
const sql = neon(process.env.DATABASE_URL!); export const db = drizzle(sql, { schema });
// Usage in API route import { db } from '@/lib/db'; import { users } from '@/lib/db/schema';
export async function GET() { const allUsers = await db.select().from(users); return Response.json(allUsers); }
// lib/db/index.ts (for WebSocket - transactions) import { Pool } from '@neondatabase/serverless'; import { drizzle } from 'drizzle-orm/neon-serverless'; import * as schema from './schema';
const pool = new Pool({ connectionString: process.env.DATABASE_URL }); export const db = drizzle(pool, { schema });
// With transactions await db.transaction(async (tx) => { await tx.insert(users).values({ email: 'test@example.com' }); await tx.update(users).set({ name: 'Updated' }); });
// drizzle.config.ts import { defineConfig } from 'drizzle-kit';
export default defineConfig({ schema: './lib/db/schema.ts', out: './drizzle', dialect: 'postgresql', dbCredentials: { url: process.env.DATABASE_URL!, }, });
// Run migrations npx drizzle-kit generate npx drizzle-kit migrate
Neon provides built-in connection pooling via PgBouncer.
Key limits:
Use pooled endpoint for application, direct for migrations.
postgres://user:pass@ep-cool-name-pooler.us-east-2.aws.neon.tech/neondb
postgres://user:pass@ep-cool-name.us-east-2.aws.neon.tech/neondb
// Prisma with pooling // prisma/schema.prisma datasource db { provider = "postgresql" url = env("DATABASE_URL") // Pooled directUrl = env("DIRECT_URL") // Direct }
// Connection pool settings for high-traffic // lib/prisma.ts import { PrismaClient } from '@prisma/client';
export const prisma = new PrismaClient({ datasources: { db: { url: process.env.DATABASE_URL, }, }, // Connection pool settings // Adjust based on compute size });
// For Drizzle with connection pool import { Pool } from '@neondatabase/serverless';
const pool = new Pool({ connectionString: process.env.DATABASE_URL, max: 10, // Max connections in local pool idleTimeoutMillis: 30000, connectionTimeoutMillis: 10000, });
// Compute size connection limits // 0.25 CU: 112 connections (105 available after reserved) // 0.5 CU: 225 connections // 1 CU: 450 connections // 2 CU: 901 connections // 4 CU: 1802 connections // 8 CU: 3604 connections
Create instant copies of your database for development, testing, and preview environments.
Branches share underlying storage (copy-on-write), making them instant and cost-effective.
neon branches create --name feature/new-feature --parent main
neon branches create --name debug/yesterday
--parent main
--timestamp "2024-01-15T10:00:00Z"
neon branches list
neon connection-string feature/new-feature
neon branches delete feature/new-feature
// In CI/CD (GitHub Actions) // .github/workflows/preview.yml name: Preview Environment on: pull_request: types: [opened, synchronize]
jobs: create-branch: runs-on: ubuntu-latest steps: - uses: neondatabase/create-branch-action@v5 id: create-branch with: project_id: ${{ secrets.NEON_PROJECT_ID }} branch_name: preview/pr-${{ github.event.pull_request.number }} api_key: ${{ secrets.NEON_API_KEY }} username: ${{ secrets.NEON_ROLE_NAME }}
- name: Run migrations
env:
DATABASE_URL: ${{ steps.create-branch.outputs.db_url_with_pooler }}
run: npx prisma migrate deploy
- name: Deploy to Vercel
env:
DATABASE_URL: ${{ steps.create-branch.outputs.db_url_with_pooler }}
run: vercel deploy --prebuilt
// Cleanup on PR close on: pull_request: types: [closed]
jobs: delete-branch: runs-on: ubuntu-latest steps: - uses: neondatabase/delete-branch-action@v3 with: project_id: ${{ secrets.NEON_PROJECT_ID }} branch: preview/pr-${{ github.event.pull_request.number }} api_key: ${{ secrets.NEON_API_KEY }}
Automatically create database branches for Vercel preview deployments. Each PR gets its own isolated database.
Two integration options:
// vercel.json - Add migration to build { "buildCommand": "prisma migrate deploy && next build", "framework": "nextjs" }
// Or in package.json { "scripts": { "vercel-build": "prisma generate && prisma migrate deploy && next build" } }
// Environment variables injected by integration // DATABASE_URL - Pooled connection for preview branch // DATABASE_URL_UNPOOLED - Direct connection for migrations // PGHOST, PGUSER, PGDATABASE, PGPASSWORD - Individual vars
// Prisma schema for Vercel integration datasource db { provider = "postgresql" url = env("DATABASE_URL") directUrl = env("DATABASE_URL_UNPOOLED") // Vercel variable }
// For Drizzle in Next.js on Vercel import { neon } from '@neondatabase/serverless'; import { drizzle } from 'drizzle-orm/neon-http';
// Use pooled URL for queries const sql = neon(process.env.DATABASE_URL!); export const db = drizzle(sql);
Neon autoscales compute resources and scales to zero.
Cold start latency: 500ms - few seconds when waking from idle. Production recommendation: Disable scale-to-zero, set minimum compute.
// Handle cold starts in application // lib/db-with-retry.ts import { prisma } from './prisma';
const MAX_RETRIES = 3; const RETRY_DELAY = 1000;
export async function queryWithRetry( query: () => Promise ): Promise { let lastError: Error | undefined;
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) { try { return await query(); } catch (error) { lastError = error as Error;
// Retry on connection errors (cold start)
if (error.code === 'P1001' || error.code === 'P1002') {
console.log(`Retry attempt ${attempt}/${MAX_RETRIES}`);
await new Promise(r => setTimeout(r, RETRY_DELAY * attempt));
continue;
}
throw error;
}
}
throw lastError; }
// Usage const users = await queryWithRetry(() => prisma.user.findMany() );
// Reduce cold start latency with SSL direct negotiation
postgres://user:pass@ep-xxx-pooler.aws.neon.tech/db?sslmode=require&sslnegotiation=direct
// Keep-alive for long-running apps // lib/db-keepalive.ts import { prisma } from './prisma';
// Ping database every 4 minutes to prevent suspend const KEEPALIVE_INTERVAL = 4 * 60 * 1000;
if (process.env.NEON_KEEPALIVE === 'true') {
setInterval(async () => {
try {
await prisma.$queryRawSELECT 1;
} catch (error) {
console.error('Keepalive failed:', error);
}
}, KEEPALIVE_INTERVAL);
}
// Compute sizing recommendations // Development: 0.25 CU, scale-to-zero enabled // Staging: 0.5 CU, scale-to-zero enabled // Production: 1+ CU, scale-to-zero DISABLED // High-traffic: 2-4 CU minimum, autoscaling enabled
Severity: HIGH
Severity: HIGH
Severity: HIGH
Severity: MEDIUM
Severity: MEDIUM
Severity: LOW
Severity: MEDIUM
Severity: HIGH
Severity: MEDIUM
Severity: ERROR
Direct database URLs should never be exposed to client
Message: Direct URL exposed to client. Only pooled URLs for server-side use.
Severity: ERROR
Connection strings should use environment variables
Message: Hardcoded connection string. Use environment variables.
Severity: WARNING
Neon requires SSL connections
Message: Missing sslmode=require. Add to connection string.
Severity: ERROR
Prisma needs directUrl for migrations through PgBouncer
Message: Using pooled URL without directUrl. Migrations will fail.
Severity: ERROR
directUrl should be non-pooled connection
Message: directUrl points to pooler. Use non-pooled endpoint for migrations.
Severity: WARNING
High pool sizes exhaust connections with many function instances
Message: Pool size too high for serverless. Use max: 5-10.
Severity: WARNING
Creating new clients per request wastes connections
Message: Creating client per request. Use connection pool or neon() driver.
Severity: WARNING
Branches should have cleanup automation
Message: Creating branch without cleanup. Add delete-branch-action to PR close.
Severity: WARNING
Scale-to-zero adds latency in production
Message: Scale-to-zero on production. Disable for low-latency.
Severity: ERROR
neon() HTTP driver doesn't support transactions
Message: HTTP driver with transaction. Use Pool from @neondatabase/serverless.