pickstack

Next.js + PostgreSQL (Drizzle)

PostgreSQL with Drizzle ORM: a schema, migration commands, a todo example and a local docker-compose. This page shows exactly what pickstack adds to a Next.js project for PostgreSQL (Drizzle): the packages, the new files and the changes to existing ones. Every combination is installed, built, linted and tested in CI.

$ npm create pickstack@latest my-app -- --framework next --modules drizzle --yes
Customize in the configurator

Packages it adds

PackageVersionType
drizzle-orm^0.45.4runtime
pg^8.23.1runtime
@types/pg^8.23.1dev
dotenv^18.0.10dev
drizzle-kit^0.31.11dev

New files

.env.example
DATABASE_URL=postgres://postgres:postgres@localhost:5432/my-app
app/todos/page.tsx
import { desc } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import { connection } from "next/server";
import { db } from "@/db";
import { todos } from "@/db/schema";

async function addTodo(formData: FormData) {
  "use server";
  const title = String(formData.get("title") ?? "").trim();
  if (!title) return;
  await db.insert(todos).values({ title: title.slice(0, 200) });
  revalidatePath("/todos");
}

export default async function Todos() {
  // Render per request: the database is not there when the app is built.
  await connection();
  const rows = await db
    .select()
    .from(todos)
    .orderBy(desc(todos.createdAt))
    .limit(100)
    .catch(() => null);

  return (
    <section className="page">
      <h1 className="title">Todos</h1>
      <form action={addTodo} className="row">
        <input
          name="title"
          required
          maxLength={200}
          placeholder="What needs doing?"
        />
        <button className="btn" type="submit">
          Add
        </button>
      </form>
      {rows ? (
        <ul>
          {rows.map((todo) => (
            <li key={todo.id}>{todo.title}</li>
          ))}
        </ul>
      ) : (
        <p className="text">
          The database isn&apos;t ready: run docker compose up -d, then npm run
          db:push.
        </p>
      )}
    </section>
  );
}
db/index.ts
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

// Connects lazily: the first query opens a connection.
const pool = new Pool({
  connectionString:
    process.env.DATABASE_URL ??
    "postgres://postgres:postgres@localhost:5432/my-app",
});
// An idle connection can drop (e.g. Postgres restarts); without a listener that crashes the process.
pool.on("error", (error) =>
  console.error("Postgres pool error:", error.message),
);

export const db = drizzle({ client: pool });
db/schema.ts
import {
  boolean,
  integer,
  pgTable,
  timestamp,
  varchar,
} from "drizzle-orm/pg-core";

export const todos = pgTable("todos", {
  id: integer().primaryKey().generatedAlwaysAsIdentity(),
  title: varchar({ length: 200 }).notNull(),
  done: boolean().notNull().default(false),
  createdAt: timestamp("created_at").notNull().defaultNow(),
});
docker-compose.yml
# Local PostgreSQL for development: docker compose up -d
services:
  postgres:
    image: postgres:18
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: my-app
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql

volumes:
  postgres-data:
drizzle.config.ts
import "dotenv/config";
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  out: "./drizzle",
  schema: "./db/schema.ts",
  dialect: "postgresql",
  dbCredentials: {
    url:
      process.env.DATABASE_URL ??
      "postgres://postgres:postgres@localhost:5432/my-app",
  },
});

Changes to existing files

app/about/page.tsx (changed)
⋯ 2 unchanged lines     <section className="page">       <h1 className="title">About</h1>       <p className="text">+        Built with Next.js (App Router), React 19, TypeScript, PostgreSQL,+        Drizzle ORM.−        Built with Next.js (App Router), React 19, TypeScript.       </p>     </section>   );⋯ 2 unchanged lines
app/layout.tsx (changed)
⋯ 18 unchanged lines             <Link className="link" href="/about">               About             </Link>+            <Link className="link" href="/todos">+              Todos+            </Link>           </nav>           {children}         </main>⋯ 5 unchanged lines

Project files

Related templates