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
ObjectId
toInt @id @default(autoincrement())
. - Relations: Foreign keys (userId
) are nowInt
and reference theUser
model'sid
. - Mapping: Used@@map
and@map
extensively 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.Text
for 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:studio
remain useful. - Remove/Deprecate:
db:push
anddb:pull:schema
are generally not recommended for PostgreSQL workflows, especially in production. Migrations provide a reliable history. postinstall
: Updated to only runprisma generate
. Thefumadocs-mdx
part 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 generate
automatically 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.prisma
to the database state recorded by previous migrations. - Generates a new SQL migration file in
prisma/migrations
reflecting 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.prisma
with 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/migrations
folder 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.prisma
to match. - Generally not recommended when using migrations, as
schema.prisma
should 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.prisma
file 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:migrate
to generate and apply a migration file. - (Optional) Seed Data: Run
npm run db:seed
if 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:deploy
script) in your deployment process to apply migrations to production/staging databases.