Neon vercel postgres

From ovachiever/droid-tings · View interactive version

Neon vercel postgres is an AI agent skill from the GitHub repository ovachiever/droid-tings. Set up serverless Postgres with Neon or Vercel Postgres for Cloudflare Workers/Edge. Includes connection pooling, git-like branching for preview environments, and Drizzle/Prisma integration. Use when: setting up edge Postgres, configuring database branching, or troubleshooting "TCP not supported", c

How to use this skill

Skill file

Neon & Vercel Serverless Postgres

Status: Production Ready

Last Updated: 2025-10-29

Dependencies: None

Latest Versions: @neondatabase/serverless@1.0.2, @vercel/postgres@0.10.0, drizzle-orm@0.44.7, neonctl@2.16.1


Quick Start (5 Minutes)

1. Choose Your Platform

Option A: Neon Direct (multi-cloud, Cloudflare Workers, any serverless)

npm install @neondatabase/serverless

Option B: Vercel Postgres (Vercel-only, zero-config on Vercel)

npm install @vercel/postgres

Note: Both use the same Neon backend. Vercel Postgres is Neon with Vercel-specific environment setup.

Why this matters:

2. Get Your Connection String

For Neon Direct:

# Sign up at https://neon.tech
# Create a project → Get connection string
# Format: postgresql://user:password@ep-xyz.region.aws.neon.tech/dbname?sslmode=require

For Vercel Postgres:

# In your Vercel project
vercel postgres create
vercel env pull .env.local  # Automatically creates POSTGRES_URL and other vars

CRITICAL:

3. Query Your Database

Neon Direct (Cloudflare Workers, Vercel Edge, Node.js):

import { neon } from '@neondatabase/serverless';

const sql = neon(process.env.DATABASE_URL!);

// Simple query
const users = await sql`SELECT * FROM users WHERE id = ${userId}`;

// Transactions
const result = await sql.transaction([
  sql`INSERT INTO users (name) VALUES (${name})`,
  sql`SELECT * FROM users WHERE name = ${name}`
]);

Vercel Postgres (Next.js Server Actions, API Routes):

import { sql } from '@vercel/postgres';

// Simple query
const { rows } = await sql`SELECT * FROM users WHERE id = ${userId}`;

// Transactions
const client = await sql.connect();
try {
  await client.sql`BEGIN`;
  await client.sql`INSERT INTO users (name) VALUES (${name})`;
  await client.sql`COMMIT`;
} finally {
  client.release();
}

CRITICAL:


The 7-Step Setup Process

Step 1: Install Package

Choose based on your deployment platform:

Neon Direct (Cloudflare Workers, multi-cloud, direct Neon access):

npm install @neondatabase/serverless

Vercel Postgres (Vercel-specific, zero-config):

npm install @vercel/postgres

With ORM:

# Drizzle ORM (recommended)
npm install drizzle-orm @neondatabase/serverless
npm install -D drizzle-kit

# Prisma (alternative)
npm install prisma @prisma/client @prisma/adapter-neon @neondatabase/serverless

Key Points:


Step 2: Create Neon Database

Option A: Neon Dashboard

  1. Sign up at https://neon.tech
  2. Create a new project
  3. Copy the pooled connection string (important!)
  4. Format: postgresql://user:pass@ep-xyz-pooler.region.aws.neon.tech/db?sslmode=require

Option B: Vercel Dashboard

  1. Go to your Vercel project → Storage → Create Database → Postgres
  2. Vercel automatically creates a Neon database
  3. Run vercel env pull to get environment variables locally

Option C: Neon CLI (neonctl)

# Install CLI
npm install -g neonctl

# Authenticate
neonctl auth

# Create project
neonctl projects create --name my-app

# Get connection string
neonctl connection-string main

CRITICAL:


Step 3: Configure Environment Variables

For Neon Direct:

# .env or .env.local
DATABASE_URL="postgresql://user:password@ep-xyz-pooler.us-east-1.aws.neon.tech/neondb?sslmode=require"

For Vercel Postgres:

