# Postgres database

A project can have its own Postgres database on BitHut. This page explains when you need one, how to add it and how to move a database you already have.

## Do you need one

A project that keeps a SQLite file in `/data` needs nothing more: SQLite is a database in a single file, and the [project disk](/docs/data/disk) keeps it. Add Postgres when the code is written for Postgres, or when you move an app that already uses one.

## Adding a database

Ask your AI: "Add a Postgres database to my project". BitHut then:

1. Starts a Postgres 17 database next to the project. Only this project can reach it. It has pgvector, so the code can run `CREATE EXTENSION vector` and keep embeddings for AI search. A database added before December 2026 does not have it.
2. Saves its address as the secret `DATABASE_URL` of the project. A secret is a value the project reads when it starts and that never appears in the chat.
3. Restarts the project with the address, if it is already running. Otherwise the project gets it on its next deploy.

The code reads `DATABASE_URL` and connects. If the code uses another variable name, your AI points it at the same address.

A database can be added before the first deploy. The cabinet shows "Database: Postgres" in the project's details.

## Memory and disk

The database runs on your plan, like the project. When you add one, BitHut checks that 256 MB more memory and the database's disk fit in your plan. If they do not, it says so and names the plan that has room. See [Plans and billing](/docs/billing).

## Copying a database from another service

A Postgres database at Railway, Render, Heroku, Supabase, Neon or on your own server can be copied into the project's database. The old address never goes through the chat.

1. Put the old database's address in a file on your computer, as a line `DATABASE_URL=postgresql://...`. Usually it is already in the project's `.env` file.
2. Pause the old app, so nothing writes to the old database during the copy.
3. Ask your AI to copy the database to BitHut. The AI gets a one-time command and runs it in the folder with the file. BitHut reads only that one line.
4. BitHut connects to the old database from its side, copies it and answers with the number of tables and rows.

What to know:

- The old database is only read; it does not change.
- The old database must be reachable from the internet. Use the provider's external or public address, not the internal one.
- If the project has no database yet, BitHut adds one first.
- Before the copy, BitHut saves a restore point of the project's database. Tables with the same names are replaced.
- By default the `public` schema is copied. Your AI can name other schemas.
- Some statements can fail, for example an extension or a role that exists only at the old provider. The result lists them. Often they do not matter; check that the tables your app needs are there.

## Moving the data of a Lovable project

A site made in Lovable keeps its data in Lovable Cloud. After the move to BitHut the site keeps working with that data where it is. To move the data too:

1. In Lovable open More, Database, Manage, Settings and press Export data. Lovable sends an email when the export is ready. Your AI can do this in your browser; you only sign in.
2. Download the `.backup` file from More, Storage into the project's folder.
3. Ask your AI to load it into BitHut. It runs the same one-time command as above with this file.
4. BitHut takes the project's tables and their rows from the file and answers with the number of tables and rows.
5. Your AI changes the site so it reads and writes this database through its own server. Until then the site still uses the data in Lovable: every new deploy lists the places in the code that still do.

What to know:

- Lovable Cloud gives no address of its database, so the copy goes through this file. Lovable allows one export a day. On Lovable's free plan where Export data is unavailable, SQL editor (`More → Database → Manage → SQL editor`) can export tables as CSV or JSON.
- Access rules of the old database (who may read which rows) stay behind. On BitHut only the project itself reaches its database, so these rules move into the project's code.
- Sign-in of your site's users, uploaded files and Lovable's server functions keep working in Lovable until you choose to move them too. Your AI asks you before moving them.
- The file can be up to 2 GB.
- Tables with vector columns (pgvector, used for AI search) load too: databases added on BitHut since December 2026 have pgvector.

### Leaving Lovable completely

If you want to close Lovable and run everything on BitHut, your AI can move the remaining parts one by one:

- **Sign-in (email + password, Google):** your AI passes `include_auth=true` when loading the `.backup` file. BitHut loads `auth.users` and `auth.identities` into a temporary `supabase_auth` schema without triggers or policies. Your AI sets up Better Auth in the project with bcrypt password verification (matching Supabase's password hashes), migrates the rows, tests sign-in with an existing password, and drops `supabase_auth`. Everyone signed in signs in again once, with the same password. If your site uses Google sign-in, create your own Google OAuth app in Google Cloud Console (your AI can guide you through it) and add its client ID and secret via a BitHut link; existing Google users match automatically by their Google ID.
- **Files (Storage):** your AI passes `include_storage=true` to load `storage.buckets` and `storage.objects` into `supabase_storage` as a checklist. Download the buckets from Lovable (`More → Storage`), upload them into `/data/storage` with `get_data_upload_command`, serve them from a route in your app with access checks, and drop `supabase_storage`.
- **Edge functions, secrets and schedules:** your AI moves files from `supabase/functions/` into server routes inside your app, updates external webhook addresses (such as Stripe), moves any `pg_cron` jobs into a BitHut scheduled project, and gives you links to paste replacement secrets (Lovable does not reveal saved secret values; `LOVABLE_API_KEY` is replaced with your own AI provider key).
- **Closing Lovable:** every deploy checks what still calls Supabase. Once no calls remain, BitHut reports that the project is ready (`lovable_ready`). Check the live site on BitHut first (sign-in, data, files, payments), and only then remove the database in Lovable (`More → Database → Manage → Settings → Remove database`) and delete the project there.

## Copies of the database

The database is saved before every new version, before every copy into it and every night. See [Backups and restore](/docs/data/backups).

For your AI: `add_database`, `get_database_import_command`.
