Skip to content

PostgreSQL & Prisma

NexusOrbit ships with MongoDB by default. If you prefer PostgreSQL, you can point Prisma at a Postgres database and use migrations instead of db push. This guide covers schema setup, environment variables, npm scripts, and a typical dev-to-deploy workflow.

Quick start (PostgreSQL branch)

For a repo that is already configured for PostgreSQL, use the dedicated PostgreSQL branch in your version control: check it out and follow that branch’s README. The steps below apply when you are switching the datasource yourself.

1. Update Prisma schema (prisma/schema.prisma)

Set the datasource provider to postgresql and ensure models use PostgreSQL-compatible types.

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

generator client {
  provider = "prisma-client-js"
}

model User {
  id            String    @id @default(uuid())
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  password      String?
  image         String?
  accounts      Account[]
  customerId    String?
  priceId       String?
  isAdmin       Boolean   @default(false)
  hasAccess     Boolean   @default(false)
  subscribedAt  DateTime?
  createdAt     DateTime  @default(now())
  updatedAt     DateTime  @updatedAt

  @@map("users")
}

model Account {
  id                String   @id @default(uuid())
  userId            String
  type              String
  provider          String
  providerAccountId String
  refresh_token     String?  @db.Text
  access_token      String?  @db.Text
  expires_at        Int?
  token_type        String?
  scope             String?
  id_token          String?  @db.Text
  session_state     String?
  createdAt         DateTime @default(now())
  updatedAt         DateTime @updatedAt
  user              User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([provider, providerAccountId])
  @@map("accounts")
}

model VerificationToken {
  id         String   @id @default(uuid())
  identifier String
  token      String
  expires    DateTime

  @@unique([identifier, token])
  @@map("verification_tokens")
}

model PasswordResetToken {
  id        String   @id @default(uuid())
  email     String
  token     String   @unique
  expires   DateTime
  createdAt DateTime @default(now())

  @@map("password_reset_tokens")
}

Adjust or extend models as your product evolves; then create migrations (see below).

2. Configure PostgreSQL (DATABASE_URL)

Set your PostgreSQL connection string in .env at the project root.

bash
# Standard PostgreSQL
DATABASE_URL="postgresql://[USER]:[PASSWORD]@[HOST]:[PORT]/[DATABASE]"

# Example: Supabase (replace placeholders)
# DATABASE_URL="postgresql://postgres:[YOUR-PASSWORD]@db.[PROJECT-REF].supabase.co:5432/postgres"

Replace [USER], [PASSWORD], [HOST], [PORT], and [DATABASE] with your provider’s values. Prisma uses this URL for migrations and at runtime.

3. Database scripts (package.json)

With PostgreSQL you typically use migrations (migrate) instead of db push (common with MongoDB). Example scripts:

json
{
  "scripts": {
    "postinstall": "prisma generate",
    "db:migrate": "prisma migrate dev",
    "db:reset": "prisma migrate reset",
    "db:generate": "prisma generate",
    "db:studio": "prisma studio",
    "db:deploy": "prisma migrate deploy"
  }
}
Script / commandPurpose
postinstallprisma generateRegenerates the Prisma Client after npm install so client code matches the schema.
npm run db:migrateRuns prisma migrate dev: creates migration files and applies them in development.
npm run db:resetRuns prisma migrate reset: drops the DB, reapplies migrations, optionally seeds. Destructive—use only in dev.
npm run db:generateRegenerates the Prisma Client after schema edits (when you do not rely solely on postinstall).
npm run db:studioOpens Prisma Studio to browse and edit data.
npm run db:deployRuns prisma migrate deploy: applies pending migrations in staging/production CI or servers.

4. Typical workflow

  1. Define models — Edit prisma/schema.prisma.
  2. Create and apply migrations — Run npm run db:migrate (name the migration when prompted).
  3. Develop — Run npm run dev.
  4. Inspect data — Run npm run db:studio when you need a GUI.
  5. Deploy — Run npm run db:deploy (or your host’s equivalent) so production applies all migrations before or alongside the app release.

MongoDB vs PostgreSQL: MongoDB work often uses prisma db push. PostgreSQL work should use migrations (migrate dev / migrate deploy) so schema changes are versioned and repeatable across environments.

Built with Nexus Orbit