# Automatically created by `vercel env pull`
POSTGRES_URL="..."               # Pooled connection (use this for queries)
POSTGRES_PRISMA_URL="..."        # For Prisma migrations
POSTGRES_URL_NON_POOLING="..."   # Direct connection (avoid in serverless)
POSTGRES_USER="..."
POSTGRES_HOST="..."
POSTGRES_PASSWORD="..."
POSTGRES_DATABASE="..."

For Cloudflare Workers (wrangler.jsonc):

{
  "vars": {
    "DATABASE_URL": "postgresql://user:password@ep-xyz-pooler.us-east-1.aws.neon.tech/neondb?sslmode=require"
  }
}

Key Points:


Step 4: Create Database Schema

Option A: Raw SQL

// scripts/migrate.ts
import { neon } from '@neondatabase/serverless';

const sql = neon(process.env.DATABASE_URL!);

await sql`
  CREATE TABLE IF NOT EXISTS users (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT UNIQUE NOT NULL,
    created_at TIMESTAMP DEFAULT NOW()
  )
`;

Option B: Drizzle ORM (recommended)

// db/schema.ts
import { pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core';

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  name: text('name').notNull(),
  email: text('email').notNull().unique(),
  createdAt: timestamp('created_at').defaultNow()
});
// db/index.ts
import { drizzle } from 'drizzle-orm/neon-http';
import { neon } from '@neondatabase/serverless';
import * as schema from './schema';

const sql = neon(process.env.DATABASE_URL!);
export const db = drizzle(sql, { schema });
# Run migrations
npx drizzle-kit generate
npx drizzle-kit migrate

Option C: Prisma

// prisma/schema.prisma
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("POSTGRES_PRISMA_URL")
}

model User {
  id        Int      @id @default(autoincrement())
  name      String
  email     String   @unique
  createdAt DateTime @default(now()) @map("created_at")

  @@map("users")
}
npx prisma migrate dev --name init

CRITICAL:


Step 5: Query Patterns

Simple Queries (Neon Direct):

import { neon } from '@neondatabase/serverless';

const sql = neon(process.env.DATABASE_URL!);

// SELECT
const users = await sql`SELECT * FROM users WHERE email = ${email}`;

// INSERT
const newUser = await sql`
  INSERT INTO users (name, email)
  VALUES (${name}, ${email})
  RETURNING *
`;

// UPDATE
await sql`UPDATE users SET name = ${newName} WHERE id = ${id}`;

// DELETE
await sql`DELETE FROM users WHERE id = ${id}`;

Simple Queries (Vercel Postgres):

import { sql } from '@vercel/postgres';

// SELECT
const { rows } = await sql`SELECT * FROM users WHERE email = ${email}`;

// INSERT
const { rows: newUser } = await sql`
  INSERT INTO users (name, email)
  VALUES (${name}, ${email})
  RETURNING *
`;

Transactions (Neon Direct):

// Automatic transaction
const results = await sql.transaction([
  sql`INSERT INTO users (name) VALUES (${name})`,
  sql`UPDATE accounts SET balance = balance - ${amount} WHERE id = ${accountId}`
]);

// Manual transaction (for complex logic)
const result = await sql.transaction(async (sql) => {
  const [user] = await sql`INSERT INTO users (name) VALUES (${name}) RETURNING id`;
  await sql`INSERT INTO profiles (user_id) VALUES (${user.id})`;
  return user;
});

Transactions (Vercel Postgres):

import { sql } from '@vercel/postgres';

const client = await sql.connect();
try {
  await client.sql`BEGIN`;
  const { rows } = await client.sql`INSERT INTO users (name) VALUES (${name}) RETURNING id`;
  await client.sql`INSERT INTO profiles (user_id) VALUES (${rows[0].id})`;
  await client.sql`COMMIT`;
} catch (e) {
  await client.sql`ROLLBACK`;
  throw e;
} finally {
  client.release();
}

Drizzle ORM Queries:

import { db } from './db';
import { users } from './db/schema';
import { eq } from 'drizzle-orm';

// SELECT
const allUsers = await db.select().from(users);
const user = await db.select().from(users).where(eq(users.email, email));

// INSERT
const newUser = await db.insert(users).values({ name, email }).returning();

