Skip to content

Deployment · By Rivtor · Updated

Deploy Next.js with Postgres and Prisma

Keep Prisma access in server-side Next.js code, generate its client during the build and apply reviewed migrations to the intended runtime database. A generated client does not update the schema.

See the Rivtor deployment workflow →

Keep database work server-side

Use server components, route handlers or server actions for database access. Browser components should call an authorized interface rather than receive database credentials. Validate user input and check account ownership before returning records. Hiding a button does not authorize an operation.

Separate generation from migration

Prisma generation produces application code; migrate deploy applies database changes. Commit schema and migration files and keep compatible client and CLI versions. Do not run reset commands or development-only schema synchronization against valuable production data.

Make rendering behavior deliberate

Static generation can execute server code during the build. A database that is reachable only at runtime will not become available simply because a page is a server component. Choose the page’s rendering strategy deliberately and inspect build logs for unintended database connections.

Budget connections across instances

Reuse clients appropriately within a process and understand the platform’s pooling mode. Transaction pooling has different semantics from a dedicated database session. After release, check a real authorized query, persistence and the error path rather than relying only on a successful build.

Command reference

npx prisma generate
npx prisma migrate deploy

These commands illustrate the documented workflow. Replace example addresses and identifiers with your own. Deployment and migration commands affect the selected project; confirm the target before running them.

Separate the three production operations

A Prisma-backed Next.js app has three distinct steps:

npx prisma generate
npm run build
npx prisma migrate deploy

Generation creates client code. The Next.js build compiles the application and may execute server code while prerendering. Migration deployment applies committed migrations to the database selected by your configuration. Their order and execution environment must fit your release process: do not let newly released code receive traffic before required schema changes are complete.

A build requiring database reads needs suitable build-time access or a rendering strategy that performs the read at request time. Supplying a runtime variable cannot repair a build that has already failed. Review environment-variable timing.

Keep the database client on the server

Import your database module only from server-side code. Avoid a use-client boundary around modules that read DATABASE_URL. Reuse a PrismaClient instance appropriately within each process, and consult the installed Prisma version's connection-management instructions.

Do not copy a client initialization snippet from another major version without checking whether your project uses a generated client output, a driver adapter or a datasource URL in a different configuration file. Keep the generator, schema, migrations and application imports in agreement.

Treat migration history as an application artifact

Commit the migrations directory. The production command applies committed migrations; it does not infer a reviewed production change from every edit to schema.prisma. A development reset command is unsuitable for retaining customer records.

Before a release, inspect pending changes for column removal, required fields without backfills and incompatible renames. Rehearse on a disposable database containing representative synthetic records. Use an expand-and-contract sequence when old and new application versions must overlap.

Verify persistence and concurrency

Test a server-rendered database read and an authenticated write using synthetic data. Refresh and confirm the record persists. Review connection pooling before increasing replicas: a healthy single process says little about the total connection allowance.

If migration deployment fails, preserve its first error and target identity without logging credentials. If the build attempts to connect to localhost, check whether development configuration leaked into production. Follow Rivtor's quickstart for deployment and verification documentation for interpreting report coverage.

Sources: Prisma production migrations and Next.js environment variables.

Next steps

Read the Rivtor reference for supported configuration and limitations, and use the deployment quickstart when trying Rivtor for the first time. Validate workflows with synthetic data before connecting valuable production records.