Skip to content

Database

SnapOtter uses PostgreSQL 17 with Drizzle ORM (pg-core / node-postgres) for data persistence. The schema is defined in apps/api/src/db/schema.ts.

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. Requests are served on a role that can only read and write rows, which is covered under Least-privilege roles below.

Tables

users

Stores user accounts. Created automatically on first run from DEFAULT_USERNAME and DEFAULT_PASSWORD.

ColumnTypeNotes
iduuidPrimary key
usernamevarcharUnique, required
passwordHashvarcharscrypt hash
rolevarcharadmin, editor, or user
mustChangePasswordbooleanForced password reset flag
createdAttimestampCreation time
updatedAttimestampLast update time

sessions

Active login sessions. Each row ties a session token to a user.

ColumnTypeNotes
idvarcharPrimary key (session token)
userIduuidForeign key to users.id
expiresAttimestampExpiry time
createdAttimestampCreation time

teams

Groups for organizing users. Admins can assign users to teams.

ColumnTypeDescription
iduuidPrimary key
namevarchar (unique, max 50 chars)Team name
createdAttimestampCreation time

api_keys

API keys for programmatic access. The raw key is shown once on creation; only the hash is stored.

ColumnTypeNotes
iduuidPrimary key
userIduuidForeign key to users.id
keyHashvarcharscrypt hash of the key
namevarcharUser-provided label
createdAttimestampCreation time
lastUsedAttimestampUpdated on each authenticated request

Keys are prefixed with si_ followed by 96 hex characters (48 random bytes).

pipelines

Saved tool chains that users create in the UI.

ColumnTypeNotes
iduuidPrimary key
namevarcharPipeline name
descriptionvarcharOptional description
stepsjsonbArray of { toolId, settings } objects
createdAttimestampCreation time

user_files

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.

ColumnTypeDescription
iduuidPrimary key
userIduuidFK to users (CASCADE DELETE)
originalNamevarcharOriginal upload filename
storedNamevarcharFilename on disk
mimeTypevarcharMIME type
sizeintegerFile size in bytes
widthintegerImage width in px
heightintegerImage height in px
versionintegerVersion number (1 = original)
parentIduuid or nullFK to user_files (parent version)
toolChainjsonbTool IDs applied in order to produce this version
createdAttimestampCreation time

jobs

Tracks processing jobs for progress reporting and cleanup.

ColumnTypeNotes
iduuidPrimary key
typevarcharTool or pipeline identifier
statusvarcharqueued, processing, completed, or failed
progressreal0.0-1.0 fraction
inputFilesjsonbArray of input file paths
outputPathvarcharPath to the result file
settingsjsonbTool settings used
errorvarcharError message if failed
createdAttimestampCreation time
completedAttimestampCompletion time

settings

Key-value store for server-wide settings that admins can change from the UI.

ColumnTypeNotes
keyvarcharPrimary key
valuevarcharSetting value
updatedAttimestampLast update time

roles

Custom roles with granular permissions.

ColumnTypeNotes
iduuidPrimary key
namevarcharUnique role name
descriptionvarcharOptional description
permissionsjsonbArray of permission strings
createdAttimestampCreation time

audit_log

Security-relevant action log.

ColumnTypeNotes
iduuidPrimary key
userIduuidFK to users
actionvarcharAction type
detailsjsonbAction-specific data
createdAttimestampAction time

user_preferences

Per-user UI state, keyed by preference name. Backs the dashboard's pinned tools through PUT /api/v1/preferences.

ColumnTypeNotes
userIdtextFK to users, cascades on delete. Primary key with key
keytextPreference name. Primary key with userId
valuejsonbPreference payload
updatedAttimestampLast write

Migrations

Drizzle handles schema migrations. Migration files live in apps/api/drizzle/. During development:

bash
cd apps/api
npx drizzle-kit generate   # generate a migration from schema changes
npx drizzle-kit migrate    # apply pending migrations

