Supabase as a Backend: Postgres, Instant APIs, Row Level Security and the Two Keys
What Supabase gives a front end out of the box, how the app reads and writes rows with one line of JavaScript, why row level security is the thing that keeps that safe, and which API key belongs in the browser.
What you will learn
- List what a Supabase project gives an app: Postgres, instant APIs, realtime, auth, storage and edge functions
- Read and insert rows from JavaScript with supabase.from()
- Turn on row level security and write a policy with auth.uid()
- Explain why the anon key can go in the browser and the service role key never can
Before you start
- Basic SQL (SELECT, tables, rows)
- Running a Next.js app locally
Every app needs a backend: a database, user logins, a place for files, and an API in between. Building all of that by hand takes weeks. Supabase hands it to you ready to use. Think of a kitchen where the plumbing, the oven and the fridge are already fitted. You walk in and start cooking.
This is the backend behind our admin starter kit (see the Next.js starter kit). In this article you'll see what Supabase is, how the app talks to it, what keeps the data safe while it does, and how to set up a project.
What Supabase is
Supabase is open source and built on Postgres, a full SQL database. People call it an alternative to Firebase: a backend as a service, but with a proper relational database underneath. Your web app or mobile app plugs into the same backend either way.
A project gives you six things, and they all share one database and one login system:
| Part | What it does |
|---|---|
| Postgres database | Each project gets its own full database, with tables, rows and SQL |
| Instant APIs | Supabase reads your tables and builds an API for them, so you don't write the endpoints |
| Realtime | When a row changes, the app hears about it straight away, without a refresh |
| Authentication | Email and password, social logins like Google, and sessions |
| Storage | Files such as images and videos |
| Edge Functions | Your own small server functions, run by Supabase |
How the app talks to it
In JavaScript or TypeScript, you install the Supabase package and create a client. Reading data is then one line. This is the example from the lecture deck:
// 1. Instant API: talk to your DB from the app
import { createClient } from '@supabase/supabase-js'
const supabase = createClient(URL, KEY)
// Read rows, no backend code to write
const { data } = await supabase.from('messages').select('*')
// SELECT * FROM messages
// Insert a row just as easily
await supabase.from('messages').insert({ text: 'Hello!' })
The request goes to the instant API, the API asks Postgres, and the rows come back. Realtime is a channel: say which table you care about, and subscribe.
// 2. Realtime: get live updates automatically
supabase.channel('room-1')
.on('postgres_changes',
{ event: 'INSERT', schema: 'public', table: 'messages' },
payload => {
// fires the instant anyone inserts
render(payload.new)
})
.subscribe() // now you're live
Now every insert into that table is pushed to every open copy of the app. You don't have to memorise this syntax. An AI assistant can write it, as long as you know what each part does.
But notice one thing. The app talks to the database directly. So what stops one user from reading another user's data? Hold that question. We'll answer it after the tour.
Create an organisation and a project
Go to supabase.com and click Start your project. Sign up if you don't have an account, and the dashboard opens.
- Organisation. A group that holds your projects. Give it a name. Type: personal. Plan: free, zero dollars a month, which is enough for the whole starter kit.
- Project name. Ours is
studyeasy. - Database password. The password of your Postgres database. Use the generate link if you like, and save it somewhere safe. You'll need it if you ever connect to the database directly.
- Region. Where the database sits. Pick the one closest to your users, since a database far away makes every request slower. Don't leave it empty: Supabase stops with "a region must be defined".
- Security options. Enable Data API builds the instant API, so keep it ticked. Automatically expose new tables makes every new table reachable through that API, and Supabase itself suggests turning it off so you decide which tables are open. Enable automatic RLS switches row level security on for every new table.
Click Create new project. It takes a minute or two, then the overview shows the status as healthy, with the project URL under the name. That URL is the address of your API, and the app will need it.
A quick tour of the dashboard
- Table editor shows tables like a spreadsheet. SQL editor runs SQL directly.
- Database lists tables, and further down, Policies, where each table's security rules live. A fresh project says "no tables in schema".
- Authentication holds your users, and under Sign in / Providers, the login methods. Email and password is at the top. Social logins like Google are below it, all disabled for now. Supabase also offers captcha, rate limits, email verification and multi-factor authentication to keep bots out.
- Storage, Edge Functions and Realtime for the parts in the table above.
- Project settings, at the bottom, holds the API keys. The Connect button at the top opens the ways in: a framework library, a connection string, an ORM, an MCP for your agent, and the keys.
Row level security: a bouncer for every row
Back to the question. The answer is row level security, or RLS. Every row has an owner. With RLS on, Postgres checks each row against a rule before it returns it, like a bouncer at the door who checks every guest against the list.
The rule is called a policy, and it's a small piece of SQL. From the lecture deck:
-- 1. Turn the bouncer on for this table
alter table profiles enable row level security;
-- now NOTHING is readable until a
-- policy explicitly allows it
-- 2. Allow reading only your own row
create policy "Read own profile"
on profiles for select
using ( auth.uid() = user_id );
-- auth.uid() = the logged-in user
-- 3. Allow updating only your own row
create policy "Update own profile"
on profiles for update
using ( auth.uid() = user_id );
-- Bob can't edit Alice's profile
In plain words: you can read or update a row only when its user_id is your own id. And the nice part is that the app code doesn't change. It's still supabase.from('profiles').select('*'). Postgres filters the rows for you.
The golden rule: always enable RLS on every table your app reads. Without it, the public key can read the whole table.
Two keys, two places
Open Project settings → API keys. There are two tabs. The first has the newer publishable and secret keys. The second has the classic pair, anon and service_role. The newer keys do the same jobs: publishable does the anon key's job, secret does the service role key's job.
- The anon key is public. As the page says, it's safe in a browser, but only when RLS is on and the policies are in place.
- The service role key bypasses RLS completely. It never goes into front-end code and you never share it. If it ever leaks, generate a new one straight away.
Common mistakes
- A table with RLS off. Anyone with your public key can read all of it.
- The service role key in front-end code. It skips every policy you wrote.
- Leaving the region empty. Project creation fails until you pick one.
- Auto-exposing every new table. Turn that option off and open tables on purpose.
Quick recap
| Idea | In one line |
|---|---|
| Supabase | Open source backend on Postgres: database, instant APIs, realtime, auth, storage, edge functions |
| Reading data | supabase.from('messages').select('*') |
| RLS | Postgres checks every row against a policy before returning it |
| Policy | using ( auth.uid() = user_id ) |
| anon / publishable key | Browser, safe only with RLS on |
| service role / secret key | Server only, bypasses RLS |
Next, we connect the starter kit to this project and give it real sign-up and login: email login with Supabase in Next.js.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
Server State With TanStack Query: One Key, One Request, One Cache
Three components need the same list of books. Fetch it by hand and you get three requests; ask TanStack Query for one key and you get one. With real request counts, retries and a quiet refetch on tab focus.
Mutations and Optimistic Updates in TanStack Query: Change Data, Then Tell the Cache
Add a book with useMutation and let invalidateQueries refetch the list, then make delete instant: snapshot, remove the row in 3 ms, roll back on a 404 and refetch the truth in onSettled.
Generate TypeScript Types, Query Hooks and Zod From an OpenAPI Spec With Orval
Let the API's OpenAPI spec write your React client: Orval turns /v3/api-docs into TypeScript types, useGetBooks and a Zod schema, and a renamed field becomes error TS2339 at BookList.tsx line 26.
A Mock Backend in the Browser With Mock Service Worker, Generated From the Spec
Build the React screen before the API exists: Orval writes MSW handlers and faker data from the spec, a service worker answers GET /api/books inside the browser, and npm run dev:real switches back to the real server.
