PostgreSQL
Switching to and managing a PostgreSQL database with Prisma.
While StarterKitPro defaults to MongoDB, you can easily switch to using a PostgreSQL database with Prisma.
1. Update Prisma Schema (prisma/schema.prisma)
Modify your prisma/schema.prisma file entirely. Change the datasource provider to postgresql and update your models to use PostgreSQL-compatible types and conventions (like auto-incrementing integer IDs).
Schema Conversion Notes
- IDs: Changed from MongoDB
ObjectIdtoString @id @default(cuid()). - Relations: Foreign keys (userId) are nowStringand reference theUsermodel'sid. - Mapping: Used@@mapand@mapextensively to map camelCase model/field names to conventional snake_case table/column names in PostgreSQL (e.g.,emailVerified->email_verified,User->users). - Index Names: Provided explicit map names for unique constraints for clarity (e.g.,@@unique([provider, providerAccountId], map: "accounts_provider_provider_account_id_key")). - Data Types: Used@db.Textfor potentially long token fields.
2. Configure Database URL (.env.local)
Update the DATABASE_URL in your .env.local file with your PostgreSQL connection string.
- Replace placeholders with your actual PostgreSQL connection details.
3. Install PostgreSQL Driver
Ensure the pg driver is installed:
4. Update package.json Scripts
When switching from MongoDB to PostgreSQL, you need to adjust your database management scripts in package.json. The core change is shifting from db:push to db:migrate for schema changes.
Recommended Scripts for PostgreSQL:
Script Changes
- Added/Prioritized:
db:migrate(usingprisma migrate dev) becomes the primary way to apply schema changes. - Keep:
db:seed,db:reset(uses migrations now),db:studioremain useful. - Remove/Deprecate:
db:pushanddb:pull:schemaare generally not recommended for PostgreSQL workflows, especially in production. Migrations provide a reliable history. postinstall: Updated to only runprisma generate. Thefumadocs-mdxpart might be specific to your documentation setup and can be kept if needed elsewhere.
5. Database Management & Migrations
Managing PostgreSQL schemas with Prisma revolves around migrations.
Script Explanations:
-
npm run postinstall: Runsprisma generateautomatically afternpm install. This ensures your Prisma Client is always up-to-date with yourschema.prisma. -
npm run db:migrate(mapped toprisma migrate dev):- The primary command for development.
- Compares your
schema.prismato the database state recorded by previous migrations. - Generates a new SQL migration file in
prisma/migrationsreflecting the changes. - Applies the new migration to your development database.
- Prompts for a migration name.
-
npm run db:push(mapped toprisma db push):- Use Sparingly/Avoid with PostgreSQL.
- Directly syncs your
schema.prismawith the database, bypassing the migration history. - Can be useful for rapid prototyping before establishing a migration history, but generally discouraged for PostgreSQL.
-
npm run db:reset(mapped toprisma migrate reset):- Use with caution!
- Drops the database.
- Re-runs all existing migration files from the
prisma/migrationsfolder in order. - Optionally runs the seed script if configured.
- Essential for resetting the development database to a clean state based on the migration history.
-
npm run db:pull(mapped toprisma db pull):- Introspects the database and updates your
schema.prismato match. - Generally not recommended when using migrations, as
schema.prismashould be the source of truth driving migrations.
- Introspects the database and updates your
-
npm run db:generate(mapped toprisma generate):- Generates the Prisma Client based on your schema. This needs to be run after any changes to your
schema.prismafile to update the TypeScript types and database access methods available in your application.
- Generates the Prisma Client based on your schema. This needs to be run after any changes to your
6. Typical PostgreSQL Workflow
- Define/Modify Models: Edit
prisma/schema.prisma. - Create Migration: Run
npm run db:migrateto generate and apply a migration file. - (Optional) Seed Data: Run
npm run db:seedif needed. - Develop: Start your application (
npm run dev). - (Optional) Inspect Data: Use
npm run db:studio. - Deployment: Use
prisma migrate deploy(potentially via annpm run db:deployscript) in your deployment process to apply migrations to production/staging databases.