// UPDATE
await db.update(users).set({ name: newName }).where(eq(users.id, id));

// DELETE
await db.delete(users).where(eq(users.id, id));

// Transactions
await db.transaction(async (tx) => {
  await tx.insert(users).values({ name, email });
  await tx.insert(profiles).values({ userId: user.id });
});

Key Points:


Step 6: Handle Connection Pooling

Connection String Format:

Pooled (serverless):     postgresql://user:pass@ep-xyz-pooler.region.aws.neon.tech/db
Non-pooled (direct):     postgresql://user:pass@ep-xyz.region.aws.neon.tech/db

When to Use Each:

Automatic Pooling (Neon/Vercel):

// Both packages handle pooling automatically when using pooled connection string
import { neon } from '@neondatabase/serverless';
const sql = neon(process.env.DATABASE_URL!); // Pooling is automatic

Connection Limits:

CRITICAL:


Step 7: Deploy and Test

Cloudflare Workers:

// src/index.ts
import { neon } from '@neondatabase/serverless';

export default {
  async fetch(request: Request, env: Env) {
    const sql = neon(env.DATABASE_URL);
    const users = await sql`SELECT * FROM users`;
    return Response.json(users);
  }
};
# Deploy
npx wrangler deploy

Vercel (Next.js API Route):

// app/api/users/route.ts
import { sql } from '@vercel/postgres';

export async function GET() {
  const { rows } = await sql`SELECT * FROM users`;
  return Response.json(rows);
}
# Deploy
vercel deploy --prod

Test Queries:

# Local test
curl http://localhost:8787/api/users

# Production test
curl https://your-app.workers.dev/api/users

Key Points:


Critical Rules

Always Do

✅ Use pooled connection strings for serverless environments (-pooler. in hostname)

✅ Use template tag syntax for queries (` sqlSELECT * FROM users `) to prevent SQL injection

✅ Include sslmode=require in connection strings

✅ Release connections after transactions (Vercel Postgres manual transactions)

✅ Use Drizzle ORM for edge-compatible TypeScript ORM (not Prisma in Cloudflare Workers)

✅ Set connection string as environment variable (never hardcode)

✅ Use Neon branching for preview environments and testing

✅ Monitor connection pool usage in Neon dashboard

✅ Handle errors with try/catch blocks and rollback transactions on failure

