Data model
Lifer keeps everything in one PostgreSQL database. This page groups the tables by role and gives the relationships and rules you need before changing them. The full definitions are the numbered files in packages/data-pipeline/migrations; M061 below means 061_*.sql. A column is usually added by a later migration than the one that created its table, so search the folder for the column name.
The single most important split is between catalog data, which Lifer downloads and may replace, and user data, which only the user creates and nothing downloaded may overwrite. State and sync explains how each update path respects it, and ADR 0005 why.
Core tables
CATALOG (global, replaceable) USER (per user_id)
┌────────────┐ ┌──────────────────┐ ┌───────────┐
│ regions │◀─────│ region_species │─────▶│ species │◀──────────┬───────────────────────┐
│ parent_id ─┼─┐ │ (region, species)│ │ gbif_key │ │ │
└────────────┘ │ └──────────────────┘ └───────────┘ │ species_id │ species_id
▲ └─▶ regions (self) ▲ ▲ ┌────┴──────────┐ ┌─────┴────────┐
│ │ │ │ captures_all │ │ user_species │
┌─────┴──────────────────────┐ species_traits ────┘ │ │ fingerprint │ │ (user,species│
│ region_species_user_added │ species_rarity │ │ deleted_at │ │ state,cover)│
│ (user, region, species) │ species_reference_ │ │ hidden_at │ └──────────────┘
└────────────────────────────┘ photos │ │ trip_id ──────┼──▶ trips
species_synonyms ───┘ └──┬─────────┬──┘
┌──────────┐ ┌──────────────────┐ │ │
│sea_zones │◀──│ sea_zone_species │──▶ species photos ◀─┘ └─▶ originals
└──────────┘ └──────────────────┘ (derivatives) (file on disk,
RAW/JPEG/video)
downloaded_packs ◀── pack_species ──▶ species albums ◀── album_captures ──▶ captures_all
users ◀── sessions, api_keys, user_inaturalist_accounts albums ◀── shared_links
Every user table carries user_id REFERENCES users ON DELETE CASCADE. Catalog tables have no user_id.
Catalog
Global rows, built by the data pipeline and delivered by the catalog seed and region packs, never by migrations.
| Table | What it holds | Key and rules |
|---|---|---|
species (M001) | One row per species: names, taxonomy, eBird and iNaturalist ids, reference photo and description with their credits, local file paths of the cached photo | PK id (uuid); gbif_key unique. CHECKs require a credit whenever a photo or description is set. genus is a generated column. is_other_taxa (M089) marks species a user added from iNaturalist (below). photo_withheld (M123) marks species whose photo can't be redistributed. |
species_traits (M001) | Habitat, population, endemism, extinction, occurrence counts, IUCN status | PK = FK species_id, cascade. IUCN status lives only here since M129, with CHECKs on its code and source. |
species_rarity (M001) | Each species' worldwide rarity tier and the scores behind it | PK = FK species_id, cascade. Tier names are fixed by a CHECK (M119). |
species_reference_photos (M003) | The gallery of reference photos per species | Unique (species_id, photo_url). |
species_synonyms (M053) | Other names for a species, from Catalogue of Life, iNaturalist, eBird or a merge | synonym_name is unique across the whole table, so a name points at one species only. |
species_search_names (M111) | Normalised names for search | Derived: kept current by triggers on species and species_synonyms. Don't write to it. |
species_merges (M113) | old_species_id → new_species_id for duplicates the pipeline merged | old_species_id has no FK on purpose: the old row is deleted once applied. |
species_splits (M118) | Parent and daughter species after a taxonomic split | Both FKs cascade. |
regions (M001) | World, continents, countries, provinces, with outlines and external codes | Self-referencing parent_id. Unique (name, parent_id) (M049). nearby_sea_zone_ids uuid[] links a region to its sea zones. outline_boxes (M200) holds each outer ring's bounding box, kept in step with the outline by a row trigger, so finding the region at a point never parses every outline. |
region_species (M001) | A region's checklist: frequency, seasonality, local tier, vagrant and introduced flags | PK (region_id, species_id), both cascade. Holds catalog rows only (see M125). |
sea_zones, sea_zone_species (M019) | Marine areas and their species | Unique zone name; checklist PK (sea_zone_id, species_id). |
reference_photo_blocklist (M106) | Photo URLs that must never be shown | PK photo_url. |
| Vector tables (M058, M101, M102, M105) | Image and text vectors per species and reference photo, for CLIP and BioCLIP 2 | Plain real[] with a model_version column; no pgvector. |
Some catalog-side tables are maintainer inputs that the seed doesn't ship: region_species_manual_overrides, region_group_effort, region_unmatched_taxa, species_nonnative_countries. region_species_hotspots reaches installs through packs.
User library
| Table | What it holds | Key and rules |
|---|---|---|
captures_all (M002, renamed in M061) | One row per photo or video the user has: species, time, place, camera fields, rating, tags, cull marks | species_id → species with no ON DELETE (a species with captures can't be deleted). fingerprint is the SHA-256 of the file. tags text[] (M098); there is no tags table. Appearance (life_stage, sex, plumage, morph, M196). imported_by_key_id (M202) is the API key that imported it, so an app adds species only to its own imports; it's left out of the captures view on purpose. Smart tag decisions (M208): smart_tags_accepted (jsonb, term id to the path it added to tags, the marker that tells an accepted smart tag from a typed one) and smart_tags_rejected (term ids never suggested again); a trigger moves an accepted term to rejected when its tag is removed, and keeps both when a sync sender sends NULL. Not in the captures view, like imported_by_key_id. trip_id and region_id are SET NULL on delete. |
captures (view) | SELECT * FROM captures_all WHERE deleted_at IS NULL AND hidden_at IS NULL (M130) | What almost all code reads. See Trash and hidden photos. |
photos (M002) | The displayable versions of a capture: paths to display, thumb and video preview derivatives, size, kind, and the card crop a trip or album species card uses when it shows this photo instead of the featured one (the featured photo's crop lives on user_species) | capture_id cascades. captures_all.current_photo_id points at the shown one. |
originals (M007) | The actual files: a RAW, JPEG or video, by path | Unique (capture_id, kind). managed says whether Lifer filed the file and may move or write it. content_hash, EXIF fingerprints for RAW pairing, volume_id for external drives. capture_id is nullable: an unpaired RAW can wait for its JPEG. user_id is always set, so ownership never depends on the capture. |
capture_species (M021) | Extra species in the same photo | PK (capture_id, species_id). |
capture_written_metadata (M199) | The exact title, headline, caption and extra keywords Lifer last wrote into each managed file, so it only ever replaces its own values | PK (original_id, slot, field), cascades with the original. slot is the file itself or one of its two sidecar names, since each is edited, and written, independently. A file with no row falls back to the in-file markers. Safe to empty: files then read as written before the record existed. |
capture_scenes (M141) | The scene classes (sky, water, fence...) and colours filling each capture's photo, as percent of the frame, for Gallery search | PK = FK capture_id, cascade. Plain jsonb with a model_version column; photo_id is the photo it was computed from, so a changed photo is redone. |
capture_smart_suggestions, capture_smart_tag_state (M207) | Smart tag suggestions per capture (vocabulary term id, score, source), worked out from stored signals by a background pass (apps/api/src/smartTags/), and when each capture was last evaluated | Both cascade with the capture. Derived, local and never synced: emptying them only makes the pass evaluate every photo again. version (vocabulary, calibration, engine, text model) and inputs (a hash of the stored signals' timestamps) decide when a photo is evaluated again. |
smart_tag_hidden_terms (M208) | Smart tag vocabulary terms the user turned off for every photo | PK (user_id, term_id). Synced as a set, with updated_at, pushed_at and a smart_tag_hidden tombstone. |
smart_tag_batches (M208) | Each accept or reject batch's undo: per photo and term, the decision before it | Local, never synced. A token undoes once within an hour; rows older than a day are pruned on the next batch. |
user_species (M002) | The user's relationship with a species: state (collected, seen or none), target flag, cover photo and card crop, first collected date, best rating | PK (user_id, species_id): at most one row per user and species. |
target_lists, target_list_species (M195) | Named target lists, general or for one region, and their species | A list only sets user_species.is_target, the one flag everything reads. direct marks a species that was a target before any list held it, so removing a list never clears it. |
albums, album_captures (M071) | Albums and their photos; cover_auto_photo_id (M204) is the cover Lifer picked, which counts as automatic only while cover_photo_id still equals it | album_captures.capture_id references captures_all, not the view; trashed photos are filtered in queries. |
trips (M046) | A trip: its source folder, destination folder (M112), cover | A capture belongs to at most one trip, through captures_all.trip_id. |
storage_volumes (M050, M104) | External drives (per user) and server library roots (per install) | A CHECK enforces the two shapes: a drive has a user and no path; a root has a path and no user. |
capture_migrations (M033) | Earlier desktop migrations' record of what was sent where; no longer written (captures_all.pushed_at and remote_id, M150, took over) | PK (capture_id, server_url). |
ignored_library_files (M063) | Files the user told reimport to skip, by content hash | Unique (user_id, content_hash). |
There's no encounters table. Encounters are computed when needed by apps/api/src/lib/clusterEncounters.ts (captures less than 60 minutes apart form one encounter).
Trash and hidden photos
Trashing sets captures_all.deleted_at; a cull reject imported as hidden sets hidden_at (M130). The captures view leaves both out, so ordinary queries never see them. Code reaches past the view to captures_all only on purpose: trash and restore (apps/api/src/captures/trash.ts, which also purges with DELETE FROM captures_all, since a delete through the view would miss trashed rows), hidden photos (captures/hidden.ts), and the few places that must include hidden photos, such as writing file metadata (uploads/xmpSidecarSync.ts), moving and reorganizing files (uploads/managedFolders.ts, uploads/reorganizeOriginals.ts), and serving the owner's own photo files (photos/routes.ts, which includes trashed ones too, for the Trash page).
When you add a column to captures_all, drop and recreate the view in the same migration. A SELECT * view keeps the column list it was created with (M098 and M130 show the pattern).
Per-user overlays on the catalog
These tables change how the shared catalog looks to one user without touching it, so a catalog update can't erase them:
| Table | Effect |
|---|---|
user_archived_species (M037) | Hides a species everywhere for this user. |
region_species_hidden (M100) | Hides a species in one region's checklist. |
user_tier_overrides (M115) | The user's own rarity tier for a species, in one region or everywhere (region_id NULL). Unique on (user_id, COALESCE(region_id, zero uuid), species_id). |
region_species_user_added (M124) | A species the user added to a region's checklist. via_list (M203) marks a list's additions; a hand addition (false) wins and a list removal takes off only its own. |
sea_zone_species_user_added (M126) | The same for a sea zone. |
Before M124, user additions were rows in region_species, where a catalog update could overwrite or prune them. M125 moved them out. Additions that matched an existing catalog row couldn't be told apart and stayed in region_species (see the comment in M125).
Other Taxa species are a partial exception. A user can add any species from iNaturalist (apps/api/src/species/otherTaxa.ts); it becomes a row in the shared species table with is_other_taxa = true, with no user_id. Which user "has" it is recorded only by their captures, user_species and checklist additions. The catalog seed never includes these rows (build-catalog-seed.ts leaves them out of the copy it dumps), so a catalog update doesn't touch them.
Lists from iNaturalist (M203, apps/api/src/species/speciesLists/) are a saved taxon and place per user that a background job fills page by page:
| Table | What it holds |
|---|---|
species_lists | The definition (inat_taxon_id, inat_place_id, their names, region_id the checklist it goes on, native_only), progress (size_estimate, added_count, status, last_error code) and the resume point (next_page, next_batch_at). Unique on (user_id, inat_taxon_id, inat_place_id, region_id). target_list_id and target_photographed (M205) also put the species it adds on a target list. Sync columns as saved_spots: updated_at, pushed_at, remote_updated_at, and a species_list tombstone on delete. |
species_list_members | One row per list and species: inat_count, establishment_means, last_seen_at, and created_species when the list created the species row. Members leave with their list. |
A list's species are existing catalog species (matched by iNaturalist id, then exact scientific name; an id or name several catalog species share is never guessed) or new Other Taxa rows with a synthetic gbif_key of minus the taxon id. They reach a checklist only through region_species_user_added rows with via_list, never region_species, so the catalog seed, packs and the publish gate never see them, and the API's open data routes for apps leave Other Taxa species out.
Per-user preferences are columns on users (photo folder organisation, species naming styles, suggestions on or off, and so on).
Packs and install state
| Table | What it holds |
|---|---|
downloaded_packs (M034) | Installed region packs: PK pack_id, content_version (M045) for skipping unchanged packs, the provinces applied (M056). |
pack_species (M054) | Which species each pack brought, and whether it supplied their photos (provided_enrichment). Removing a pack uses it as a reference count. |
install_settings (M103) | Key and JSON value per install: the catalog seed version, whether to fetch withheld photos, the versions of installed vector sets. |
inat_server_config (M075) | One row (id boolean PRIMARY KEY CHECK (id)): the iNaturalist client id and redirect URI. No client secret. |
Pack photos have no table of their own: their state is the path columns on species and species_reference_photos, which point into APP_DATA_DIR.
Auth
| Table | Key and rules |
|---|---|
users (M002) | Unique email; password_hash is Argon2id. The app allows one account per server (/auth/register refuses once a user exists); the desktop app creates local@lifer.app automatically. |
sessions (M002, M109) | id is the SHA-256 of the cookie token, never the token. expires_at indexed for the daily cleanup in lib/maintenance.ts. |
api_keys (M072) | key_hash (SHA-256, unique) and permissions text[], the scopes. |
shared_links (M071, M132) | A share of one album: unique token_hash, the SHA-256 of the link's token, which is how a link is found; token_encrypted, the owner's copy for the share list, encrypted with the server's key (lib/secretBox.ts); token is only set on rows from before M132 until the API's next start encrypts it. Optional password_hash, expires_at, revoked_at, download and metadata flags. |
user_inaturalist_accounts (M075) | OAuth tokens for posting observations, encrypted with the server's key (inaturalist/tokenStore.ts). capture_inaturalist_observations records which capture became which observation (unique capture_id). |
The schema is multi-user throughout (every user table has user_id) even though the app creates one account. Don't rely on it as an isolation boundary: it hasn't been built or tested as one. See Security model.
Caches and bookkeeping
gbif_response_cache(M040),inat_response_cache(M055): raw API responses keyed by URL. Safe to empty.collection_data_version: a sequence, not a table. Statement-level triggers on the species, checklist, capture and overlay tables bump it on every change (M111, M121, M124, M126, M201, M203).GET /collection,/collection/countand/collection/treeuse it in their ETags (apps/api/src/collection/routes.ts); the tree route also keeps its last few answers by ETag in memory (in the desktop app it builds the All regions tree into that copy in the background once the version has held still for a few seconds, so Tree of life opens from memory), and the web client resends a kept ETag itself so a 304 reuses the parsed answer (apps/web/src/api/answers.ts). A new table that changes what the collection page shows needs the trigger too, or browsers keep showing a stale page.region_species_rollup(M198): for each region, the species of its subtree (asregionTreeCtedraws it) that its ownregion_speciesrows lack. The Tree of life's region filter reads it, so a continent is one indexed lookup instead of every province's rows. About 180,000 rows (roughly 20 MB with its key) on the full catalogue, rebuilt in full in about 10 seconds. Derived, never shipped in a seed. Statement triggers onregion_speciesand onregions(parent_id,home_country_id) bump theregion_checklist_versionsequence; when it no longer matches the version stamped ininstall_settings, the API rebuilds the rollup in the background (apps/api/src/regions/rollup.ts) and serves the region filter from the subtree's rows meanwhile. It rebuilds at startup and after a pack install, a pack removal or a catalog update. A write toregion_speciesorregionsthat skips triggers (replica mode,DISABLE TRIGGER) must bumpregion_checklist_versionitself, as the catalog seed's empty-table load does. Safe to empty.schema_migrations: applied migration file names, created bypackages/data-pipeline/src/migrate.ts.pipeline_runs(M115): the maintainer pipeline's stage log. Unused by installs.
Two leftovers, scan_roots (M013, replaced by trips in M046) and users.catalog_seed_version (moved to install_settings in M103), were dropped in M131.
Invariants to keep
- Catalog tables never hold user data, and user tables never hold catalog data. User-made rows go in the overlay tables. The publish gate fails if a user's Other Taxa species ends up in a pack.
- Catalog species ids are stable across installs. Seeds, packs and the photo store are keyed by the same
species.id, and updates upsert on it. A catalog species that disappears upstream is merged (species_merges), never just deleted, because captures anduser_speciesreference it with no cascade. - One
user_speciesrow per user and species. Merges fold two rows into one (packages/core/src/species/speciesMerges.ts). - Files are referenced, not stored.
originals.refis a file path;photospaths point intoAPP_DATA_DIR. The database never holds image bytes. - Duplicates aren't prevented by the schema.
captures_all (user_id, fingerprint)is indexed but not unique; the import flow detects duplicates and asks (see Imports and metadata). - Foreign keys without
ON DELETE(captures_all.species_id,captures_all.current_photo_id,user_species.species_id,user_species.cover_photo_id,albums.cover_photo_id) block deletes deliberately. Clear or re-point the reference first.
What the catalog seed contains
packages/data-pipeline/src/scripts/build-catalog-seed.ts dumps exactly these tables, data only: species, species_reference_photos, species_traits, species_rarity, species_synonyms, species_merges, species_splits, regions, region_species, sea_zones, sea_zone_species, reference_photo_blocklist. It dumps a scratch copy of them with local file paths blanked and Other Taxa rows left out, so the source tables are never changed. Vectors ship as separate binary files. Everything else in the database is either user data or install state, and no download writes to it.