Connecting to a Database for AI Coders | SheCodes
Intermediate 8 min read

Connecting Your App to a Database for AI Coders

Take your app from static to dynamic. Read real data, save user input, sync across devices.

In this chapter

  • How a connection between your app and a database actually works
  • How to plug into Supabase in under 5 minutes
  • Reading data on a page (server-side and client-side)
  • Writing data when a user submits a form

How a Database Connection Works

Your app and your database live in different places. Your app runs in a browser or on Vercel. Your database runs on Supabase's servers. To talk to each other, they need three things:

URL The address of the database. Like a phone number.
API key The key that lets your app in. Stored in .env.local, never typed into your code.
Client library The little helper installed in your project that handles all the talking.

The 5-Minute Supabase Setup

Here's the fastest way to go from no database to a working connection:

  1. 1
    Create a free Supabase account at supabase.com
  2. 2
    Click "New project", give it a name, set a password (save it somewhere!)
  3. 3
    Wait ~2 minutes while Supabase provisions your database
  4. 4
    Click "Connect" at the top of your project (or Settings → API Keys) and copy two values: the Project URL and the publishable key (it starts with sb_publishable_)
  5. 5
    Paste both into your AI tool with the prompt below
HOOK SUPABASE INTO MY PROJECT

“Connect Supabase to my project. My Project URL is [paste URL] and my public key is [paste key]. Set up everything needed to start using Supabase, and keep my keys in a safe place so they're never pushed to GitHub. Confirm it's working by loading a row or two from any of my tables.”

Reading Data on a Page

Once Supabase is connected, you can pull data into any page. There are two ways to do it: on the server (faster, better SEO) and in the browser (more interactive).

Server-side (preferred)

The data is fetched before the page is sent to the user. They see the data instantly. Google can read it. Best for blog posts, product pages, anything that doesn't change every second.

Client-side

The data is fetched in the browser after the page loads. The user sees a loading state first, then the data appears. Best for live updates, user-specific data, dashboards.

ADD DATA TO A PAGE

“On my /products page, fetch all rows from the products table in Supabase and display them as cards. Use server-side rendering so the data is available on first load. Each card should show the product name, price, and image. Sort by newest first.”

Writing Data From a Form

Reading is easy. Writing (saving what users type) is where it gets interesting. Here's the typical flow:

  1. 1
    User types into a form (name, email, message)
  2. 2
    User clicks Submit
  3. 3
    Your app sends the data to Supabase using INSERT
  4. 4
    Supabase saves the row and sends back a confirmation
  5. 5
    Your app shows a success toast and clears the form
SAVE A FORM TO THE DATABASE

“On my /contact page, when the user submits the form, save the data (name, email, message) to a contact_messages table in Supabase. Show a loading state while saving, a success toast when it works, and an error toast if it fails. Clear the form after a successful save.”

Row-Level Security (RLS): The Important Part

Supabase keeps your data locked down by default. That's a feature, not a bug: it means random people can't read your database just because they know your URL. There are two locks:

  • Table access: a new table isn't reachable from your app at all until you grant access to it. Supabase made this the default in 2026. Without it, every query fails with "permission denied for table".
  • RLS (Row-Level Security): rules that decide which rows each user can see. Tables you create in Supabase's Table Editor get RLS switched on automatically. Tables your AI tool creates with SQL or migrations may not, so always ask for it.

To allow queries, you grant access to the table and write RLS policies. A policy is a rule like "authenticated users can read their own rows" or "anyone can read published posts." With RLS on and no policies, your app sees zero data.

Common RLS policies (in plain English)

  • Public read: "Anyone, even without logging in, can SELECT from this table."
  • Authenticated read: "Only logged-in users can SELECT."
  • Owner only: "A user can only SELECT/UPDATE/DELETE rows where user_id matches their auth ID."
  • Admin override: "Users in the admin role can do anything."
OPEN A NEW TABLE SAFELY

“I just created a posts table in Supabase. Grant my app access to it so it stops saying 'permission denied for table', make sure Row-Level Security is turned on, and explain in plain English who can now read and change what.”

ADD RLS POLICIES

“My posts table has security turned on but no rules set, so my app can't see anything. Add these rules: anyone can read published posts, signed-in users can create new posts, and each user can only edit or delete their own posts. Walk me through what each rule does in plain English.”

Common Gotchas

"Empty array" returned from a query when data clearly exists

RLS is blocking it. Check the table's policies in Supabase or ask AI to add a SELECT policy.

"Failed to fetch" in the browser console

Your environment variables aren't loading. Restart the dev server after editing .env.local.

"permission denied for table posts" (error 42501)

The table hasn't been opened up to your app yet. Ask your AI tool to grant access to the table and turn on RLS with the right policies. If the table is auth.users, don't query it directly; use a profiles table linked by user_id instead.

Data shows up locally but not in production

You forgot to add the env vars to Vercel. Settings → Environment Variables → paste them in.

Real World Example: Turning a static todo app into a real one

You built a todo app where tasks live in browser memory. Refresh the page and they vanish. Now you want them to persist forever, sync across devices, and stay private to each user. Connect Supabase, create a tasks table with RLS, and rewire the app to read/write from the database instead of memory.

Build this with AI

“My todo app keeps tasks only in the browser's memory, so they disappear when I refresh the page. Set up Supabase, create a tasks table (id, which user it belongs to, title, whether it's done, and when it was created), and make sure each signed-in visitor only ever sees their own tasks. Rewrite my app so tasks are loaded from Supabase when the page opens, new tasks get saved there, and checking or deleting a task updates Supabase too. Show a loading spinner while tasks are loading and a friendly message if something goes wrong.”

Prompt This

REAL-TIME SUBSCRIPTION

“In my chat app, subscribe to new messages in the messages table using Supabase Realtime. When a new message is inserted, automatically add it to the displayed message list without a page reload. Clean up the subscription when the component unmounts.”

OPTIMISTIC UPDATES

“When the user toggles a checkbox in my todo app, update the UI instantly (don't wait for the database). If the database write fails, revert the UI change and show an error toast. This makes the app feel much faster.”

PAGINATION

“My posts page shows all posts at once and it's slow with 500+ rows. Add pagination: 20 posts per page, with Previous/Next buttons. Use Supabase's range() helper to fetch only the right slice each time.”

Frequently Asked Questions

What does it mean to 'connect' an app to a database?

Your app and your database are two separate things, usually running on different computers. Connecting them means giving your app the URL and password it needs to talk to the database. Once connected, your app can read and write data.

Where does the connection password go?

In a file called .env.local on your computer. This file is never committed to git, so it stays private. Your AI tool sets this up for you and adds .env.local to .gitignore automatically.

What is RLS (Row-Level Security)?

It's a Supabase/Postgres feature that says 'this user can only see their own rows.' Without RLS, every logged-in user could read every other user's data. With RLS, the database itself enforces who can see what.

Why does my data show up in Supabase but not in my app?

Usually one of two things. Either the table was never opened up to your app (Supabase now asks you to grant access to each new table, otherwise you get 'permission denied for table'), or RLS is on but has no policies, so every query comes back empty. One prompt fixes both: 'grant my app access to this table, turn on RLS and add a policy that lets users read their own rows.'