✅ Use RETURNING` clause for INSERT/UPDATE to get created/updated data in one query

Never Do

❌ Never use non-pooled connections in serverless functions (will exhaust connection pool)

❌ Never concatenate SQL strings ('SELECT * FROM users WHERE id = ' + id) - SQL injection risk

❌ Never omit sslmode=require - connections will fail or be insecure

❌ Never forget to client.release() in manual Vercel Postgres transactions - connection leak

❌ Never use Prisma in Cloudflare Workers - requires Node.js runtime (use Drizzle instead)

❌ Never hardcode connection strings - use environment variables

❌ Never run migrations from edge functions - use Node.js environment or Neon console

❌ Never commit .env files - add to .gitignore

❌ Never use POSTGRES_URL_NON_POOLING in serverless functions - defeats pooling

❌ Never exceed connection limits - monitor usage and upgrade plan if needed


Known Issues Prevention

This skill prevents 15 documented issues:

Issue #1: Connection Pool Exhausted

Error: Error: connection pool exhausted or too many connections for role

Source: https://github.com/neondatabase/serverless/issues/12

Why It Happens: Using non-pooled connection string in high-concurrency serverless environment

Prevention: Always use pooled connection string (with -pooler. in hostname). Check your connection string format.

Issue #2: TCP Connections Not Supported

Error: Error: TCP connections are not supported in this environment

Source: Cloudflare Workers documentation

Why It Happens: Traditional Postgres clients use TCP sockets, which aren't available in edge runtimes

Prevention: Use @neondatabase/serverless (HTTP/WebSocket-based) instead of pg or postgres.js packages.

Issue #3: SQL Injection from String Concatenation

Error: Successful SQL injection attack or unexpected query results

Source: OWASP SQL Injection Guide

Why It Happens: Concatenating user input into SQL strings: sql('SELECT * FROM users WHERE id = ' + id)

Prevention: Always use template tag syntax: ` sqlSELECT * FROM users WHERE id = ${id} `. Template tags automatically escape values.

Issue #4: Missing SSL Mode

Error: Error: connection requires SSL or FATAL: no pg_hba.conf entry

Source: https://neon.tech/docs/connect/connect-securely

Why It Happens: Connection string missing ?sslmode=require parameter

Prevention: Always append ?sslmode=require to connection string.

Issue #5: Connection Leak (Vercel Postgres)

Error: Gradually increasing memory usage, eventual timeout errors

Source: https://github.com/vercel/storage/issues/45

Why It Happens: Forgetting to call client.release() after manual transactions

Prevention: Always use try/finally block and call client.release() in finally block.

Issue #6: Wrong Environment Variable (Vercel)

Error: Error: Connection string is undefined or connect ECONNREFUSED

Source: https://vercel.com/docs/storage/vercel-postgres/using-an-orm

Why It Happens: Using DATABASE_URL instead of POSTGRES_URL, or vice versa

Prevention: Use POSTGRES_URL for queries, POSTGRES_PRISMA_URL for Prisma migrations.

Issue #7: Transaction Timeout in Edge Functions

Error: Error: Query timeout or Error: transaction timeout

Source: https://neon.tech/docs/introduction/limits

Why It Happens: Long-running transactions exceed edge function timeout (typically 30s)

Prevention: Keep transactions short (<5s), batch operations, or move complex transactions to background workers.

Issue #8: Prisma in Cloudflare Workers

Error: Error: PrismaClient is unable to be run in the browser or module resolution errors

Source: https://github.com/prisma/prisma/issues/18765

Why It Happens: Prisma requires Node.js runtime with filesystem access

Prevention: Use Drizzle ORM for Cloudflare Workers. Prisma works in Vercel Edge/Node.js runtimes only.

Issue #9: Branch API Authentication Error

Error: Error: Unauthorized when calling Neon API

Source: https://neon.tech/docs/api/authentication

Why It Happens: Missing or invalid NEON_API_KEY environment variable

Prevention: Create API key in Neon dashboard → Account Settings → API Keys, set as environment variable.

Issue #10: Stale Connection After Branch Delete

Error: Error: database "xyz" does not exist after deleting a branch

Source: https://neon.tech/docs/guides/branching

Why It Happens: Application still using connection string from deleted branch

Prevention: Update DATABASE_URL when switching branches, restart application after branch changes.

Issue #11: Query Timeout on Cold Start

Error: Error: Query timeout on first request after idle period

Source: https://neon.tech/docs/introduction/auto-suspend

Why It Happens: Neon auto-suspends compute after inactivity, ~1-2s to wake up

Prevention: Expect cold starts, set query timeout >= 10s, or disable auto-suspend (paid plans).

Issue #12: Drizzle Schema Mismatch

Error: TypeScript errors like Property 'x' does not exist on type 'User'

Source: https://orm.drizzle.team/docs/generate

Why It Happens: Database schema changed but Drizzle types not regenerated

Prevention: Run npx drizzle-kit generate after schema changes, commit generated files.

Issue #13: Migration Conflicts Across Branches

Error: Error: relation "xyz" already exists or migration version conflicts

Source: https://neon.tech/docs/guides/branching#schema-migrations

Why It Happens: Multiple branches with different migration histories

Prevention: Create branches AFTER running migrations on main, or reset branch schema before merging.

Issue #14: PITR Timestamp Out of Range

Error: Error: timestamp is outside retention window

Source: https://neon.tech/docs/introduction/point-in-time-restore

Why It Happens: Trying to restore from a timestamp older than retention period (7 days on free tier)

Prevention: Check retention period for your plan, restore within allowed window.

Issue #15: Wrong Adapter for Prisma

Error: Error: Invalid connection string or slow query performance

Source: https://www.prisma.io/docs/orm/overview/databases/neon

Why It Happens: Not using @prisma/adapter-neon for serverless environments

Prevention: Install @prisma/adapter-neon and @neondatabase/serverless, configure Prisma to use HTTP-based connection.


Configuration Files Reference

package.json (Neon Direct)

{
  "dependencies": {
    "@neondatabase/serverless": "^1.0.2"
  }
}

package.json (Vercel Postgres)

{
  "dependencies": {
    "@vercel/postgres": "^0.10.0"
  }
}

package.json (With Drizzle ORM)

{
  "dependencies": {
    "@neondatabase/serverless": "^1.0.2",
    "drizzle-orm": "^0.44.7"
  },
  "devDependencies": {
    "drizzle-kit": "^0.31.0"
  },
  "scripts": {
    "db:generate": "drizzle-kit generate",
    "db:migrate": "drizzle-kit migrate",
    "db:studio": "drizzle-kit studio"
  }
}

drizzle.config.ts

import { defineConfig } from 'drizzle-kit';

export default defineConfig({
  schema: './db/schema.ts',
  out: './db/migrations',
  dialect: 'postgresql',
  dbCredentials: {
    url: process.env.DATABASE_URL!
  }
});

Why these settings:


Common Patterns

Pattern 1: Cloudflare Worker with Neon

// src/index.ts
import { neon } from '@neondatabase/serverless';

interface Env {
  DATABASE_URL: string;
}

export default {
  async fetch(request: Request, env: Env) {
    const sql = neon(env.DATABASE_URL);

    // Parse request
    const url = new URL(request.url);

    if (url.pathname === '/api/users' && request.method === 'GET') {
      const users = await sql`SELECT id, name, email FROM users`;
      return Response.json(users);
    }

    if (url.pathname === '/api/users' && request.method === 'POST') {
      const { name, email } = await request.json();
      const [user] = await sql`
        INSERT INTO users (name, email)
        VALUES (${name}, ${email})
        RETURNING *
      `;
      return Response.json(user, { status: 201 });
    }

    return new Response('Not Found', { status: 404 });
  }
};

When to use: Cloudflare Workers deployment with Postgres database


Pattern 2: Next.js Server Action with Vercel Postgres

// app/actions/users.ts
'use server';

import { sql } from '@vercel/postgres';
import { revalidatePath } from 'next/cache';

export async function getUsers() {
  const { rows } = await sql`SELECT id, name, email FROM users ORDER BY created_at DESC`;
  return rows;
}

export async function createUser(formData: FormData) {
  const name = formData.get('name') as string;
  const email = formData.get('email') as string;

  const { rows } = await sql`
    INSERT INTO users (name, email)
    VALUES (${name}, ${email})
    RETURNING *
  `;

  revalidatePath('/users');
  return rows[0];
}

export async function deleteUser(id: number) {
  await sql`DELETE FROM users WHERE id = ${id}`;
  revalidatePath('/users');
}

When to use: Next.js Server Actions with Vercel Postgres


Pattern 3: Drizzle ORM with Type Safety

// db/schema.ts
import { pgTable, serial, text, timestamp, integer } from 'drizzle-orm/pg-core';

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  name: text('name').notNull(),
  email: text('email').notNull().unique(),
  createdAt: timestamp('created_at').defaultNow()
});

export const posts = pgTable('posts', {
  id: serial('id').primaryKey(),
  userId: integer('user_id').notNull().references(() => users.id),
  title: text('title').notNull(),
  content: text('content'),
  createdAt: timestamp('created_at').defaultNow()
});
// db/index.ts
import { drizzle } from 'drizzle-orm/neon-http';
import { neon } from '@neondatabase/serverless';
import * as schema from './schema';

const sql = neon(process.env.DATABASE_URL!);
export const db = drizzle(sql, { schema });
// app/api/posts/route.ts
import { db } from '@/db';
import { posts, users } from '@/db/schema';
import { eq } from 'drizzle-orm';

export async function GET() {
  // Type-safe query with joins
  const postsWithAuthors = await db
    .select({
      postId: posts.id,
      title: posts.title,
      content: posts.content,
      authorName: users.name
    })
    .from(posts)
    .leftJoin(users, eq(posts.userId, users.id));

  return Response.json(postsWithAuthors);
}

When to use: Need type-safe queries, complex joins, edge-compatible ORM


Pattern 4: Database Transactions

// Neon Direct - Automatic Transaction
import { neon } from '@neondatabase/serverless';

const sql = neon(process.env.DATABASE_URL!);

const result = await sql.transaction(async (tx) => {
  // Deduct from sender
  const [sender] = await tx`
    UPDATE accounts
    SET balance = balance - ${amount}
    WHERE id = ${senderId} AND balance >= ${amount}
    RETURNING *
  `;

  if (!sender) {
    throw new Error('Insufficient funds');
  }

  // Add to recipient
  await tx`
    UPDATE accounts
    SET balance = balance + ${amount}
    WHERE id = ${recipientId}
  `;

  // Log transaction
  await tx`
    INSERT INTO transfers (from_id, to_id, amount)
    VALUES (${senderId}, ${recipientId}, ${amount})
  `;

  return sender;
});

When to use: Multiple related database operations that must all succeed or all fail


Pattern 5: Neon Branching for Preview Environments

# Create branch for PR
neonctl branches create --project-id my-project --name pr-123 --parent main

# Get connection string for branch
BRANCH_URL=$(neonctl connection-string pr-123)

# Use in Vercel preview deployment
vercel env add DATABASE_URL preview
# Paste $BRANCH_URL

# Delete branch when PR is merged
neonctl branches delete pr-123
# .github/workflows/preview.yml
name: Create Preview Database
on:
  pull_request:
    types: [opened, synchronize]

jobs:
  preview:
    runs-on: ubuntu-latest
    steps:
      - name: Create Neon Branch
        run: |
          BRANCH_NAME="pr-${{ github.event.pull_request.number }}"
          neonctl branches create --project-id ${{ secrets.NEON_PROJECT_ID }} --name $BRANCH_NAME
          BRANCH_URL=$(neonctl connection-string $BRANCH_NAME)

      - name: Deploy to Vercel
        env:
          DATABASE_URL: ${{ steps.branch.outputs.url }}
        run: vercel deploy --env DATABASE_URL=$DATABASE_URL

When to use: Want isolated database for each PR/preview deployment


Using Bundled Resources

Scripts (scripts/)

setup-neon.sh - Creates Neon database and outputs connection string

chmod +x scripts/setup-neon.sh
./scripts/setup-neon.sh my-project-name

test-connection.ts - Verifies database connection and runs test query

npx tsx scripts/test-connection.ts

References (references/)

When Claude should load these:

Assets (assets/)


Advanced Topics

Database Branching Workflows

Neon's branching feature allows git-like workflows for databases:

Branch Types:

Branch Creation:

# Create from main
neonctl branches create --name dev --parent main

# Create from specific point in time (PITR)
neonctl branches create --name restore-point --parent main --timestamp "2025-10-28T10:00:00Z"

# Create from another branch
neonctl branches create --name feature --parent dev

Branch Management:

# List branches
neonctl branches list

# Get connection string
neonctl connection-string dev

# Delete branch
neonctl branches delete feature

# Reset branch to match parent
neonctl branches reset dev --parent main

Use Cases:

CRITICAL:


Connection Pooling Deep Dive

How Pooling Works:

  1. Client requests a connection
  2. Pooler assigns an existing idle connection or creates new one
  3. Client uses connection for query
  4. Connection returns to pool (reusable)

Pooled vs Non-Pooled:

| Feature | Pooled (-pooler.) | Non-Pooled |

|---------|---------------------|------------|

| Use Case | Serverless, edge functions | Long-running servers |

| Max Connections | ~10,000 (shared) | ~100 (per database) |

| Connection Reuse | Yes | No |

| Latency | +1-2ms overhead | Direct |

| Idle Timeout | 60s | Configurable |

When Connection Pool Fills:

Error: connection pool exhausted

Solutions:

  1. Use pooled connection string (most common fix)
  2. Upgrade to higher tier (more connection slots)
  3. Optimize queries (reduce connection time)
  4. Implement connection retry logic
  5. Use read replicas (distribute load)

Monitoring:


Optimizing Query Performance

Use EXPLAIN ANALYZE:

const result = await sql`
  EXPLAIN ANALYZE
  SELECT * FROM users WHERE email = ${email}
