Databases and Storage

Databases

Pick a database and connect it.

Tenon stores content, users and jobs in one database. You pick it with the database option in tenon.config.ts. The smallest setup uses Node's built-in SQLite and needs no package:

tenon.config.ts
import { defineCms } from "tenoncms";
import { sqlite } from "tenoncms/sqlite";

export default defineCms({
  // ...
  database: sqlite("./cms.db"),
});

Tenon creates cms.db and its tables on the first request.

The examples on this page read values from process.env. Astro and Nuxt read them in their own way. See Environment variables.

Pick a database

Each database has its own import and driver, so you install only the one you use:

DatabasedatabaseInstallUse it for
SQLitesqlite(path) from tenoncms/sqliteNothing (Node 22.16+)A Node server with a disk that keeps its files
SQLite and Tursolibsql(url) from tenoncms/libsql@libsql/clientA local file, or Turso on a serverless host such as Vercel
Postgrespostgres(url) from tenoncms/postgrespgA Postgres server, such as Neon, on Node or Vercel
PGlitepglite(new PGlite(dir)) from tenoncms/pglite@electric-sql/pglitePostgres in local development and tests
Durable ObjectdurableObject() from tenoncms/cloudflareNothingCloudflare Workers. See Cloudflare

npm create tenoncms asks for the database and offers only the ones that work where the site runs. See create-tenoncms.

A missing URL or path stops the server at startup, with one error that lists every problem of the config. A build that loads the config, for example to prerender pages, needs the values too.

SQLite and Turso

SQLite keeps the whole database in one file. It needs a disk that keeps its files between deploys, so it fits a Node server. On a serverless host, use Turso, which runs libSQL as a service.

Node's built-in SQLite

sqlite() uses node:sqlite, which ships with Node 22.16 and later:

tenon.config.ts
import { sqlite } from "tenoncms/sqlite";

export default defineCms({
  // ...
  database: sqlite(process.env.DATABASE_PATH ?? "./cms.db"),
});
The file, or :memory: for a database that lives only as long as the process. A relative path starts in the working directory. Use an absolute path in production.

On an older Node version, use libsql() with a file: URL.

libSQL

libsql() takes a URL, or the options of @libsql/client. Install the driver:

npm install @libsql/client
tenon.config.ts
import { libsql } from "tenoncms/libsql";

export default defineCms({
  // ...
  database: libsql({
    url: process.env.DATABASE_URL ?? "file:./cms.db",
    authToken: process.env.DATABASE_AUTH_TOKEN,
  }),
});
A file (file:./cms.db), :memory:, or a Turso URL (libsql://<db>-<org>.turso.io). A relative file starts in the working directory.The token of a Turso database. A file needs none.

libsql() also takes every other option of createClient() from @libsql/client. A string is the same as { url }.

  • A libsql:, https: or wss: URL uses the web client of @libsql/client. It loads no native binary, so it also runs in serverless functions.
  • On Vercel, a function's files do not persist. Use a file in development and Turso in production.

Postgres and PGlite

Postgres

postgres() connects to a Postgres server through a pg pool. Install the driver:

npm install pg
tenon.config.ts
import { postgres } from "tenoncms/postgres";

export default defineCms({
  // ...
  database: postgres(process.env.DATABASE_URL!),
});
A connection string, like postgres://user:password@localhost:5432/cms. Or the options of a pg pool, such as host, user, password, database, max and ssl. A pool config without a connection string can also read the PG* environment variables.
  • postgres() also returns pool, the pg pool. On Vercel, pass it to attachDatabasePool() from @vercel/functions, so idle connections close before the function is suspended. See Vercel.
  • A pooled connection string works, also behind PgBouncer in transaction mode, like Neon's URL with -pooler.
  • Tenon creates its tables in the current schema of the connection.

PGlite

PGlite is Postgres compiled to WebAssembly. It runs in the same process, in memory or in a folder. Use it to develop and test against Postgres without a server:

npm install @electric-sql/pglite
tenon.config.ts
import { PGlite } from "@electric-sql/pglite";
import { pglite } from "tenoncms/pglite";

export default defineCms({
  // ...
  database: pglite(new PGlite(process.env.DATABASE_DIR ?? "./.pglite")),
});

pglite() takes the PGlite instance. new PGlite() without a folder keeps the data in memory.

Keep the database files out of git:

.gitignore
cms.db*
.pglite

Durable Objects

On Cloudflare Workers, the CMS runs inside a Durable Object with its own SQLite storage. database: durableObject() from tenoncms/cloudflare needs no package and no URL. The setup has more parts, so it has its own guide: Cloudflare.

Your own database driver

database also takes { dialect }, a Kysely dialect that you build yourself. It runs the SQLite queries, or the Postgres queries when the dialect uses Kysely's PostgresAdapter.

Its transactions MUST stay open across await, because hooks and nested writes run inside them.

Migrations

Tenon creates and updates its own tables. There is no command to run. On the first query after a start, it runs the migrations of the installed tenoncms version, then the tables of better-auth and its plugins.

DatabaseHow it migrates
Postgres, PGlite, libSQLIn one transaction. A failed run changes nothing.
node:sqlite, Durable ObjectWithout a transaction.
  • Several instances can start at once on a fresh Postgres or libSQL database. They migrate one after the other: Postgres waits for a lock, libSQL for the write lock of the database.
  • An instance on a migrated database only reads, so it blocks no one.
  • With sqlite(), start one instance first on a fresh database.

Your fields need no migration. Content lives as JSON, so a change to a collection takes effect on the next start. See Upgrades and schema changes for what happens to existing content.

Switch to another database

Move the content with a backup. For example, from SQLite to Postgres:

  1. Back up the site with npx tenon backup --content-only.
  2. Point database in tenon.config.ts to the new database.
  3. Restore the file with npx tenon restore.

The users do not move. See Move from SQLite to Postgres.