In production, pending migrations are applied automatically on startup.

Least-privilege roles

Two roles, two jobs. DATABASE_URL serves requests and holds SELECT, INSERT, UPDATE, DELETE on the app's tables plus USAGE and SELECT on their sequences. That is the entire list. It cannot create or drop a table, install an extension, TRUNCATE, read pg_authid, create a database, alter a role, or touch the drizzle schema where migration history lives.

DATABASE_MIGRATION_URL is the privileged one. It runs migrations and grants the runtime role during boot, then closes before a single request is served.

Compose and the all-in-one image are wired this way already, existing installs included. On boot SnapOtter creates the runtime role if it is missing, grants it, migrates, then sweeps the grants onto tables that were there before. Upgrading needs no manual SQL.

Leaving DATABASE_MIGRATION_URL empty runs single-role, with DATABASE_URL doing both jobs exactly as it did before the split. That is a supported configuration, not a deprecated one. It is the right answer on managed Postgres, where creating roles is often not yours to do.

External and managed Postgres

On RDS, Supabase, Cloud SQL, or any cluster you run yourself, the split is opt-in. Create the runtime role once:

sql
CREATE ROLE snapotter_app LOGIN PASSWORD 'choose-a-strong-password'
  NOSUPERUSER NOCREATEDB NOCREATEROLE NOBYPASSRLS;

Then hand SnapOtter both connection strings, pointed at the same host, port, and database:

bash
DATABASE_URL=postgres://snapotter_app:[email protected]:5432/snapotter
DATABASE_MIGRATION_URL=postgres://snapotter:[email protected]:5432/snapotter

Stop there. SnapOtter applies the grants itself and reapplies them after every migration, so a table added by a future release is covered without anyone running SQL for it.

The role in DATABASE_MIGRATION_URL has to own the SnapOtter tables, because only a table's owner can grant on it. On an existing install that means the role you have been running SnapOtter as, not a fresh one created for the purpose. Point it at a new role that owns nothing and boot fails with an error saying exactly this. It also needs CREATEROLE to create and maintain the runtime role, and the right to create the drizzle schema.

Name the same role in both URLs and the split is off, and SnapOtter says so in the log instead of pretending otherwise. If your provider gives you no role that can both own the tables and hold CREATEROLE, run single-role.

Why the superuser bit is left alone

SnapOtter never strips SUPERUSER from a role by itself. On an install created before the split, snapotter is the cluster's only superuser, and demoting it would leave the cluster with none, recoverable only through single-user mode with the server stopped. Moving the long-lived connection to the restricted role is what buys the protection instead. The superuser is on the wire for the few seconds of boot and then gone.

Fresh all-in-one installs never have that problem. They get three roles: postgres (bootstrap superuser, absent from every connection string SnapOtter uses), snapotter (NOSUPERUSER, owns the data, connects only at boot), and snapotter_app (rows only, serves requests).

To demote an older snapotter anyway, create a second superuser first and log in as it to confirm it works. Then ALTER ROLE snapotter NOSUPERUSER.

Backup and restore

The relational database lives in the Postgres container's SnapOtter-pgdata volume, not the app's /data volume.

Logical backup with validation (recommended)

bash
# 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

# 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

Both commands connect as snapotter, the owner, and should keep doing so. The runtime role can't see the drizzle schema, so a dump taken as that role would come out incomplete. --no-owner leaves restored objects owned by whoever runs the restore, so running it as the owner puts ownership where the grants expect it. One catch on a fresh cluster: pg_dump carries the grants but not the roles they name, so create snapotter_app before restoring or --exit-on-error stops on the first GRANT. SnapOtter reapplies the grants on its next boot regardless.

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.

Cold volume snapshot

bash
# 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

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.

Migrating from 1.x (SQLite)

Upgrading from SnapOtter 1.x has its own guide: see Upgrading from 1.x to 2.0. 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.