Guides
Postgres
Run each context on its own Postgres database.
Postgres is a database server: your backend connects to it over the network instead of opening a file. Use it when several instances of your backend must share one database, or when you already run Postgres.
Start a backend on Postgres
Section titled “Start a backend on Postgres”-
Create the backend with
--db postgres:Terminal window newt init newthub --db postgrescd newthubnewt add context tasks -
Start a Postgres server for development, if you do not have one. With Docker:
Terminal window docker run -d --name newthub-postgres -e POSTGRES_PASSWORD=postgres \-p 5432:5432 postgres -
Create one database per context (see Create the databases):
Terminal window docker exec newthub-postgres createdb -U postgres newthub__tasks -
Start the backend:
Terminal window bun run dev
On start, each context connects to its database and runs its migrations.
One database per context
Section titled “One database per context”Each context has its own database, and its own variable for it. newt add context tasks writes it to .env and .env.example:
TASKS_DATABASE_URL=postgres://postgres:postgres@localhost:5432/newthub__tasks- The variable is the context’s folder name in capitals,
-turned into_, plus_DATABASE_URL. - The suggested database name is
<backend>__<context>: the backend’s package name and the context’s folder, with anything but letters and digits turned into_. Two backends on one server never share a database. - The variable is required. Without it the backend does not start:
TASKS_DATABASE_URL is not set: see .env.example.
src/stores.ts opens each one:
export async function openDatabase( context: string, migrations: string, stores: Stores,): Promise<Database> { if (stores === 'memory') { return new MemoryDatabase() } return PostgresDatabase.open({ url: requiredEnv(`${envOf(context)}_DATABASE_URL`), migrations, })}Create the databases
Section titled “Create the databases”newt does not create databases: each one must exist before the backend starts. Create one per context, with any tool you like.
With psql:
CREATE DATABASE newthub__tasks;newt’s text columns are declared COLLATE "C" in the migrations it generates, so the database’s default locale does not matter. If you prefer plain TEXT columns, create the database with the C locale instead:
CREATE DATABASE newthub__tasks TEMPLATE template0 LOCALE 'C';The user in the URL needs to create tables in the database: the migrations run as that user.
Migrations
Section titled “Migrations”Each context’s SQL files live in src/contexts/<context>/migrations/. newt add module tasks/task writes the Postgres version:
CREATE TABLE "task" ( id TEXT COLLATE "C" PRIMARY KEY, version INTEGER NOT NULL, name TEXT COLLATE "C" NOT NULL, created_at TEXT COLLATE "C" NOT NULL, updated_at TEXT COLLATE "C" NOT NULL);PostgresDatabase.openruns every pending migration when the backend starts, together with newt’s own tables (newt_job,newt_schedule_tickand others).- Several instances starting at once are safe: newt takes a lock while it migrates, so each migration runs once.
- newt checks every text column when it opens the database. A column that would compare or sort text differently from other databases stops the start, with a message that names it.
For JSON values use jsonb, for yes/no values boolean, for whole numbers beyond 2^31 bigint. More in Database and migrations.
Connection options
Section titled “Connection options”PostgresDatabase.open(options) takes:
| Option | Default | What it is |
|---|---|---|
url |
required | The database’s URL |
migrations |
none | The context’s migrations folder |
connections |
10 |
Connections in the pool |
waitTimeoutMs |
30000 |
How long a save waits for a lock |
newt connects with Bun’s SQL client (Bun.SQL). Options it reads from the URL work too, such as TLS:
TASKS_DATABASE_URL=postgres://app:secret@db.example.com:5432/newthub__tasks?sslmode=requireTo change the pool size, edit src/stores.ts:
return PostgresDatabase.open({ url: requiredEnv(`${envOf(context)}_DATABASE_URL`), migrations, connections: 20,})Tests on Postgres
Section titled “Tests on Postgres”The tests of a Postgres backend run in memory unless you set TEST_DATABASE_URL. With it, each test creates its own database on that server, runs your migrations, and drops it. See Testing.
TEST_DATABASE_URL=postgres://postgres:postgres@localhost:5432/postgres \ bun test