Databases
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:
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:
| Database | database | Install | Use it for |
|---|---|---|---|
| SQLite | sqlite(path) from tenoncms/sqlite | Nothing (Node 22.16+) | A Node server with a disk that keeps its files |
| SQLite and Turso | libsql(url) from tenoncms/libsql | @libsql/client | A local file, or Turso on a serverless host such as Vercel |
| Postgres | postgres(url) from tenoncms/postgres | pg | A Postgres server, such as Neon, on Node or Vercel |
| PGlite | pglite(new PGlite(dir)) from tenoncms/pglite | @electric-sql/pglite | Postgres in local development and tests |
| Durable Object | durableObject() from tenoncms/cloudflare | Nothing | Cloudflare 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:
import { sqlite } from "tenoncms/sqlite";
export default defineCms({
// ...
database: sqlite(process.env.DATABASE_PATH ?? "./cms.db"),
});
: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
pnpm add @libsql/client
yarn add @libsql/client
bun add @libsql/client
import { libsql } from "tenoncms/libsql";
export default defineCms({
// ...
database: libsql({
url: process.env.DATABASE_URL ?? "file:./cms.db",
authToken: process.env.DATABASE_AUTH_TOKEN,
}),
});
file:./cms.db), :memory:, or a Turso URL (libsql://<db>-<org>.turso.io). A relative file starts in the working directory.libsql() also takes every other option of createClient() from @libsql/client. A string is the same as { url }.
- A
libsql:,https:orwss: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
pnpm add pg
yarn add pg
bun add pg
import { postgres } from "tenoncms/postgres";
export default defineCms({
// ...
database: postgres(process.env.DATABASE_URL!),
});
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 returnspool, thepgpool. On Vercel, pass it toattachDatabasePool()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
pnpm add @electric-sql/pglite
yarn add @electric-sql/pglite
bun add @electric-sql/pglite
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:
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.
| Database | How it migrates |
|---|---|
| Postgres, PGlite, libSQL | In one transaction. A failed run changes nothing. |
node:sqlite, Durable Object | Without 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:
- Back up the site with
npx tenon backup --content-only. - Point
databaseintenon.config.tsto the new database. - Restore the file with
npx tenon restore.
The users do not move. See Move from SQLite to Postgres.
Related resources
- Configuration: the options of
tenon.config.tsand its environment variables. - Media storage: where uploaded files live.
- Deployment: the database for each host.
- Backup and restore: copy content between databases.