Supabase Postgres Table: Create & Secure It
In this article
This guide walks you through creating your first table in Supabase (a hosted Postgres database) and, more importantly, locking it down with Row Level Security (RLS). By the end, you’ll have a table that only your app’s authenticated users can read or write, which is the default best practice for any production app. You’ll need a Supabase account (free tier works) and a new or existing project.
What You'll Need
- A Supabase account (sign up at supabase.com)
- A Supabase project (create one in the dashboard; the free tier is fine)
- Basic familiarity with SQL and the concept of primary keys
- Optional: a REST client like Postman or cURL, but the dashboard SQL editor is enough
Step-by-Step
- Log in to the Supabase Dashboard and open your project. If you haven’t created a project yet, click “New project,” choose a name, set a database password, and select a region close to your users. Wait for the provisioning to finish (a few minutes).
- Open the SQL Editor. In the left sidebar, click “SQL Editor.” This is where you’ll run SQL commands against your database. Alternatively, you can use the “Table Editor” (a visual interface) but SQL is clearer for learning and gives you full control.
- Create a table with a primary key. Paste the following SQL into the editor and click “Run”:
create table public.profiles ( id uuid primary key default gen_random_uuid(), email text unique not null, full_name text, created_at timestamptz default now() );
This creates a table calledprofileswith a UUID primary key, an email column (unique and required), an optional name, and a timestamp. Thegen_random_uuid()function generates a random UUID automatically – you don’t need to supply it. - Verify the table exists. In the SQL Editor, run
select * from public.profiles;– you should see an empty result set. You can also see the table under “Table Editor” in the sidebar. - Enable Row Level Security (RLS). By default, RLS is disabled on new tables, meaning anyone with the anon key (the public API key) can read and write your data. That’s dangerous. Run:
alter table public.profiles enable row level security;
Now, without any policies, no one (even authenticated users) can access the table. That’s a safe starting point. - Create a basic SELECT policy. You want authenticated users to see their own profile. Run:
create policy "select own profile" on public.profiles for select using (auth.uid() = id);
This policy allows a user to select rows where theidcolumn equals their authenticated user ID (auth.uid()). - Create an INSERT policy. Allow users to insert their own profile. Run:
create policy "insert own profile" on public.profiles for insert with check (auth.uid() = id);
Thewith checkclause ensures the row being inserted has anidmatching the authenticated user. - Create UPDATE and DELETE policies (optional but recommended). Run:
create policy "update own profile" on public.profiles for update using (auth.uid() = id) with check (auth.uid() = id);create policy "delete own profile" on public.profiles for delete using (auth.uid() = id);
Now users can modify or remove only their own row. - Test the policies. In the dashboard, go to “Authentication” → “Users” and click “Add user” to create a test user (email/password). Then, in the “Table Editor,” you can’t see rows because the editor uses the anon key (RLS blocks it). To test properly, use the “API” section or a REST client: send a request with the user’s JWT in the
Authorizationheader. For example, using cURL:curl -X POST https://YOURPROJECT.supabase.co/auth/v1/token?grant_type=password \ -H "apikey: YOUR_ANON_KEY" \ -d '{"email":"test@example.com","password":"yourpassword"}'
Get theaccess_tokenfrom the response, then callGET /rest/v1/profileswith that token. You should see your test user’s row (if you inserted one) and no one else’s. - Secure the API keys. In the dashboard, under “Settings” → “API,” you’ll see the anon and service_role keys. The anon key is meant to be public (it’s in your frontend), but the service_role key bypasses RLS – never expose it in client-side code. Keep it in server-side environments only.
Common Mistakes
- Forgetting to enable RLS. If you skip step 5, your table is wide open. Always enable RLS before adding any data.
- Using the service_role key in the frontend. This key ignores RLS. If it leaks, anyone can read/write all data. Use the anon key in your client.
- Not using
auth.uid()correctly. Theidcolumn in your table must match the user’s ID from Supabase Auth. If you use a different column (likeuser_id), adjust the policy accordingly. - Creating policies that are too broad. For example, a SELECT policy with
using (true)lets everyone see all rows. Only use that for genuinely public data (like a blog). - Assuming the Table Editor shows RLS results. The dashboard’s Table Editor uses the anon key, so with RLS on, you’ll see zero rows. That’s expected – test via API instead.
Tips
- Use UUIDs instead of auto-increment integers. UUIDs are more secure (no enumeration) and work well with Supabase Auth’s UUID user IDs.
- Add a trigger to sync user metadata. When a user signs up, you often want a row in
profilesautomatically. Create a trigger onauth.usersthat inserts a new profile with the user’s ID and email. - Use the “RLS policy” helper in the dashboard. In the Table Editor, click “RLS policies” and then “New policy” – the UI generates SQL for you. It’s a great way to learn.
- Test with multiple users. Create two test users and ensure they can only see their own data. This is the fastest way to validate your policies.
- Keep policies small and specific. One policy per operation (select, insert, etc.) is easier to debug than a single complex policy.
Troubleshooting
- “Permission denied for table profiles” – This usually means RLS is enabled but no policy matches. Check that you’re authenticated (send the JWT) and that the policy uses the correct column. Run
select * from pg_policies where tablename = 'profiles';to list policies. - You can see all rows when you shouldn’t. Your SELECT policy might be too broad. Review the
usingcondition. Also, check if you’re using the service_role key accidentally – that bypasses RLS. - Insert fails with “new row violates row-level security policy.” The
with checkcondition is false. Ensure theidyou’re inserting equalsauth.uid(). If your table uses a different column for the user, adjust the policy. - Can’t see the table in Table Editor. That’s normal with RLS on. Use the SQL Editor or API to verify. You can temporarily disable RLS to view data, but remember to re-enable it.
- Auth.uid() is null. This happens when the request isn’t authenticated. Make sure you’re sending the
Authorization: Bearer <access_token>header. In the dashboard’s API testing, you may need to add the JWT manually.
Frequently Asked Questions
What is Row Level Security (RLS)?
RLS is a Postgres feature that restricts which rows a user can see or modify based on a policy. In Supabase, policies can use auth.uid() to match the logged-in user.
Do I need to enable RLS on every table?
Yes, unless the table is intentionally public. Even if you don’t have policies yet, enabling RLS blocks all access until you add policies – a safe default.
What’s the difference between the anon key and the service_role key?
The anon key is safe to expose in your frontend; it works with RLS. The service_role key bypasses RLS and grants full access – keep it secret, server-side only.
Can I use a regular integer primary key instead of UUID?
Yes, but UUIDs are recommended because they’re globally unique and don’t leak row counts. If you use an integer, you’ll need to map it to the auth user ID manually.
How do I automatically create a profile row on signup?
Create a trigger on auth.users after insert, which inserts into public.profiles with the new user’s ID and email. Supabase provides a template in the SQL editor under “Get started” → “User Management.”
Why can’t I see my data in the Table Editor?
The Table Editor uses the anon key, which respects RLS. If you’re not logged in as that user, you won’t see rows. Use the API with a user JWT to verify.