`;

Create Indexes:

await sql`CREATE INDEX idx_users_email ON users(email)`;
await sql`CREATE INDEX idx_posts_user_id ON posts(user_id)`;

Use Drizzle Indexes:

import { pgTable, serial, text, index } from 'drizzle-orm/pg-core';

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  email: text('email').notNull().unique()
}, (table) => ({
  emailIdx: index('email_idx').on(table.email)
}));

Batch Queries:

// ❌ Bad: N+1 queries
for (const user of users) {
  const posts = await sql`SELECT * FROM posts WHERE user_id = ${user.id}`;
}

// ✅ Good: Single query with JOIN
const postsWithUsers = await sql`
  SELECT users.*, posts.*
  FROM users
  LEFT JOIN posts ON posts.user_id = users.id
`;

Use Prepared Statements (Drizzle):

const getUserByEmail = db.select().from(users).where(eq(users.email, sql.placeholder('email'))).prepare('get_user_by_email');

// Reuse prepared statement
const user1 = await getUserByEmail.execute({ email: 'alice@example.com' });
const user2 = await getUserByEmail.execute({ email: 'bob@example.com' });

Security Best Practices

1. Never Expose Connection Strings

// ❌ Bad
const sql = neon('postgresql://user:pass@host/db');

// ✅ Good
const sql = neon(process.env.DATABASE_URL!);

2. Use Row-Level Security (RLS)

-- Enable RLS
ALTER TABLE posts ENABLE ROW LEVEL SECURITY;

-- Create policy
CREATE POLICY "Users can only see their own posts"
  ON posts
  FOR SELECT
  USING (user_id = current_user_id());

3. Validate Input

// ✅ Validate before query
const emailSchema = z.string().email();
const email = emailSchema.parse(input.email);

const user = await sql`SELECT * FROM users WHERE email = ${email}`;

4. Limit Query Results

// ✅ Always paginate
const page = Math.max(1, parseInt(request.query.page));
const limit = 50;
const offset = (page - 1) * limit;

const users = await sql`
  SELECT * FROM users
  ORDER BY created_at DESC
  LIMIT ${limit} OFFSET ${offset}
