Skip to main content

Schema Management

This guide covers managing your database schema beyond basic migrations, including Row Level Security (RLS), functions, triggers, and type safety.

Table Design Principles

Naming Conventions

Standard Table Structure

Every table should include these common patterns:

Row Level Security (RLS)

RLS Strategy

RLS is your first line of defense. Every table should have RLS enabled with appropriate policies.

Common RLS Patterns

Testing RLS Policies

Always test your policies in the Supabase dashboard:
1

Navigate to Auth Policies

Go to Database → Tables → Select table → Policies
2

Use Policy Editor

Click “New policy” and use the template editor
3

Test with Different Users

Use “Test policy” with different user contexts
4

Check SQL Directly

Database Functions

Utility Functions

Business Logic Functions

Triggers

Common Trigger Patterns

Function for Triggers

Type Safety with TypeScript

Generate Types from Database

Using Generated Types

Views and Materialized Views

Creating Views for Complex Queries

Materialized Views for Performance

Performance Optimization

Indexing Strategy

Always Index Foreign Keys

Index Filter Columns

Composite Indexes

Partial Indexes

Query Performance Tips

Troubleshooting Schema Issues

Always use IF NOT EXISTS:
Use CASCADE carefully:
Check if RLS is enabled but no policies exist:
Debug with RAISE NOTICE:

Next, learn about data operations or troubleshooting common issues.