Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

PostgreSQL with pgvector

Drusite keeps accounts, workspaces and every page’s sealed document and history in PostgreSQL, and its search index with pgvector. The Compose file starts one for you; this page is for using your own, or a managed one.

Prerequisites

  • PostgreSQL 16 or newer. CI runs on 16, the oldest Drusite supports; the Compose file runs 18.
  • pgvector 0.8 or newer, installed on the server. The pgvector/pgvector images have it, and most managed services offer it (Amazon RDS and Aurora, Google Cloud SQL, Azure Database for PostgreSQL, Supabase, Neon, CloudNativePG with an image that includes it).
  • A way to run SQL as an administrator, once.

1. Make a role and a database

Drusite should connect as an ordinary role. Row-level security is its second line between workspaces, and PostgreSQL doesn’t apply it to superusers or roles with BYPASSRLS, so the server refuses those (unless you set [database] allow_rls_bypass, which you shouldn’t). The role may own its database.

CREATE ROLE drusite LOGIN PASSWORD 'choose-a-long-password';
CREATE DATABASE drusite OWNER drusite;
\connect drusite
CREATE EXTENSION vector;

CREATE EXTENSION vector needs an administrator, because pgvector isn’t a trusted extension. If the role can create it, the server does so itself on first start; if not, it stops and says to run that line.

2. Point Drusite at it

The URL carries the password, so give it from a file or the environment, never in the config:

[database]
url_file = "/run/secrets/database-url"

The file holds one line, such as postgres://drusite:PASSWORD@db.example.test:5432/drusite?sslmode=require. URL-encode the password if it has characters such as @, / or #. Any libpq-style option in the query string works, sslmode and sslrootcert included.

3. Start the server

On every start the server checks, before it changes anything:

  1. that PostgreSQL is 16 or newer;
  2. that its role isn’t a superuser and doesn’t have BYPASSRLS;
  3. that pgvector 0.8 or newer is available, and enabled in this database (creating it if allowed);

then applies any pending migrations. Each failure stops the server with a message saying what to fix; Troubleshooting lists them.

Migrations as a separate step

By default the server migrates on start. To run migrations yourself, as a deploy step or with a more privileged role, turn that off and run migrate first:

[database]
migrate_on_start = false
drusite-server migrate

A server whose database has pending migrations and migrate_on_start = false refuses to start, and its /readyz says "database": "failing".

Connections

Each server process opens up to max_connections (16 by default). With several replicas, PostgreSQL needs room for all of them, plus a few for drusite-server commands and your own tools. A connection pooler such as PgBouncer works in session mode.

What’s in it, and what that reveals

The database holds page documents and history sealed with each workspace’s key (or, for end-to-end encrypted workspaces, with keys only the members’ devices have). It also holds, for standard workspaces, the search index: each block’s words as a tsvector and, with search by meaning, its embedding. Those are derived from the text and reveal what it’s about to anyone who can read the database or its backups. [search] enabled = false keeps them out. See Search by meaning.

Backups

Back the database up with whatever you use for PostgreSQL: pg_dump, WAL archiving with pgBackRest or Barman, or your provider’s snapshots. A backup is useless without the master key that was current when it was made: see backups and restore.