6. Database
Introduction
I use Prisma ORM to connect to PostgreSQL for data management.
PostgreSQL was chosen mainly because it can be used with cloud services like Supabase to start projects with zero cost.
Self-hosted Service Connection
Simply write the postgresql connection string, with environment variable name DATABASE_URL.
# prisma orm database
DATABASE_URL="postgresql://username:password@server:port/database?schema=public"
Supabase Connection
Prepare DATABASE_URL connection API Key.
- URL server address.
anonpublickey is used for client-side data access.service_rolesecretis used for server-side data creation and modification.

Prepare DIRECT_DATABASE_URL direct connection configuration, used for running Prisma synchronization functions.

.env configuration, configure two environment variables as follows.
DATABASE_URL="prisma://accelerate.prisma-data.net/?api_key=__API_KEY__"
DIRECT_DATABASE_URL="postgresql://user:password@host:port/db_name?schema=public"
prisma/schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_DATABASE_URL")
schemas = ["next_auth", "public"]
}
Sync Data
Synchronize the data table definitions in schema.prisma to the server PostgreSQL tables.
npm run prisma:migrate
Create Prisma Client
After execution, connection library files will be created in the /prisma/client directory and copied to /node_modules/@prisma/client/ location for use.
npm run prisma:generate
At this point, the Prisma configuration is complete.
end