This example shows how to insert and retrieve data from a Supabase (Postgres) database using Next.js. It uses the App Router and SSR patterns:
- Mutation logic lives in
app/action.tsusing Next.js Server Actions. - Query logic lives in
app/queries.tsand is called from server components. - Supabase client configuration is under
lib/supabase/.
To run this example locally you need a .env.local file with your Supabase project keys:
NEXT_PUBLIC_SUPABASE_URL=your-supabase-url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-supabase-anon-keyAdd your Supabase API keys there and then start the dev server.
- Install dependencies:
pnpm install-
Create a
.env.localfile in the project root and add your Supabase keys (see above). -
Start the development server:
pnpm dev- Open the app in your browser:
http://localhost:3000
This example expects a notes table in your Supabase Postgres database. The table stores each note created from the UI:
| Column | Type | Description |
|---|---|---|
id |
uuid |
Primary key for the note |
username |
text |
Name/handle of the note author |
title |
text |
Short title for the note |
description |
text |
Main content/body of the note |
created_at |
timestamptz |
Timestamp when the note was created |
A possible SQL definition for this table is:
create table if not exists notes (
id uuid primary key default gen_random_uuid(),
username text not null,
title text not null,
description text,
created_at timestamptz not null default now()
);You can choose from one of the following two methods to use this repository:
Deploy the example using Vercel:
Execute create-next-app with npm or Yarn to bootstrap the example:
pnpm create next-app --example https://github.com/vercel/examples/tree/main/app-directory/supabase-nextjsNext, run Next.js in development mode:
pnpm devDeploy it to the cloud with Vercel (Documentation).