Databases for Web Apps
Where your app's data lives
Browsers forget; databases remember. Users, posts, orders and messages belong in a database on the server.
SQL vs NoSQL
| SQL (PostgreSQL, MySQL, SQLite) | NoSQL (MongoDB, Firebase, Redis) | |
|---|---|---|
| Shape | tables with a fixed schema | flexible documents or key-value |
| Relations | joins and foreign keys | embedded or duplicated data |
| Best for | most apps: users, orders, anything relational | flexible data, caching, real-time feeds |
💡 For most new web apps, PostgreSQL is a safe default. Supabase gives you a hosted PostgreSQL with an API, authentication and storage.
Core SQL
SELECT id, title FROM posts WHERE published = true ORDER BY created_at DESC LIMIT 10;
INSERT INTO posts (title, author_id) VALUES ('Hello', 7);
UPDATE posts SET title = 'Hi' WHERE id = 1;
DELETE FROM posts WHERE id = 1;
Good habits
- Always use parameters (placeholders), never string concatenation, to prevent SQL injection.
- Add indexes on columns you filter or join on often.
- Use foreign keys to keep data consistent.
- Store timestamps in UTC (
timestamptz). - Paginate big lists (
LIMIT/OFFSETor cursor-based). - ORMs and query builders (Prisma, Drizzle, Knex) add type safety and migrations.
Example
-- A tiny blog schema (PostgreSQL / Supabase)
create table authors (
id bigint generated always as identity primary key,
name text not null,
email text unique not null
);
create table posts (
id bigint generated always as identity primary key,
author_id bigint not null references authors (id) on delete cascade,
title text not null,
body text,
published boolean not null default false,
created_at timestamptz not null default now()
);
create index on posts (author_id);
-- Latest published posts with author names
select p.title, a.name as author, p.created_at
from posts p
join authors a on a.id = p.author_id
where p.published
order by p.created_at desc
limit 10;