All the parts

Build · Remember it

Database

Where does my app keep its data so it is still there tomorrow?

What it is

A database stores your app's data safely and lets you find it fast. SQL databases (Postgres, MySQL, SQLite) keep data in tables with fixed columns, like a spreadsheet, and link tables together. Document databases (MongoDB, Firestore) store flexible JSON-like objects, which is easy to start but harder when data is connected.

SQL is a set of neat spreadsheets that refer to each other. A document database is a filing cabinet of folders, where each folder can hold different things.

Add it when Users create anything you must remember: accounts, posts, orders, settings.

It can wait when The site is static, or the data is a small file you edit by hand.

1 Which one should I pick?

Answer what you can. The ranking updates as you go, and you can ignore it if you disagree.

What does your data look like?
What do you want most?
What matters most right now?

Most people start with

2 Side by side

Three pips is strong, one is weak. Pick a row to read about it below.

OptionQuick to set upCheap while smallEasy to leaveRoom to growPrice
Free tier on some hosts, then by size and use
Free tier, then per project plan
Free tier, then by cluster size
Free to use; hosted Turso has a free tier
Free tier, then pay per read, write and storage

3 Look closer, then wire it in

Managed service

Managed Postgres (Neon, RDS)

The most trusted SQL database, run and backed up by someone else.

Pick it when Your data has relationships, such as users, orders and items, and you want a safe default.

Skip it if Your data has no clear shape and changes every week, and you want zero planning.

  • Strong rules keep your data correct
  • Huge ecosystem, and your skills carry over to any host
  • Easy to move between providers
  • You must design tables and run migrations
  • Too many connections from serverless can overload it
Quick to set up
Cheap while small
Easy to leave
Room to grow

PriceFree tier on some hosts, then by size and use

How to connect it

  1. Create a database in the provider's dashboard and copy the connection string.
  2. Put it in your environment as DATABASE_URL.
  3. Create a table with a migration, not by hand in production.
  4. Use a pooled connection string when your app runs on serverless.
Put these in your .env file
  • DATABASE_URL
db.ts
import { neon } from "@neondatabase/serverless";

const sql = neon(process.env.DATABASE_URL!);

export async function notesFor(userId: string) {
  return sql`select id, title from notes where user_id = ${userId}`;
}

4 Watch it work

Press play, or step through one message at a time.

Press Play to watch it run · 9 steps

Your app never lets the browser touch the database. It asks, the database answers.

Saving and reading a noteYour app never lets the browser touch the database. It asks, the database answers.Save this noteCheck the user and the inputinsert into notes (...)Saved, id 7DoneShow my notesselect ... where user_id = meRowsThe list
  • Client
  • Service
  • Database
Text description
  1. Browser → Your app: Save this note
  2. Your app: Check the user and the input
  3. Your app → Database: insert into notes (...)
  4. Database replies to Your app: Saved, id 7
  5. Your app replies to Browser: Done
  6. Browser → Your app: Show my notes
  7. Your app → Database: select ... where user_id = me
  8. Database replies to Your app: Rows
  9. Your app replies to Browser: The list

5 Mistakes to skip

  • Building SQL by gluing user text into a string.

    Use placeholders or an ORM so input is never run as a command (SQL injection).

  • Letting the browser talk to the database with no access rules.

    Call it from your server, or turn on row-level security or security rules before launch.

  • Having no backups and testing changes on live data.

    Turn on automatic backups, and try migrations on a copy first.

See where it sits in a whole app

The stack builder draws your product with database in it and animates a request through it.

Open the stack builder

Works closely with