Next.js 16 + Prisma 7 + Postgres: from a fresh app to production
Build a small tasks app on a brand-new Next.js 16 project: a Postgres database on Neon, Prisma 7 for the queries, server actions for the changes, and a deploy to Vercel. Every file is shown in full, and every step was run on a fresh project before this was published.
Tested step by step on with Next.js 16.4.6 and Prisma 7.10.1
By the end of this guide you’ll have a working tasks app: a page that lists tasks from a Postgres database, a form to add one, and buttons to tick one off or delete it — live on the internet. Along the way you’ll learn the parts every database-backed Next.js app needs: a database, Prisma’s schema and migrations, a database client, seed data, server actions, and a deploy.
Every step shows the whole file, so you can copy it as it is. The guide was run from start to finish on a fresh create-next-app project and a fresh shadcn init project, with Next.js 16.4, Prisma 7.10 and Neon Postgres.
Before you start
- Node.js 20.9 or newer. Check with
node -v. - A new Next.js 16 app. Either
npx create-next-app@latest my-app --yesornpx shadcn@latest initworks. Accept the defaults. - A free Neon account for the database. (A Postgres running on your own computer works too.)
- For the last step only: a GitHub account and a free Vercel account.
Run every command from your project’s folder (the one with package.json in it).
1. Install the packages
prismais the command line you’ll use to change the database;@prisma/clientis the code your app calls to read and write it.@prisma/adapter-pgis the Postgres driver. Prisma 7 needs one: it no longer ships its own.dotenvlets Prisma’s command line read your.envfile, andtsxruns the TypeScript seed script in step 7.
@7 on all three Prisma packages: they must share one major version. Without it, npm can install a newer major (at the time of writing, prisma alone installs an 8.0 release candidate) and the packages stop matching.2. Create the database
- Sign in to Neon and create a project. Any name and region works; pick the region closest to where you’ll deploy (Vercel’s default is Washington, D.C., so
US East). - Click Connect. With Connection pooling switched on, copy the connection string. This is the one your app uses.
- Switch Connection pooling off and copy the string again. This is the direct one; its host has no
-poolerin it. Prisma’s commands use this one.
Create a file called .env next to package.json and paste both strings in:
- Why two? A pooled connection shares a small number of real database connections between many requests, which is what a serverless app needs. Migrations need a direct connection of their own. Neon and Prisma both recommend this split.
- Using a local Postgres instead? Put the same string in both lines, for example
postgresql://postgres@localhost:5432/tasks. - Both starters already list
.env*in.gitignore, so your passwords stay out of Git.
3. Configure Prisma
Create prisma.config.ts next to package.json. It tells Prisma’s command line where your schema, your migrations and your database are.
The first line, import "dotenv/config", is what reads .env. Prisma 7 no longer does it by itself, so without it every command says the database URL is missing.
4. Describe your data
Make a folder called prisma and create schema.prisma in it. A model is a table: this one is a list of tasks, each with a title and a done/not-done flag.
@id @default(autoincrement()): each task gets the next number, 1, 2, 3…@default(false)and@default(now()): values Postgres fills in when you don’t give one.output = "../generated/prisma": where Prisma writes the client code it generates from this file.
That generated folder is rebuilt from the schema, so keep it out of Git. Open .gitignore and add this line at the end:
5. Create the table
- The first command compares your schema with the database, writes the SQL that makes them match into
prisma/migrations/<date>_init/migration.sql, and runs it. It ends with “Your database is now in sync with your schema.” Commit theprisma/migrationsfolder: it is the history of your database. - The second writes the client into
generated/prismaand prints “Generated Prisma Client”. Prisma 7 no longer does this insidemigrate dev, so run it every time you change the schema, or your code won’t see the change.
6. Create the database client
This file creates the one Prisma client the whole app shares. It connects through the pooled DATABASE_URL, and keeping it on globalThis stops every hot reload in development from opening new connections.
The import is relative (../generated/…) rather than @/generated/… so that tools which don’t read your TypeScript paths, like the seed script below or Better Auth’s command line, can load this file too.
7. Add some starting data
A seed script fills an empty database with something to look at. Create seed.ts in the prisma folder:
It should print “Seeded 3 tasks”. It empties the table first, so you can run it again any time to get back to these three. To look at the rows yourself, run npx prisma studio and open the Task table in the browser tab it opens.
8. Write the server actions
Server actions are functions that run on the server when a form is submitted. These three add, tick and delete a task. Create the folder app/tasks and this file in it:
"use server"at the top makes every exported function a server action. Anyone can call one with any data, so check the input on the server, asaddTaskdoes, even if the form checks it too.revalidatePath("/tasks")tells Next.js the page’s data changed, so it shows the new list straight away.deleteManyinstead ofdelete: deleting a task that’s already gone (a double click) does nothing instead of throwing an error.
9. Build the form
The add form is a small client component, because it shows the server’s error message under the input:
useActionState gives you three things: what the action last returned (state, our error or null), the function to put on the form, and whether it’s running (pending). After a successful add, React clears the form for you. Because it’s a real <form> with an action, it even works before the page’s JavaScript has loaded.
10. Build the page
The tick and delete buttons are tiny forms, each sending the task’s id in a hidden field. The page itself is a server component: it reads the database directly, with no API route in between.
<Suspense> and connection()? A new create-next-app project turns on Cache Components, which tries to build every page ahead of time. A page that simply awaits prisma.task.findMany() then fails npm run build with “Next.js encountered uncached or runtime data during prerendering”. connection() says “this part needs a real request”, so the list is read fresh every time, and <Suspense> lets the rest of the page show instantly while it loads. The same code works in a project without Cache Components.11. Try it
Start the dev server with npm run dev, open http://localhost:3000/tasks, and check:
- The three seeded tasks are listed, “Create the first migration” crossed out.
- Click Add with an empty box: “Write a task first.” appears under it.
- Type a task and click Add: it appears at the bottom and the box empties.
- Click a task’s ☐: it’s crossed out. Click ☑ to undo.
- Click Delete: it’s gone. Refresh the page: everything you did is still there, because it’s in the database.
Then stop the dev server and run npm run build. It should finish without errors.
12. Change the schema
Real apps change their data all the time. Say tasks need an optional due date. Update the model in prisma/schema.prisma (the whole file again, with one new line):
Then the same two commands as before, with a name that says what changed:
The ? in DateTime? makes the field optional, so the tasks you already have simply get no due date and nothing is lost. prisma.task now knows about dueDate: TypeScript will suggest it in create, update and findMany. Every schema change follows the same pattern: edit the schema, migrate dev, generate, commit the new migration.
13. Deploy to Vercel
The generated folder isn’t in Git, so the server has to build it after installing your packages. In package.json, add this line inside "scripts":
- Push your project to a new GitHub repository.
- In Vercel, click Add New → Project, import the repository, and before deploying open Environment Variables. Add
DATABASE_URLandDATABASE_URL_UNPOOLEDwith the same two values as your.env. - Click Deploy. When it’s done, open
/taskson your new address: the same tasks are there, because it’s the same database.
Vercel builds your app; it doesn’t run your migrations. Your .env points at the same database Vercel uses, so before deploying a schema change run:
It applies any migration the database hasn’t had yet, and says “No pending migrations to apply.” when it’s up to date (as it is now — migrate dev already applied yours). Once real people use your app, give development its own database (Neon calls it a branch) so your experiments never touch their data.
If something goes wrong
- “The datasource.url property is required in your Prisma config file”.
.envis missing, misnamed or not next topackage.json, orprisma.config.tslost its first line,import "dotenv/config". - “Error: P1001: Can’t reach database server”. The connection string is wrong or incomplete. Copy it again from Neon’s Connect dialog, including the part after
?. - “Cannot find module '../generated/prisma/client'”. The client hasn’t been generated. Run
npx prisma generate. - “The table
public.Taskdoes not exist in the current database.” The migration hasn’t run against this database. Runnpx prisma migrate dev(ornpx prisma migrate deployfor production). npm run buildsays “uncached or runtime data during prerendering”. A page reads the database outside<Suspense>. Read it in a component inside<Suspense>and callawait connection()first, as in step 10.- TypeScript doesn’t know a field you just added. Run
npx prisma generate, then restart the dev server. - A “SECURITY WARNING: The SSL modes 'prefer', 'require', and 'verify-ca'…” line in the terminal. It’s harmless: the Postgres driver is talking about the
sslmode=requirein Neon’s string. Change it tosslmode=verify-fullin both lines of.env(and in Vercel) and it goes away.
Questions people ask
Do I need two database URLs with Prisma 7?
If your database has a connection pooler, as Neon does, yes. The app uses the pooled URL, because a serverless app opens lots of short connections and a pool shares a few real ones between them. Migrations need a direct connection, so Prisma’s command line gets the unpooled one. On a local Postgres, put the same URL in both.
Why can’t Prisma 7 find my database URL?
Prisma 7 stopped reading .env by itself. The import "dotenv/config" line at the top of prisma.config.ts does it now. Leave that line out and every Prisma command says the URL is missing, even though it’s sitting right there in .env.
Do I have to run prisma generate after every migration?
Yes. In Prisma 7, migrate dev changes the database but no longer rebuilds the client, so run npx prisma generate straight after it. Skip it and TypeScript won’t know about the field you just added. On Vercel, the postinstall line from step 13 runs it for you on every deploy.
Why does npm run build fail with “uncached or runtime data during prerendering”?
A new create-next-app project has Cache Components on, so Next.js tries to build every page ahead of time, and a page that just awaits a database query can’t be built that way. Put the part that reads the database inside <Suspense> and call await connection() in it, like the tasks page does.
Where to go next
- Give every person their own tasks: add sign-up and sign-in with Better Auth, which builds on exactly this setup.
- Add a
userIdtoTaskand filterfindManyby the signed-in user — the same schema-change steps as step 12. - Read the Prisma Client reference for filtering, sorting and relations.