Database


This template uses Supabase as a database and authentication provider. Here's a quick introduction if you've never heard of it.

5-Min Walkthrough


Setup

1. Create a Supabase project

Sign up for a Supabase account and create a new organization and project.

You'll be shown a project ID and anon key for the new project. Copy these over to your .env file.

Next, go to your project page > Settings > API > Project API Keys, and copy the service role key over to your .env file. Your environment variables should look like this:

...

# Make sure to add all three!
NEXT_PUBLIC_SUPABASE_URL=https://your-project-ref.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=eyxxxxxx
NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY=eyxxxxxx

...

2. Run migrations on your database

The template comes out-of-the-box with a bunch of database migrations – SQL queries that will set up the tables for your database.

Running these migrations will help you get set up for all the other features in the template, including authentication, Stripe payments, and image generation.

To run the migrations, run these three commands in your shell:

# To verify you have Supabase CLI installed
# If you don't, run `npm i supabase --save-dev` first
npx supabase --version
 
# To link your project to the Supabase CLI
# For more info, see https://supabase.com/docs/reference/cli/supabase-link
npx supabase link --project-ref <your_project_ref>
 
# Push migrations to your Supabase database
# For more info, see https://supabase.com/docs/reference/cli/supabase-db-push
npx supabase db push

Note that the project ref will be the id-like part of the Supabase URL you pasted in Step 1, ignoring the "https://" and ".supabase.co".

That's it! Visit the Supabase Dashboard > Your Project > Tables to ensure the new tables have been added to the database.

The migration files handle two other jobs:

  • Adding storage buckets under Your Project > Storage, so you can store files like user avatars and generated images
  • Setting up pgvector and vector search functions, so you can store pieces of texts with vector embeddings and perform Retrieval Augmented Generation (RAG). (Think: features like "chatting with PDFs".)

3. Set up authentication

The last thing you'll want to do while on the Supabase dashabord is to configure authentication. Check out the Authentication tutorial to set up Magic Links and Google OAuth with Supabase.

Running Supabase locally

You can run Supabase locally for development to keep your production and development databases separate. Follow the official Supabase Local Development guide to learn more.

If you're running Supabase locally, browse through the supabase/config.toml file to set up configurations for the local database. To enable Google Auth locally, switch enabled = true on Line 134 (it is turned off by default). Make sure to set your project_id correctly as well.


Last Updated: June 7