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

# Database

> Choose and configure the right database backend for your deployment

FIM One supports two database backends: **SQLite** (default, zero-config) and **PostgreSQL** (recommended for production). The backend is determined by the `DATABASE_URL` environment variable.

```bash theme={null}
# SQLite (default — no configuration needed)
DATABASE_URL=sqlite+aiosqlite:///./data/fim_one.db

# PostgreSQL (production)
DATABASE_URL=postgresql+asyncpg://user:pass@localhost:5432/fim_one
```

`start.sh` and the Docker entrypoint run the Alembic migrations on every start, so tables are created automatically — no manual migration step is required.

***

## SQLite vs PostgreSQL

| | SQLite | PostgreSQL |
| - | - | - |
| **Setup** | Keine Konfiguration erforderlich, dateibasiert | Erfordert einen separaten Server |
| **Concurrency** | Single-Writer (globale Sperre) | Vollständiges MVCC, Sperren auf Zeilenebene |
| **Multi-Worker** | Nicht unterstützt (`WORKERS` muss `1` sein) | Vollständig unterstützt |
| **SSE Streaming** | Verbindungen während Streams können andere Anfragen blockieren | Gleichzeitige Lese- und Schreibvorgänge sind nicht betroffen |
| **Backup** | `.db`-Datei kopieren | `pg_dump` oder Streaming-Replikation |
| **Am besten geeignet für** | Entwicklung, Einzelnutzer, Demos | Produktion, mehrere Benutzer, Teams |

> [!TIP]
>
> **Lokale Entwicklung:** SQLite funktioniert sofort – kein Datenbankserver, kein Redis, nichts zu konfigurieren. Einfach mit dem Programmieren beginnen.
>
> **Produktion:** Mit Docker Compose bereitstellen und PostgreSQL + Redis werden automatisch bereitgestellt. Keine manuelle Datenbankeinrichtung erforderlich.

***

## Known Limitation: SQLite Concurrent Streaming

> [!WARNING]
>
> **SQLite can become a bottleneck under concurrent load.**
>
> FIM One uses Server-Sent Events (SSE) for streaming AI responses. During streaming, each active SSE connection holds a database connection from the pool for the duration of the stream. SQLite enforces a global write lock, meaning only one write operation can proceed at a time — all other writes queue behind it.
>
> Even though the connection pool supports up to 15 simultaneous connections (`pool_size=10` + `max_overflow=5`), the bottleneck is the lock itself, not the pool. When multiple users are chatting simultaneously, their write operations (saving messages, updating token counts) serialize against each other.
>
> **Symptoms you may observe:**
>
> * Conversation list loads slowly while another user is streaming
> * Settings pages feel sluggish during active chat sessions
> * API responses are delayed when multiple streams are active
>
> **Recommendation:** If you have more than 2-3 concurrent users, switch to PostgreSQL. PostgreSQL uses MVCC (Multi-Version Concurrency Control) with row-level locking, so concurrent reads and writes proceed independently without blocking each other.

***

## Connection Pool Configuration

FIM One configures SQLAlchemy connection pool settings internally for each backend. These are tuned defaults that do not require environment variables (the SQLite pool can be resized with `SQLITE_POOL_SIZE` / `SQLITE_MAX_OVERFLOW`) — they are applied automatically based on the `DATABASE_URL` scheme. Understanding them helps explain runtime behavior.

### SQLite Pool Settings

| Setting | Value | Description |
| - | - | - |
| `pool_size` | `10` | Base number of persistent connections in the pool |
| `max_overflow` | `5` | Additional connections created under load (up to 15 total) |
| WAL journal mode | Enabled | Write-Ahead Logging allows concurrent reads while a write is in progress, significantly reducing lock contention |
| `busy_timeout` | `30s` | When the write lock is held, other writers wait up to 30 seconds before raising an error, instead of failing immediately |
| `synchronous` | `NORMAL` | Safe with WAL mode; provides better write throughput than the default `FULL` |

WAL mode and the 30-second busy timeout are set via SQLite PRAGMAs on every new connection. This combination ensures that short-lived reads (loading a conversation list, fetching settings) are not blocked by long-running write transactions, and that concurrent writes queue gracefully instead of failing.

### PostgreSQL-Pool-Einstellungen

