# Database

## Set up a project

In the root of your project, run:

```bash
fakebase init
```

This creates `fakebase.toml`, a `migrations/` folder, and a `.gitignore` entry for Fakebase's local files. Running it again is safe: an existing `fakebase.toml` is never overwritten.

Add `--start` to also start the database. In a terminal, `init` asks for anything it needs. With `--yes` it uses the defaults: the folder name as the project name, PostgreSQL 17 in Docker, and a free port.

## fakebase.toml

`fakebase.toml` describes the project. It never holds credentials, so it's safe to commit.

```toml
version = 1

[project]
name = "shop"

[migrations]
dir = "migrations"

[database]
postgres_version = 17
provider = "docker"
port = 54321
```

| Setting            | Meaning                                                               |
| ------------------ | --------------------------------------------------------------------- |
| `name`             | The project name                                                      |
| `dir`              | Where the migration files live, relative to the project               |
| `postgres_version` | The PostgreSQL major version                                          |
| `provider`         | `docker` if Fakebase runs the database, `external` if you provide one |
| `port`             | The port the Docker database listens on                               |

## Start and stop

```bash
fakebase db start
```

Starts PostgreSQL in Docker. The first start creates the container and downloads PostgreSQL, so it can take a minute. The data is kept in a Docker volume.

```bash
fakebase db stop
```

Stops the database. Your data is kept for the next start.

```bash
fakebase db status
```

Shows whether the database is running, its port and PostgreSQL version, and which database the migrate commands connect to.

## Connect your app

```bash
fakebase db env
```

Writes `DATABASE_URL` to `.env.local` and makes sure git ignores the file. The connection URL is never printed. Use `--file` and `--var` to write a different file or variable name.

## See the schema

```bash
fakebase db inspect
```

Shows the live schema: tables, columns, keys, constraints, indexes, enums, views, functions, triggers, and row-level security policies. Use `--table` to show a single table.

To see the schema in Fakebase Studio, the desktop app, instead:

```bash
fakebase studio
```

If Studio isn't installed, the command tells you where to download it.

## Use your own database

To use a PostgreSQL database you already run instead of Docker, set the provider to `external`:

```bash
fakebase init --provider external
```

Then give Fakebase the connection URL in `FAKEBASE_DATABASE_URL`, or with `--database-url` on any command. Fakebase never reads your app's `DATABASE_URL`. Fakebase warns you when the database isn't on your machine.

## What to commit

| File                                      | Commit | What it is                                |
| ----------------------------------------- | ------ | ----------------------------------------- |
| `fakebase.toml`                           | Yes    | The project settings                      |
| `migrations/`                             | Yes    | Your schema, as migration files           |
| Agent files from `fakebase agent install` | Yes    | Instructions for your coding agents       |
| `.fakebase/`                              | No     | Local connection details for the database |
| `.env.local`                              | No     | Your app's `DATABASE_URL`                 |

`fakebase init` and `fakebase db env` add the files that stay local to `.gitignore` for you.

Add `--help` to any command for its flags.

---

## Next steps

- [Migrations](https://fakebase.studio/docs/cli-migrations.md): Create, check, and apply plain-SQL migrations with the Fakebase CLI.
- [Overview](https://fakebase.studio/docs/cli.md): The Fakebase CLI runs a local PostgreSQL database for your project and manages its schema with plain-SQL migrations.
