CursorSupabaseIntermediate10 min read

How to Connect Supabase Database in Cursor

Set up a Supabase Postgres database in your Cursor project with type-safe queries, migrations, and AI-generated data layers.

Before you start

  • Cursor IDE installed
  • A Supabase account
  • A Next.js or Node.js project open in Cursor

Step by step

1

Install the Supabase client

Open Cursor's terminal and install the Supabase JS client library.

npm install @supabase/supabase-js
2

Create your environment variables

Create a .env.local file in your project root with your Supabase credentials.

NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJhbG...your-key
SUPABASE_SERVICE_ROLE_KEY=eyJhbG...your-service-key
3

Generate a typed Supabase client

Use Cursor's AI to create a properly typed client that matches your database schema.

Paste this into Cursor:

Create a lib/supabase.ts file that:
1. Creates a Supabase client using createClient from @supabase/supabase-js
2. Exports it as a named export
3. Uses environment variables for the URL and anon key
4. Add a separate createServerClient for server-side usage with the service role key
4

Generate TypeScript types from your schema

Use the Supabase CLI to auto-generate TypeScript types from your database schema.

npx supabase gen types typescript --project-id your-project-id > src/types/database.ts
5

Build your data access layer

Ask Cursor to generate functions for all your CRUD operations, using the generated types.

Paste this into Cursor:

Using the Database types from src/types/database.ts and the Supabase client from lib/supabase.ts, create a data access layer in lib/db.ts with:
1. getAllPosts() - fetches all posts ordered by created_at desc
2. getPostById(id) - fetches a single post
3. createPost(data) - inserts a new post
4. updatePost(id, data) - updates a post
5. deletePost(id) - deletes a post
All functions should be fully typed and handle errors.
6

Create a database migration

Define your schema in SQL and run it via the Supabase dashboard or CLI.

-- In Supabase SQL Editor:
CREATE TABLE posts (
  id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
  title text NOT NULL,
  content text,
  user_id uuid REFERENCES auth.users(id),
  created_at timestamptz DEFAULT now()
);

ALTER TABLE posts ENABLE ROW LEVEL SECURITY;

Common errors

TypeError: supabase.from is not a function

The Supabase client wasn't initialized correctly.

Fix: Make sure you're calling createClient with two arguments: the URL and the key.

RLS blocks all queries

Row Level Security is enabled but no policies allow access.

Fix: Create RLS policies that match your access patterns. Start with a policy allowing authenticated users to read all rows.

Types out of sync with database

Your TypeScript types don't match the actual database schema.

Fix: Re-run npx supabase gen types typescript to regenerate types after schema changes.

Related guides

Weekly Newsletter

Get next week's fix before you need it.

Join developers getting weekly vibe coding tips, error fixes, and tool updates.

Subscribe on Substack →