examples/prisma-postgres/README.md
This repository provides boilerplate to quickly set up a simple Next.js CRUD application with Prisma Postgres and Prisma ORM for database operations.
Follow these steps to quickly set up the project and start using Prisma ORM with Next.js.
Run create-next-app using your preferred package manager:
# Using npm
npx create-next-app@latest --example prisma-postgres my-prisma-postgres-app
# Using yarn
yarn create next-app --example prisma-postgres my-prisma-postgres-app
# Using pnpm
pnpm create-next-app --example prisma-postgres my-prisma-postgres-app
# Using bun
bunx create-next-app --example prisma-postgres my-prisma-postgres-app
Navigate into the created app:
cd ./my-prisma-postgres-app
Install the dependencies if you haven't already:
# Using npm
npm install
# Using yarn
yarn install
# Using pnpm
pnpm install
# Using bun
bun install
Run the following command in your terminal:
npx prisma init --db
If you don't have a Prisma Data Platform account yet, or if you are not logged in, the command will prompt you to log in using one of the available authentication providers. A browser window will open so you can log in or create an account. Return to the CLI after you have completed this step.
Once logged in (or if you were already logged in), the CLI will prompt you to:
us-east-1)After successful creation, you will see output similar to the following:
<details> <summary>CLI output</summary>Let's set up your Prisma Postgres database!
? Select your region: ap-northeast-1 - Asia Pacific (Tokyo)
? Enter a project name: testing-migration
✔ Success! Your Prisma Postgres database is ready ✅
We found an existing schema.prisma file in your current project directory.
--- Database URL ---
Connect Prisma ORM to your Prisma Postgres database with this URL:
prisma+postgres://accelerate.prisma-data.net/?api_key=ey...
--- Next steps ---
Go to https://pris.ly/ppg-init for detailed instructions.
1. Install and use the Prisma Accelerate extension
Prisma Postgres requires the Prisma Accelerate extension for querying. If you haven't already installed it, install it in your project:
npm install @prisma/extension-accelerate
...and add it to your Prisma Client instance:
import { withAccelerate } from "@prisma/extension-accelerate"
const prisma = new PrismaClient().$extends(withAccelerate())
2. Apply migrations
Run the following command to create and apply a migration:
npx prisma migrate dev
3. Manage your data
View and edit your data locally by running this command:
npx prisma studio
...or online in Console:
https://console.prisma.io/{workspaceId}/{projectId}/studio
4. Send queries from your app
If you already have an existing app with Prisma ORM, you can now run it and it will send queries against your newly created Prisma Postgres instance.
5. Learn more
For more info, visit the Prisma Postgres docs: https://pris.ly/ppg-docs
Locate and copy the database URL provided in the CLI output. Then, follow the instructions in the next step to create a .env file in the project root.
.env fileYou now need to configure your database connection via an environment variable.
First, create an .env file:
touch .env
Then update the .env file by replacing the existing DATABASE_URL value with the one you previously copied. It will look similar to this:
DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=PRISMA_POSTGRES_API_KEY"
Run the following commands to set up your database and Prisma schema:
# Using npm
npx prisma migrate dev --name init
# Using yarn
yarn prisma migrate dev --name init
# Using pnpm
pnpm prisma migrate dev --name init
# Using bun
bun prisma migrate dev --name init
Add initial data to your database:
# Using npm
npx prisma db seed
# Using yarn
yarn prisma db seed
# Using pnpm
pnpm prisma db seed
# Using bun
bun prisma db seed
Start the development server:
# Using npm
npm run dev
# Using yarn
yarn dev
# Using pnpm
pnpm run dev
# Using bun
bun run dev
Once the server is running, visit http://localhost:3000 to start using the app.
The app includes the following routes:
/: Display the thee most recent posts/posts: Paginated list view of all posts/posts/new: Create a new post/users/new: Create a new user/api/posts/: Pagination logicDeploy the example using Vercel:
Explore different ways to use Prisma ORM in your project with the following resources to help you expand your knowledge and customize your workflow:
For further learning and support: