0
0
Supabasecloud~30 mins

Creating tables via dashboard in Supabase - Try It Yourself

Choose your learning style9 modes available
Creating tables via dashboard
📖 Scenario: You are building a blog application with Supabase. You need to create the database schema with a users table and a posts table with a foreign key relationship. You'll write the SQL that the dashboard generates behind the scenes.
🎯 Goal: Create two related tables with proper primary keys, constraints, foreign keys, and Row Level Security enabled.
📋 What You'll Learn
Create a users table with UUID primary key and email
Create a posts table with foreign key to users
Enable Row Level Security on both tables
Add a basic RLS policy for authenticated read access
💡 Why This Matters
🌍 Real World
Every Supabase application needs tables with proper RLS policies. The dashboard provides a visual way to create them, but understanding the generated SQL is essential for migrations and debugging.
💼 Career
Full-stack developers using Supabase must understand table creation, foreign keys, and RLS to build secure applications.
Progress0 / 4 steps
1
Create the users table
Create a table public.users with columns: id as UUID with default gen_random_uuid() as PRIMARY KEY, email as TEXT NOT NULL UNIQUE, and created_at as TIMESTAMPTZ with default now().
Supabase
Need a hint?

Use gen_random_uuid() as the default value for UUID primary keys in Supabase.

2
Create the posts table with foreign key
Create a table public.posts with columns: id as BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, user_id as UUID referencing public.users(id) with ON DELETE CASCADE, title as TEXT NOT NULL, and created_at as TIMESTAMPTZ with default now().
Supabase
Need a hint?

Use REFERENCES public.users(id) ON DELETE CASCADE to create the foreign key with cascade delete.

3
Enable Row Level Security
Enable Row Level Security on both public.users and public.posts tables using ALTER TABLE ... ENABLE ROW LEVEL SECURITY.
Supabase
Need a hint?

Use ALTER TABLE table_name ENABLE ROW LEVEL SECURITY for each table.

4
Add a read policy for authenticated users
Create a policy named "Users can read all posts" on public.posts for SELECT using the role authenticated with the check (true) to allow all authenticated users to read posts.
Supabase
Need a hint?

Use CREATE POLICY name ON table FOR SELECT TO role USING (expression).