> ## Documentation Index
> Fetch the complete documentation index at: https://docs.result.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Database

> PostgREST-style queries against your Postgres tables.

```ts theme={"dark"}
const { data, error } = await backend.database
  .from("posts")
  .select()
  .order("created_at", { ascending: false });
```

Every table automatically has `id` (uuid), `created_at`, and `updated_at` -
never add those columns yourself.

## CRUD

```ts theme={"dark"}
await backend.database.from("posts").insert([{ title: "Hello" }]); // always an array
await backend.database.from("posts").update({ title: "Hi" }).eq("id", postId);
await backend.database.from("posts").delete().eq("id", postId);

// one row instead of an array
const { data: post } = await backend.database
  .from("posts").select().eq("id", postId).single();
```

## Filters and modifiers

* Filters chain: `.eq()` `.neq()` `.gt()` `.gte()` `.lt()` `.lte()` `.like()` `.ilike()` `.in()` `.is()`
* Modifiers: `.order()` `.limit()` `.range()` `.single()` `.maybeSingle()`
* Postgres functions: `backend.database.rpc("fn_name", { arg: 1 })`

## Row-level security

Tables created with RLS on (the default) and a `user_id` uuid column scope
rows to the signed-in user automatically - no policy code needed. After
[sign-in](/sdk/authentication), queries just return the right rows.

## Changing schema

Tables and migrations are CLI operations, not SDK calls:

```bash theme={"dark"}
npx @resultdev/cli db create-table posts -c "title:string:required" -c "user_id:uuid"
npx @resultdev/cli db migrate --name add-index --sql "CREATE INDEX ..."
```

See [CLI reference](/cli/reference#db).
