# Site databases

Source: https://modulify.ai/docs/data/databases

Every project can have its own SQL database, created and shaped by the AI.

A site database is a real SQL database that belongs to one project. Your published site reads and writes it at runtime, and you browse and edit the contents from the CMS tab.

It is not a spreadsheet or a proprietary content store. It has tables, columns, foreign keys, constraints and triggers.

Every site database runs on SQLite, and your site reaches it over HTTP.

## Getting a database

A site created now already has its database, so its CMS tab opens on the collections browser with no collections in it yet. A site that has never had a database shows an empty state instead, which says **No database** with the description "Ask the AI to create a database." and a single **Ask AI** button. Chatting about something else never creates one: the database only appears the first time the AI stores data.

The button drops a prompt into chat that asks for the database and, in the same breath, asks the AI to read through your codebase and say which of your existing content would be better off as CMS collections. So the reply comes back with recommendations grounded in what you have actually built, not a blank database. While that prompt is still in the chat box the button is disabled, and when the box holds something else it asks before replacing it, see [Requests from other panels](https://modulify.ai/docs/build/site-chat#requests-from-other-panels). You can also just describe what you want to store, and the AI provisions the database itself before it starts building:

```markdown
Add a blog with posts, authors and tags. Posts need a title, cover image, body and publish date.
```

The database is ready as soon as it is created, with no wait for it to start, so the AI can build tables straight away. At that point `DATABASE_URL` and `DATABASE_TOKEN` are available to your site and the CMS tab switches from the empty state to the collections browser.

A clone, a duplicate or a template clone of a site that has a database gets its own copy of that database, and a copy of a site that has none gets a new database while it is being created. [Sharing and cloning](https://modulify.ai/docs/projects/sharing-and-cloning#copying-a-database) covers what a copy takes with it.

## How the AI creates tables

The AI has direct SQL access to your database and uses it to create tables, add columns, write indexes and constraints, run migrations and insert seed data. It follows a fixed set of conventions so the CMS can read the result:

- Every table gets a generated UUID primary key, an `id` column of type `text` that the database fills in. Auto incrementing integer ids are not used.
- A human readable URL key lives in a separate `slug` column, never as the primary key.
- Relations point at the target table's `id`, so foreign key columns are `text` too.
- Timestamps are named exactly `created_at` and `updated_at`, typed `text` and holding the time as an ISO 8601 string in UTC. `created_at` is set by its default, and `updated_at` is re-stamped on every edit by a trigger created alongside the table.

The database has three column types, `text`, `integer` and `real`, so booleans are stored as `1` and `0`, and JSON, lists and dates as text.

The CMS treats `id`, `created_at` and `updated_at` as system managed, so you never edit them. An item you open shows its `id` as read only, the two timestamps are left out of the row editor, and the table shows them.

### Field metadata

Some things a column type cannot express on its own. A column that stores an image URL is just `text` as far as the database is concerned. Modulify keeps that extra information in a table called `_collection_fields`, which the AI writes to in the same migration that creates the column.

That metadata is what makes a column render as a color swatch, a rich text editor, a code editor, a file uploader, an image uploader, a multi reference picker, or one field with [language tabs](https://modulify.ai/docs/data/languages). It also marks which `integer` column is a boolean and which `text` column holds a date, JSON, a list or an image gallery, because the column type alone cannot tell them apart. `_collection_fields` is never shown as a collection of its own. See [Column types](https://modulify.ai/docs/data/column-types) for what each one looks like.

## Running SQL

SQL runs through the AI, not through a console. There is no query editor in the product: you ask for the change in chat and the AI runs the statement for you.

```markdown
Add a "featured" boolean to posts, defaulting to false, and set it true for the three newest posts.
```

The SQL tool has real limits. A single statement is capped at 20,000 characters, and a result set comes back to the AI capped at 200 rows. There is no per statement timeout, but the AI stops waiting for an answer after 30 seconds, which does not stop the statement itself, so it keeps each statement small and breaks long running work into steps. Every statement is written for SQLite.

SQL that removes or overwrites data needs the **Delete projects** permission of the person chatting, the same one clearing a collection asks for: dropping a table or a column, a `DELETE`, a `REPLACE`, and an `UPDATE` with no `WHERE` that overwrites existing values. Data the same request saves first does not count, so any member can still have chat change a field's type, fill a new field, swap two fields or move data into a new table, as long as chat does it in one step, and dropping a language's own columns when you [remove that language](https://modulify.ai/docs/data/languages#remove-a-language) or change the default works too. Without the permission chat runs nothing and says that a workspace owner or admin can do it. Chat still removes a single item for any member, the way the CMS does.

When the AI runs a statement that changes the schema, the CMS refreshes on its own, so a new collection appears in the sidebar without a reload.

## How your site reads the database

Your site reaches its database over HTTP, not over a database connection. Two encrypted project secrets are injected into your site's environment: `DATABASE_URL` holds the address of the database, and `DATABASE_TOKEN` holds the token that scopes each request to this site. There is one value of each, shared by the preview, development and production machines.

Read both from server side code. The AI typically uses `drizzle-orm` with its SQLite proxy driver and reads the values from `process.env`:

```typescript
const databaseUrl = process.env.DATABASE_URL
const databaseToken = process.env.DATABASE_TOKEN
```

Neither value is hardcoded into the codebase, and neither is ever read from the browser.

Each request runs one SQL statement. When several changes belong together, such as an order and its lines or a payment and the credits it buys, your site sends them as one batch of up to 50 statements, and either all of them are saved or none is. If any statement in the batch fails, the database answers with that error and nothing in the batch is applied:

```json
{
    "batch": [
        { "sql": "UPDATE stock SET quantity = quantity - ?1 WHERE id = ?2 AND quantity >= ?1", "params": [2, "item-1"] },
        { "sql": "INSERT INTO orders (id, item_id, quantity) VALUES (?1, ?2, ?3)", "params": ["order-1", "item-1", 2] }
    ]
}
```

### The database secrets are locked

`DATABASE_URL` and `DATABASE_TOKEN` appear in the [Secrets](https://modulify.ai/docs/data/secrets) tab, each with a **Managed** badge. Hovering the badge says "This secret is managed by Modulify and cannot be edited or removed." You can reveal and copy each value, but you cannot change or delete either one, and neither can the AI. They live and die with the database itself.

Locked means unchangeable, not hidden. The eye icon on each row works exactly as it does on any other secret, so anyone with the **Delete projects** permission can read the whole value out: the address in `DATABASE_URL` and the token in `DATABASE_TOKEN`. Treat those the way you treat any other production credential, because whoever holds them holds your data.

**Copy secrets** and **Download secrets** leave both rows out, so they never land in a copied or downloaded `.env`. See [Copy and export](https://modulify.ai/docs/data/secrets#copy-and-export).

## What you can and cannot do

You can browse every collection, page through rows, search them, create rows, edit them, duplicate them, delete them, clear a collection, export data, and toggle whether system collections are visible.

You cannot add, rename or drop a column from the CMS. You cannot create a collection by clicking a button, run your own SQL in the product, or point a project at a database of your own, since a site database is always one Modulify created for it. Every schema change goes through chat, or through `execute_sql` from a connected AI client, as described below.

On a site with [several chats](https://modulify.ai/docs/build/multiple-chats), chats change the schema one at a time. A statement from chat that drops or renames something or deletes rows without a `WHERE`, and chat emptying one collection or clearing every collection, are refused while another chat is running, and the AI asks you to run it again once the other chats finish.

The credentials are the one thing that is not withheld. They sit in `DATABASE_URL` and `DATABASE_TOKEN`, and each row in the Secrets tab reveals and copies its value like any other, as [The database secrets are locked](https://modulify.ai/docs/data/databases#the-database-secrets-are-locked) describes. What no one can do is change them.

## When the database is unavailable

If the CMS cannot load your database, for example because the database service cannot be reached, it shows **Could not load table** with "It retries on its own shortly." That does not affect your data. While the **Data** sub-tab is on screen, the CMS checks the database again every 60 seconds, and the first check that reaches it brings your collections back without a reload.

## From an AI client over MCP

`get_site_database` says whether a site has a database and whether it is running, without ever returning connection details, and `create_site_database` gives a site a database if it does not have one, which is safe to call even when it already does. Neither tool hands back the connection: a client reads `DATABASE_URL` and `DATABASE_TOKEN` only with `get_secret`, behind the `credentials:reveal` scope you tick yourself, exactly as revealing them in the Secrets tab is a deliberate act.

A connected client works through the CMS tools, or runs a statement with `execute_sql`, which sits behind its own `data:sql` scope, unticked by default, and needs the **Delete projects** permission for any statement. The database backups described below have tools of their own.

See [MCP tools](https://modulify.ai/docs/mcp/tools#database).

## Backups

Any paid plan backs up the whole database once a day, and you can take a backup yourself up to 10 times in 24 hours with **Backup**, which asks you to confirm and says how many manual backups that leaves. Each backup is a `.zip` holding `database.sql`, a plain text SQL file with every table, row, index, trigger and view, and it is kept outside your storage. Backups live under the **Database** tab of [Backups](https://modulify.ai/docs/editor/backups). A day where nothing changed, or a database with no tables, is passed over by the daily run. Backups are kept for 30 days, and the newest finished one is never removed, as [How long database backups are kept](https://modulify.ai/docs/editor/backups#how-long-database-backups-are-kept) explains.

Downloading happens in that tab only. Chat and a connected MCP client can list the backups, take one, delete one or all of them and turn daily backups on or off, with `list_database_backups`, `create_database_backup`, `delete_database_backup`, `clear_database_backups` and `set_database_backup_schedule` over MCP, but neither can download or restore one. See [Backups from chat and MCP](https://modulify.ai/docs/editor/backups#backups-from-chat-and-mcp).

There is no restore for a database backup. To get rows back, download a backup that still holds them and add the rows again from the CMS or through chat. [Backups](https://modulify.ai/docs/editor/backups#get-data-back-from-a-database-backup) walks through it.

The CMS **Settings** sub-tab has a **Backups** section with two rows. **Database backups** has a **View backups** button that opens that tab directly. **Daily backups** is a switch, on for every site until someone turns it off, and turning it off stops only the daily backup of this site's database: **Backup** still works and every backup already taken stays. On the free plan both rows carry a **Paid** badge and the switch is disabled. See [Turn daily database backups off](https://modulify.ai/docs/editor/backups#turn-daily-database-backups-off).

**Delete all database backups** under **Danger zone** in the same sub-tab removes every finished backup at once, and needs the **Delete projects** permission. Clearing collections does not delete any backup.

## Next

- [The CMS](https://modulify.ai/docs/data/cms) walks through browsing and editing your content.
- [Column types](https://modulify.ai/docs/data/column-types) lists every column type and the editor it gets.
- [Clearing and exporting](https://modulify.ai/docs/data/clearing-and-exporting) covers getting your data out and wiping it.
- [Backups](https://modulify.ai/docs/editor/backups) covers the daily database backups.