| Einstellung | Wert | Beschreibung |
| - | - | - |
| `pool_size` | `10` | Basiszahl persistenter Verbindungen im Pool |
| `max_overflow` | `20` | Zusätzliche Verbindungen, die unter Last erstellt werden (bis zu 30 insgesamt) |
| `pool_timeout` | `30s` | Maximale Wartezeit für eine freie Verbindung aus dem Pool, bevor ein Timeout-Fehler ausgelöst wird |
| `pool_recycle` | `1800s` | Verbindungen werden alle 30 Minuten recycelt, um veraltete Verbindungen zu vermeiden (wichtig für Cloud-gehostete Datenbanken, die untätige Verbindungen schließen) |

PostgreSQL verwaltet Parallelität nativ über MVCC, daher steuern die Pool-Einstellungen hauptsächlich die Ressourcennutzung statt Konflikte. Das 30-Minuten-Recycling-Intervall vermeidet Probleme mit Firewalls, Load Balancern oder verwalteten Datenbankdiensten, die untätige TCP-Verbindungen stillschweigend trennen.

***

## Wechsel zu PostgreSQL

### Schritt 1: Starten Sie eine PostgreSQL-Instanz

Der schnellste Weg ist mit Docker:

```bash theme={null}
docker run -d \
  --name postgres \
  -e POSTGRES_PASSWORD=secret \
  -e POSTGRES_DB=fim_one \
  -p 5432:5432 \
  postgres:16-alpine
```

### Schritt 2: DATABASE\_URL festlegen

Fügen Sie die folgende Zeile in Ihrer `.env`-Datei hinzu oder aktualisieren Sie sie:

```bash theme={null}
DATABASE_URL=postgresql+asyncpg://postgres:secret@localhost:5432/fim_one
```

### Schritt 3: FIM One neu starten

```bash theme={null}
# Local development
./start.sh portal

# Or restart your process manager / systemd service
```

Tabellen werden beim ersten Start automatisch erstellt. Es ist keine manuelle Schema-Migration erforderlich.

> [!NOTE]
>
> **Vorhandene SQLite-Daten werden nicht automatisch migriert.** Das Wechseln von `DATABASE_URL` von SQLite zu PostgreSQL beginnt mit einer frischen Datenbank. Wenn Sie vorhandene Gespräche, Agenten oder Konnektoren in SQLite haben, die Sie beibehalten möchten, siehe den Abschnitt [Datenmigration](#data-migration) unten.

### Docker Compose (Recommended for Production)

If you deploy with Docker Compose, **PostgreSQL and Redis are already included and auto-configured** — there is nothing extra to set up. The `docker-compose.yml` sets `DATABASE_URL` internally — your `.env` value is overridden:

```yaml theme={null}
environment:
  DATABASE_URL: postgresql+asyncpg://${POSTGRES_USER:-fim}:${POSTGRES_PASSWORD:-fim}@postgres:5432/${POSTGRES_DB:-fim_one}
```

No additional database setup is needed when using Docker Compose.

***

## Datenmigration

Es gibt kein integriertes Migrationstool von SQLite zu PostgreSQL. Für die meisten Bereitstellungen hängt der empfohlene Ansatz von Ihrer Situation ab:

**Neue Bereitstellung (keine vorhandenen Daten):** Setzen Sie einfach `DATABASE_URL` auf Ihre PostgreSQL-Verbindungszeichenfolge und starten Sie FIM One. Alle Tabellen werden automatisch erstellt.

**Vorhandene Daten, die beibehalten werden müssen:** Manuelle Export/Import ist erforderlich. Der allgemeine Ansatz:

1. Exportieren Sie Daten aus SQLite mit einem Tool wie `sqlite3` CLI oder einem Python-Skript
2. Transformieren Sie die Daten nach Bedarf (SQLite und PostgreSQL haben geringfügige Typunterschiede)
3. Importieren Sie in PostgreSQL mit `psql`, `pg_restore` oder Skripten auf Anwendungsebene

> [!TIP]
>
> Für die meisten Benutzer, die von einer Entwicklungsumgebung zu einer Produktionsumgebung wechseln, ist es einfacher, mit PostgreSQL neu anzufangen und Ihre Agenten und Konnektoren über die Benutzeroberfläche neu zu erstellen. Der Gesprächsverlauf ist normalerweise nicht kritisch genug, um eine manuelle Migration zu rechtfertigen.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.