← Back to Blog
PostgreSQLNext.jsDatabase

PostgreSQL + Next.js: Complete Setup Guide

24 April 2026 · by Yunmin Shin

Why PostgreSQL for Next.js Projects?

PostgreSQL is the most capable open-source relational database available. It handles JSON natively, supports full-text search, has excellent indexing options, and scales vertically and horizontally. For Bangkok businesses building applications that manage real data — orders, users, inventory, bookings — PostgreSQL is the correct default choice.

Paired with Next.js, the combination covers the full stack: React for UI, Next.js for server logic and routing, and PostgreSQL for persistent storage.

How Do You Connect PostgreSQL to Next.js?

The recommended approach in 2026 uses a connection pool rather than a direct connection. Direct connections do not survive serverless function cold starts cleanly. Neon and Supabase both provide hosted PostgreSQL with built-in connection pooling via PgBouncer. For self-hosted setups, use PgBouncer separately.

Install the Neon serverless driver or the standard pg package depending on your hosting environment:

npm install @neondatabase/serverless
# or for standard environments
npm install pg

Create a lib/db.ts file that exports your database connection. Using a module-level singleton prevents opening a new connection on every request in development.

What ORM Should You Use?

Drizzle ORM has become the preferred choice for TypeScript Next.js projects. It is type-safe, generates SQL you can actually read, and has zero magic — the queries you write are close to the SQL that executes.

Prisma remains a valid alternative with a better developer experience for beginners, but its query engine adds latency in serverless environments. Drizzle runs as pure JavaScript with no binary dependencies, making cold starts faster.

Define your schema in db/schema.ts, run drizzle-kit generate to create migration files, and drizzle-kit migrate to apply them. Keep migration files in version control.

How Do You Handle Migrations in Production?

Never run migrations automatically on application startup in production. This creates a race condition when multiple instances of your app deploy simultaneously. Instead:

  1. Run migrations as a separate step in your CI/CD pipeline before the new code is deployed.
  2. Write backward-compatible migrations — add columns as nullable before making them required.
  3. Keep a record of applied migrations in a drizzle_migrations table (Drizzle handles this automatically).

For Bangkok projects on Vercel, add a build step that runs migrations via drizzle-kit migrate before the Next.js build completes.

What About Connection Pooling in Serverless?

Serverless functions create a new connection on each cold start. Without pooling, a traffic spike will exhaust PostgreSQL's connection limit rapidly. The fix:

  • Use Neon's serverless driver which handles HTTP-based queries that do not hold connections open.
  • Or use PgBouncer in transaction-mode pooling to reuse connections across function invocations.

Neon's free tier is sufficient for early-stage Bangkok projects and scales to paid plans as traffic grows.

Ready to Build Something Fast?

Get a free quote on LINE. We reply within 24 hours.

Ready to build something fast and scalable?

Get a free project quote on LINE. We reply within 24 hours.

무료 견적 on LINE