`;

5. Use Read-Only Roles for Analytics

CREATE ROLE readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;

Dependencies

Required:

Optional:


Official Documentation


Package Versions (Verified 2025-10-29)

{
  "dependencies": {
    "@neondatabase/serverless": "^1.0.2",
    "@vercel/postgres": "^0.10.0",
    "drizzle-orm": "^0.44.7"
  },
  "devDependencies": {
    "drizzle-kit": "^0.31.0",
    "neonctl": "^2.16.1"
  }
}

Latest Prisma (if needed):

{
  "dependencies": {
    "@prisma/client": "^6.10.0",
    "@prisma/adapter-neon": "^6.10.0"
  },
  "devDependencies": {
    "prisma": "^6.10.0"
  }
}

Production Example

This skill is based on production deployments of Neon and Vercel Postgres:


Troubleshooting

Problem: `Error: connection pool exhausted`

Solution:

  1. Verify you're using pooled connection string (ends with -pooler.region.aws.neon.tech)
  2. Check connection usage in Neon dashboard
  3. Upgrade to higher tier if consistently hitting limits
  4. Optimize queries to reduce connection hold time

Problem: `Error: TCP connections are not supported`

Solution:

Problem: `Error: database "xyz" does not exist`

Solution:

Problem: Slow queries on cold start

Solution:

Problem: `PrismaClient is unable to be run in the browser`

Solution:

Problem: Migration version conflicts across branches

Solution:


Complete Setup Checklist

Use this checklist to verify your setup:


Questions? Issues?

  1. Check references/common-errors.md for extended troubleshooting
  2. Verify all steps in the 7-step setup process
  3. Check official docs: https://neon.tech/docs
  4. Ensure you're using pooled connection string for serverless environments
  5. Verify sslmode=require is in connection string
  6. Test connection with scripts/test-connection.ts
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.