Skip to main content

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.
Supabase is perfect when you need a powerful relational database with real-time capabilities and built-in authentication.

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

  1. Go to https://supabase.com
  2. Sign up or log in
  3. Click New Project
  4. Fill in project details:
    • Name: Your project name
    • Database Password: Strong password (save this!)
    • Region: Choose closest to your users
  5. Click Create new project
  6. Wait ~2 minutes for project initialization

Get Your Credentials

  1. Once project is ready, go to SettingsAPI
  2. Copy these three values:

Project URL

Format: https://xxxxx.supabase.coUse: Both frontend and backend

Anon (Public) Key

Starts with: eyJhbGci...Use: Frontend (safe to expose)Length: Very long JWT token

Service Role Key

Starts with: eyJhbGci...Use: Backend (keep secret!)Note: Has full database access
Never expose the Service Role Key in frontend! It bypasses all security rules.

Step 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)
2. Supabase Client Setup
3. API Endpoints

Frontend Implementation

1. Environment Variables (/app/frontend/.env)
2. Supabase Client Setup
3. Using Supabase in Components

Step 4: Create Database Tables in Supabase

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

  1. Go to SQL Editor in Supabase Dashboard
  2. Create a new query
  3. Run this SQL:

Method 3: Ask Agent to Generate SQL

Step 5: Enable Real-Time (Optional)

Enable Real-Time for Tables

  1. In Supabase Dashboard, go to DatabaseReplication
  2. Find your table (e.g., “messages”)
  3. Toggle Real-time to enabled
  4. Click Save

Frontend Real-Time Subscription

Already shown in Step 3 - Frontend Implementation section.

Step 6: Setup Row Level Security (RLS)

Production Security: Always enable RLS for production apps to protect your data.

What is RLS?

Row Level Security allows you to restrict which rows users can see and modify based on policies.

Enable RLS

  1. Go to AuthenticationPolicies
  2. Select your table
  3. Click Enable RLS

Create Policies

Example: Users can only see their own data
Example: Public read, authenticated write

Step 7: Setup Supabase Authentication (Optional)

Enable Auth Providers

  1. Go to AuthenticationProviders
  2. Enable desired providers:
    • Email/Password
    • Google
    • 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:
Frontend:

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

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
Ask agent:
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
Ask agent:
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
Ask agent:

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.”