Overview
Supabase is an open-source Firebase alternative that provides PostgreSQL database, authentication, real-time subscriptions, and storage. This guide walks you through integrating Supabase into your Emergent application.What You’ll Build
By the end of this guide, you’ll have:- Supabase PostgreSQL database connected
- CRUD operations for your data
- Real-time subscriptions (optional)
- Supabase Authentication (optional)
- File storage (optional)
Prerequisites
1
Supabase Account
Create a free account at supabase.com
2
Create Project
Create a new Supabase project
3
Get Credentials
Copy your project URL and API keys
Step 1: Create Supabase Project & Get Credentials
Create Supabase Project
- Go to https://supabase.com
- Sign up or log in
- Click New Project
- Fill in project details:
- Name: Your project name
- Database Password: Strong password (save this!)
- Region: Choose closest to your users
- Click Create new project
- Wait ~2 minutes for project initialization
Get Your Credentials
- Once project is ready, go to Settings → API
- Copy these three values:
Project URL
Format:
https://xxxxx.supabase.coUse: Both frontend and backendAnon (Public) Key
Starts with:
eyJhbGci...Use: Frontend (safe to expose)Length: Very long JWT tokenService Role Key
Starts with:
eyJhbGci...Use: Backend (keep secret!)Note: Has full database accessStep 2: Request Integration from Emergent Agent
Basic Database Integration
With Authentication
With Real-Time Features
Step 3: What the Integration Agent Will Create
Backend Implementation
1. Environment Variables (/app/backend/.env)
Get All Records
Get All Records
Create Record
Create Record
Update Record
Update Record
Delete Record
Delete Record
Complex Query with Joins
Complex Query with Joins
Frontend Implementation
1. Environment Variables (/app/frontend/.env)
Fetch Data
Fetch Data
Insert Data
Insert Data
Real-Time Subscription
Real-Time Subscription
Step 4: Create Database Tables in Supabase
Method 1: Using Supabase Dashboard (Recommended)
1
Open Table Editor
In Supabase Dashboard, click Table Editor in sidebar
2
Create New Table
Click Create a new table
3
Define Table
- Name: users
- Enable Row Level Security: Check or uncheck
- Columns: Define your columns
4
Add Columns
Click Add column for each field:
- id (uuid, primary key, default: gen_random_uuid())
- name (text)
- email (text)
- created_at (timestamptz, default: now())
5
Save Table
Click Save to create the table
Method 2: Using SQL Editor
- Go to SQL Editor in Supabase Dashboard
- Create a new query
- Run this SQL:
Method 3: Ask Agent to Generate SQL
Step 5: Enable Real-Time (Optional)
Enable Real-Time for Tables
- In Supabase Dashboard, go to Database → Replication
- Find your table (e.g., “messages”)
- Toggle Real-time to enabled
- Click Save
Frontend Real-Time Subscription
Already shown in Step 3 - Frontend Implementation section.Step 6: Setup Row Level Security (RLS)
What is RLS?
Row Level Security allows you to restrict which rows users can see and modify based on policies.Enable RLS
- Go to Authentication → Policies
- Select your table
- Click Enable RLS
Create Policies
Example: Users can only see their own dataStep 7: Setup Supabase Authentication (Optional)
Enable Auth Providers
- Go to Authentication → Providers
- Enable desired providers:
- Email/Password
- GitHub
- etc.
Google OAuth Setup
1
Get Google Credentials
Create OAuth app in Google Cloud Console
2
Configure in Supabase
Add Client ID and Secret in Supabase
3
Set Redirect URL
Add redirect URL to Google OAuth app
Frontend Auth Implementation
Step 8: Test the Integration
1
Start Preview
Click Preview in Emergent
2
Test Database Operations
- Try creating a record
- Fetch and display records
- Update a record
- Delete a record
3
Check Supabase Dashboard
Verify data appears in Supabase Table Editor
4
Test Real-Time (if enabled)
- Open app in two browser windows
- Create record in one
- Should appear in other instantly
5
Test Auth (if configured)
- Try signing up
- Try logging in
- Check protected routes
Step 9: Deploy
Configure Environment Variables
In Emergent deployment settings, add: Backend:Pre-Deployment Checklist
- All tables created in Supabase
- RLS policies configured (if using)
- Environment variables set
- Auth providers configured (if using)
- Real-time enabled on required tables (if using)
- Tested in Preview mode
Common Use Cases
Use Case 1: Blog Platform
Use Case 2: Real-Time Chat
Troubleshooting
Connection Error
Connection Error
Error: “Failed to connect to Supabase”Solutions:
- Verify Supabase URL is correct
- Check API keys are correct
- Ensure project is active (not paused)
- Verify environment variables are loaded
RLS Blocking Queries
RLS Blocking Queries
Error: “Row level security policy” or empty resultsSolutions:
- Check if RLS is enabled
- Verify policies allow the operation
- Use service role key in backend (bypasses RLS)
- Test with RLS disabled temporarily
Real-Time Not Working
Real-Time Not Working
Error: Subscriptions not receiving updatesSolutions:
- Verify Real-time is enabled for table
- Check subscription code is correct
- Ensure listening for correct events
- Check browser console for errors
Best Practices
Security
- Always use RLS in production
- Never expose service role key in frontend
- Use anon key for frontend
- Validate data server-side
- Use UUID for primary keys
Performance
- Create indexes on foreign keys
- Index frequently queried columns
- Use select() to specify needed columns
- Limit query results with .limit()
- Use pagination for large datasets
Data Modeling
- Use foreign keys for relationships
- Set appropriate ON DELETE actions
- Use TIMESTAMPTZ for timestamps
- Add created_at/updated_at fields
- Normalize data appropriately
Real-Time
- Only enable for needed tables
- Clean up subscriptions on unmount
- Handle connection errors
- Consider rate limiting
- Use channels for grouping
Additional Resources
Supabase Docs
Official Supabase documentation
SQL Reference
PostgreSQL and Supabase SQL guide
RLS Guide
Row Level Security documentation
Supabase Dashboard
Manage your Supabase projects
Quick Reference
Integration Prompt
Common Queries
For Supabase questions, ask: “How do I [operation] with Supabase?” or “Show me an example of [feature] in Supabase.”