# Database

Browse and edit tables, run SQL, apply migrations, restore backups, read logs, check row level security and get your connection keys.

Your project database is a managed SQL database. Manage it from **Settings > Lyna Cloud > Database**. If the project has no database yet, see [Set up a database](/features/cloud/overview#set-up-a-database).

The Database page on Tables: the Data, Operations, Security and Connection sections on the left, and five tables with their RLS state and column count.

## What you can do

- Create, edit and delete tables, columns and foreign keys.
- Add, edit, filter and delete rows.
- See how your tables relate in the Schema Visualizer.
- Run SQL, chart the results and export them.
- Start from a ready-made schema template.
- Save queries as migrations and apply them.
- Create, restore and download backups.
- Read the database logs.
- Turn row level security on or off and read each table's policies.
- Apply the advisor's performance and security fixes.
- Copy your keys and save them to `.env`.

The page groups its sections in four sets: **Data** (Tables, SQL Editor), **Operations** (Migrations, Backups, Logs), **Security** (Audit, Advisor) and **Connection** (Keys & Settings). Choose the full screen button to give the database the whole window.

## Tables

The **Tables** list shows each table with its rows, columns and RLS state. Search it, open a table with **View records**, or use the table menu to **Edit table** or **Delete table**.

1. ### Create a table

   Choose **New table**. Enter a **Table name**, then add columns with a name, a type, a default, and whether the column is the primary key (**PK**) or can be empty (**Null**).
2. ### Link it to other tables

   Under **Foreign keys**, choose **Add foreign key**, pick the column, the table and the column it points to, and what happens **On delete** and **On update** (No action, Cascade, Set null or Restrict).
3. ### Save

   Choose **Create table**. The table appears in the list.

Inside a table you get three tabs:

- **Data**: add, edit and delete rows. Use **Filter** to narrow rows with operators such as equals, greater than, like or in list. Select several rows to delete them together.
- **Columns**: each column's name, type, constraints, default and reference.
- **Policies**: the table's row level security policies, with their roles and their `USING` and `WITH CHECK` rules.

A table open on its Columns tab, with each column's name, type, constraints, default value and reference.

> [!WARNING]
>
> Deleting a table, a column or rows cannot be undone. Lyna asks you to type the table name before it deletes a table.

### Schema Visualizer

Choose **Schema Visualizer** in the Tables list to see your tables as a map, with their keys, their relationships, the sign-in users table and your storage buckets. Drag tables to move them, scroll or pinch to zoom, choose **Auto layout** to tidy the map, and double-click a table to edit it.

The Schema Visualizer with the project's tables joined by relationship lines, the Authentication node with its Users table, the minimap and the zoom controls.

## SQL Editor

Write a query and choose **Run** (`Cmd/Ctrl + Enter`).

- **Results** shows the rows. **Export** downloads them as CSV or copies them as JSON or Markdown.
- **Chart** draws a bar, line or pie chart from a numeric column.
- **Templates** loads a ready-made schema: CRM, AI chatbot, Online store, Community forum, Photo sharing or Team workspace.
- **Save as migration** saves the query as a migration file in your project.

## Migrations

Migrations are SQL files in your project's `supabase/migrations` folder. The **Migrations** section lists them with their status: **Applied**, **Pending** or **Missing file**.

- **New migration** creates an empty migration. Write the SQL, then **Save**.
- **Apply** runs a pending migration on the database. An applied migration is read-only.
- **Repair** recreates a migration file that is missing from your project. **Repair all** fixes every missing file at once.

Managing migrations needs a running sandbox, so keep the project open.

## Backups

- **Manual backups**: choose **Create backup** to save a snapshot. You can **Rename**, **Restore**, **Download** or **Delete** each one. The number of manual backups depends on your plan; the counter shows how many you have used.
- **Scheduled backups**: on Pro, Lyna takes a backup every day and keeps it for 7 days.

> [!WARNING]
>
> **Restoring overwrites your data**
>
> A restore replaces your current data with the backup. Data created after the backup is lost, and the database is briefly unavailable. Lyna first saves a **Pre-restore** backup so you can roll back.

## Logs

The **Logs** section shows the last hour of activity. Pick a **Log type** in one of five groups: Database, Connections, Data API, Edge functions, or Auth, storage and realtime. Filter by **Level**, and open an entry to read its message and fields. Observability reports can open the logs on a given time window.

## Audit

**Audit** checks row level security on every table. It shows how many tables are **Protected** or **Unprotected**, and warns when a table has RLS on but no policy (then nobody can read or write it through the API). Use **Enable RLS**, **Disable RLS** or **View policies** on each table.

## Advisor

**Advisor** lists performance and security suggestions, such as a foreign key without an index, an unused or duplicate index, a public table without row security, or a policy that trusts user metadata. Filter by **Security** or **Performance**. When a suggestion can be fixed for you, choose **Fix**, check what it will do, then **Apply fix**. You can also copy the SQL of a fix.

## Keys & Settings

- **Lyna Cloud endpoint**, **Publishable key** and **Secret key**. The endpoint shows as `<Lyna Cloud endpoint>`, and its copy button gives the full address your app needs; a database you connected from your own account shows its **Project URL** instead. The publishable key is safe in a browser and is limited by your row level security policies. The secret key bypasses row level security: keep it on the server. Only the project's owner and admins can see the secret key; for other members its row is locked.
- **Change secret key** (the project's owner, on a database Lyna runs): Lyna makes a new secret key, checks that it works, moves everything it runs to it, then turns the old secret key off for good, and any copy of it with it. Your site, its sign-ins and its publishable key keep working. Anything outside Lyna that used the old key, such as another server or a script, needs the new one: copy it from the same page. If a step fails, the old key keeps working until Lyna uses the new one, and **Try again** finishes the change. While the database's older keys, from before the upgrade, are still on, the change says so: they keep working until someone uses **Turn off old keys**. A database that still uses the older key format shows **Upgrade keys** instead.
- **Upgrade keys** (the project's owner and admins, on a database Lyna runs that still uses the older key format): Lyna creates a new publishable key and a new secret key and uses them for the database, and your site's `.env` gets the new publishable key. Nobody who is signed in to your site is signed out, and your site keeps working: the older keys stay on for now, because your published site and your edge functions still use them. If a step fails, **Try again** picks up where it stopped. Once the keys are upgraded, **Change secret key** appears, and **Turn off old keys** follows.
- **Turn off old keys** (the project's owner and admins, on a database Lyna runs whose older keys are still on): Lyna turns off the keys the database had before the upgrade, so a copy someone kept stops working. It is offered only once nothing Lyna runs uses them; until then the row lists what still does, such as an edge function that reads an older key or a site published before the upgrade, with what to do. A new Lyna Cloud database starts with its older keys off.
- **Change database password** (the project's owner and admins, on a database Lyna runs): edge functions receive a connection that includes the database's password, so anyone who could deploy one may have copied it. Lyna sets a new random password and uses it at once; you never need to type it. A database tool or another app that connects with the old password stops working.
- **Environment variables**: choose **Save to .env** to write the keys your app needs to the project's `.env`, or copy them. On Lyna Cloud the block shows the address and the project ID your `.env` holds; a database whose address is not yet a Lyna address shows `<Lyna Cloud endpoint>` in its place. **Save to .env** and the copy write the real values.
- **Danger zone**: **Disconnect** (your own Supabase project) or **Delete database**. You type `DISCONNECT` or `DELETE` to confirm.

> [!WARNING]
>
> **Never put the secret key in your app**
>
> Code in your app is downloaded by every visitor. Use the publishable key there, and use the secret key only in [edge functions](/features/cloud/edge-functions).

## FAQ

### Can the AI change my database?

Yes. Ask in the chat, for example "add a comments table linked to posts, with row level security so people only edit their own comments". Database changes can ask for your approval before they run.

### Why does my app get no rows from a table?

If row level security is on and the table has no policy, all access is denied. Open **Audit** or the table's **Policies** tab, then add a policy, or ask the AI to add one.

### Who can edit the database?

Editors, admins and the owner. Viewers can read the pages, but the keys are hidden and every control is locked.

## Related

- [Users and auth](/features/cloud/users-and-auth): Let people sign in and tie rows to them.
- [API](/features/cloud/api): Query your tables over HTTP.
- [Observability](/features/cloud/logs-and-observability): Find slow queries and errors.
- [Security best practices](/tips-and-tricks/security-best-practices): Write row level security policies.
