2026-05-16 12:48:23 +08:00
---
2026-06-16 18:04:52 +08:00
description : PostgreSQL database schema, tables, migrations, and backup procedures for SnapOtter.
2026-05-16 12:48:23 +08:00
---
2026-07-11 13:01:55 +08:00
# Database {#database}
2026-03-22 21:00:37 +08:00
2026-06-16 18:04:52 +08:00
SnapOtter uses PostgreSQL 17 with [Drizzle ORM ](https://orm.drizzle.team/ ) (pg-core / node-postgres) for data persistence. The schema is defined in `apps/api/src/db/schema.ts` .
2026-03-22 21:00:37 +08:00
2026-06-16 18:04:52 +08:00
The connection is configured via the `DATABASE_URL` environment variable (default `postgres://snapotter:snapotter@postgres:5432/snapotter` ). In Docker Compose, the Postgres container stores its data in the `SnapOtter-pgdata` named volume.
2026-03-22 21:00:37 +08:00
2026-07-11 13:01:55 +08:00
## Tables {#tables}
2026-03-22 21:00:37 +08:00
2026-07-11 13:01:55 +08:00
### users {#users}
2026-03-22 21:00:37 +08:00
Stores user accounts. Created automatically on first run from `DEFAULT_USERNAME` and `DEFAULT_PASSWORD` .
| Column | Type | Notes |
|---|---|---|
2026-06-16 18:04:52 +08:00
| `id` | uuid | Primary key |
| `username` | varchar | Unique, required |
| `passwordHash` | varchar | scrypt hash |
| `role` | varchar | `admin` , `editor` , or `user` |
| `mustChangePassword` | boolean | Forced password reset flag |
| `createdAt` | timestamp | Creation time |
| `updatedAt` | timestamp | Last update time |
2026-03-22 21:00:37 +08:00
2026-07-11 13:01:55 +08:00
### sessions {#sessions}
2026-03-22 21:00:37 +08:00
Active login sessions. Each row ties a session token to a user.
| Column | Type | Notes |
|---|---|---|
2026-06-16 18:04:52 +08:00
| `id` | varchar | Primary key (session token) |
| `userId` | uuid | Foreign key to `users.id` |
| `expiresAt` | timestamp | Expiry time |
| `createdAt` | timestamp | Creation time |
2026-03-22 21:00:37 +08:00
2026-07-11 13:01:55 +08:00
### teams {#teams}
2026-04-16 16:26:58 +08:00
Groups for organizing users. Admins can assign users to teams.
| Column | Type | Description |
|--------|------|-------------|
2026-06-16 18:04:52 +08:00
| `id` | uuid | Primary key |
| `name` | varchar (unique, max 50 chars) | Team name |
| `createdAt` | timestamp | Creation time |
2026-04-16 16:26:58 +08:00
2026-07-11 13:01:55 +08:00
### api_keys {#api-keys}
2026-03-22 21:00:37 +08:00
API keys for programmatic access. The raw key is shown once on creation; only the hash is stored.
| Column | Type | Notes |
|---|---|---|
2026-06-16 18:04:52 +08:00
| `id` | uuid | Primary key |
| `userId` | uuid | Foreign key to `users.id` |
| `keyHash` | varchar | scrypt hash of the key |
| `name` | varchar | User-provided label |
| `createdAt` | timestamp | Creation time |
| `lastUsedAt` | timestamp | Updated on each authenticated request |
2026-03-22 21:00:37 +08:00
Keys are prefixed with `si_` followed by 96 hex characters (48 random bytes).
2026-07-11 13:01:55 +08:00
### pipelines {#pipelines}
2026-03-22 21:00:37 +08:00
Saved tool chains that users create in the UI.
| Column | Type | Notes |
|---|---|---|
2026-06-16 18:04:52 +08:00
| `id` | uuid | Primary key |
| `name` | varchar | Pipeline name |
| `description` | varchar | Optional description |
| `steps` | jsonb | Array of `{ toolId, settings }` objects |
| `createdAt` | timestamp | Creation time |
2026-03-22 21:00:37 +08:00
2026-07-11 13:01:55 +08:00
### user_files {#user-files}
2026-04-16 16:26:58 +08:00
2026-07-19 22:59:34 +08:00
Persistent file library. A saved edit is inserted as an independent root row by default ("save as new": `version` 1, `parentId` null, so the original stays listed), or as a parent-linked version when you overwrite the original (`parentId` set, `version` incremented, superseding it). The `toolChain` column records the tools applied.
2026-04-16 16:26:58 +08:00
| Column | Type | Description |
|--------|------|-------------|
2026-06-16 18:04:52 +08:00
| `id` | uuid | Primary key |
| `userId` | uuid | FK to users (CASCADE DELETE) |
| `originalName` | varchar | Original upload filename |
| `storedName` | varchar | Filename on disk |
| `mimeType` | varchar | MIME type |
2026-04-16 16:26:58 +08:00
| `size` | integer | File size in bytes |
| `width` | integer | Image width in px |
| `height` | integer | Image height in px |
| `version` | integer | Version number (1 = original) |
2026-06-16 18:04:52 +08:00
| `parentId` | uuid or null | FK to user_files (parent version) |
| `toolChain` | jsonb | Tool IDs applied in order to produce this version |
| `createdAt` | timestamp | Creation time |
2026-04-16 16:26:58 +08:00
2026-07-11 13:01:55 +08:00
### jobs {#jobs}
2026-03-22 21:00:37 +08:00
Tracks processing jobs for progress reporting and cleanup.
| Column | Type | Notes |
|---|---|---|
2026-06-16 18:04:52 +08:00
| `id` | uuid | Primary key |
| `type` | varchar | Tool or pipeline identifier |
| `status` | varchar | `queued` , `processing` , `completed` , or `failed` |
| `progress` | real | 0.0-1.0 fraction |
| `inputFiles` | jsonb | Array of input file paths |
| `outputPath` | varchar | Path to the result file |
| `settings` | jsonb | Tool settings used |
| `error` | varchar | Error message if failed |
| `createdAt` | timestamp | Creation time |
| `completedAt` | timestamp | Completion time |
2026-03-22 21:00:37 +08:00
2026-07-11 13:01:55 +08:00
### settings {#settings}
2026-03-22 21:00:37 +08:00
Key-value store for server-wide settings that admins can change from the UI.
| Column | Type | Notes |
|---|---|---|
2026-06-16 18:04:52 +08:00
| `key` | varchar | Primary key |
| `value` | varchar | Setting value |
| `updatedAt` | timestamp | Last update time |
2026-07-11 13:01:55 +08:00
### roles {#roles}
2026-06-16 18:04:52 +08:00
Custom roles with granular permissions.
| Column | Type | Notes |
|---|---|---|
| `id` | uuid | Primary key |
| `name` | varchar | Unique role name |
| `description` | varchar | Optional description |
| `permissions` | jsonb | Array of permission strings |
| `createdAt` | timestamp | Creation time |
2026-07-11 13:01:55 +08:00
### audit_log {#audit-log}
2026-06-16 18:04:52 +08:00
Security-relevant action log.
| Column | Type | Notes |
|---|---|---|
| `id` | uuid | Primary key |
| `userId` | uuid | FK to users |
| `action` | varchar | Action type |
| `details` | jsonb | Action-specific data |
| `createdAt` | timestamp | Action time |
2026-03-22 21:00:37 +08:00
2026-07-27 15:37:30 +08:00
### user_preferences {#user-preferences}
Per-user UI state, keyed by preference name. Backs the dashboard's pinned tools through `PUT /api/v1/preferences` .
| Column | Type | Notes |
|---|---|---|
| `userId` | text | FK to users, cascades on delete. Primary key with `key` |
| `key` | text | Preference name. Primary key with `userId` |
| `value` | jsonb | Preference payload |
| `updatedAt` | timestamp | Last write |
2026-07-11 13:01:55 +08:00
## Migrations {#migrations}
2026-03-22 21:00:37 +08:00
2026-06-16 18:04:52 +08:00
Drizzle handles schema migrations. Migration files live in `apps/api/drizzle/` . During development:
2026-03-22 21:00:37 +08:00
```bash
2026-06-16 18:04:52 +08:00
cd apps/api
npx drizzle-kit generate # generate a migration from schema changes
npx drizzle-kit migrate # apply pending migrations
2026-03-22 21:00:37 +08:00
```
2026-06-16 18:04:52 +08:00
In production, pending migrations are applied automatically on startup.
2026-07-11 13:01:55 +08:00
## Backup and restore {#backup-and-restore}
2026-06-16 18:04:52 +08:00
The relational database lives in the Postgres container's `SnapOtter-pgdata` volume, not the app's `/data` volume.
2026-07-27 15:37:30 +08:00
**Logical backup with validation (recommended)**
2026-06-16 18:04:52 +08:00
```bash
2026-07-27 15:37:30 +08:00
# Dump into PostgreSQL's portable custom archive format
docker exec SnapOtter-postgres \
pg_dump --format= custom --no-owner -U snapotter snapotter > snapotter.dump
test -s snapotter.dump
docker exec -i SnapOtter-postgres pg_restore --list < snapotter.dump >/dev/null
2026-06-16 18:04:52 +08:00
2026-07-27 15:37:30 +08:00
# Restore into a fresh/disposable target first and fail on the first SQL error
docker exec -i SnapOtter-postgres \
pg_restore --exit-on-error --clean --if-exists --no-owner \
-U snapotter -d snapotter < snapotter.dump
2026-06-16 18:04:52 +08:00
```
2026-07-27 15:37:30 +08:00
This database dump does not contain saved library objects in `/data/files` or durable BullMQ state in Redis. Back up and restore those with the coordinated procedure in [Security & Hardening ](/guide/security#backup-and-recovery ).
**Cold volume snapshot**
2026-06-16 18:04:52 +08:00
```bash
2026-07-27 15:37:30 +08:00
# Stop every service first, then use your storage platform to snapshot the
# PostgreSQL, app-data, and Redis volumes as one crash-consistent set.
docker compose -f docker/docker-compose.yml stop
2026-06-16 18:04:52 +08:00
```
2026-07-27 15:37:30 +08:00
Do not copy a live PostgreSQL data directory with `tar` . Compose prefixes volume names by project, so resolve the mounted volume IDs from `docker inspect` or your storage platform rather than assuming the literal label `SnapOtter-pgdata` .
2026-07-11 13:01:55 +08:00
### Migrating from 1.x (SQLite) {#migrating-from-1-x-sqlite}
2026-06-16 18:04:52 +08:00
2026-07-04 23:15:39 +08:00
Upgrading from SnapOtter 1.x has its own guide: see [Upgrading from 1.x to 2.0 ](./upgrading ). In short, reuse your existing `/data` volume and 2.0 auto-detects and imports `/data/snapotter.db` on first boot (or set `SQLITE_MIGRATE_PATH` to point at it explicitly). Back up the whole `/data` volume first, not just `snapotter.db` : 1.x uses SQLite WAL mode, so a stopped container often leaves most of its data in `snapotter.db-wal` beside an almost-empty `snapotter.db` .