NEW RasterizedDB 1.1.0 is out: sharding and the gateway, the message broker, roles and row-level security, compression, .NET on NuGet, and the enterprise edition. What's new ›
DOCS / ENTERPRISE / TENANTS

Tenants

Strict tenant mode turns one RasterizedDB server into a database-as-a-service host: each customer (a tenant) gets a database of its own, and nothing that belongs to the server as a whole, or to another customer, can be seen or changed from a customer's database. Use it when the people connecting to your server are customers you do not trust with each other's data or with the server.

It works on a single server. To spread customers over several servers behind one address, add per-user shards.

How it works

Strict tenant mode divides sessions into two kinds:

  • The operator: a superuser of the operator database (operator_database in the configuration file, normally the server's default database postgres). Only the operator administers the process: creates and drops databases and tenants, changes settings, runs backups, replication and storage statements, and reads process-wide state.
  • Everyone else: every session in any other database, superusers of that database included. A superuser of a customer's database administers that database (its tables, roles and grants) and nothing beyond it.

Roles are per database in RasterizedDB, so a tenant's roles exist only in the tenant's database and can log in nowhere else.

What a tenant cannot do

AreaRefused for anyone but the operator
Other databasesSHOW DATABASES lists only the session's own database, without its location. The pg_database catalogue shows only the session's own database. CREATE DATABASE, DROP DATABASE and the branching statements are refused.
Process-wide settingsSET CONFIG, SET FILTER, RESET FILTER, KV CONFIG (other than KV CONFIG GET), KV SAVE, KV BGSAVE, REPLICATION … and SHARD … statements.
Server storage pathsBACKUP, RESTORE, KV FLUSHALL, and the STORAGE … statements that add or change server locations and stores. Placing or moving one's own tables on locations and stores the server already has (STORAGE PLACE, STORAGE MOVE, STORAGE SET, STORAGE REBALANCE) stays allowed.
ImportingIMPORT … FROM POSTGRES.
Statistics and other sessions' statementsSHOW QUERIES, SHOW QUERY STATS, SHOW RECENT, RESET QUERY STATS and SHOW BRANCH DIFF.
Process-wide stateSHOW BACKUP, SHOW REPLICATION, SHOW STORAGE, SHOW CONFIG, SHOW SHARDS, SHOW SHARD … and SHOW BRANCHES.
Other sessionsEnding another account's session on the MySQL front end (KILL).
The Redis-compatible portRefused at AUTH for any session outside the operator database.
Tenant statementsCREATE TENANT, ALTER TENANT, DROP TENANT and SHOW TENANTS.

A refusal is SQLSTATE 42501 with a message naming the operator database, for example:

text
ERROR:  process-wide settings are reserved to the operator in strict tenant mode (database "postgres")

Strict tenant mode also:

  • refuses to start the server with --pg-auth trust, so no login gets in without a password;
  • never lets CREATE DATABASE take over a directory under the data directory that already holds data, so a new customer database never opens files it did not write;
  • holds the server's outbound connections to the hosts in outbound_hosts, when that is set (below).

The permission checks of the GPL server still apply underneath: strict tenant mode only refuses more.

Turn it on

Add these lines to the enterprise configuration file and start the enterprise server with --enterprise-config and the PostgreSQL protocol (--protocol pgwire or both). Installing shows the full command.

text
tenant_mode strict
operator_database postgres
tenants_file /var/lib/rasterizeddb/rdb_tenants

operator_database must be the server's default database (--pg-default-db, postgres by default). tenants_file is where the tenant statements keep their directory; without it they are refused.

Tenant statements are answered on PostgreSQL-protocol connections. Connect as a superuser of the operator database to run them.

Create a tenant

sql
CREATE TENANT acme OWNER acme_admin PASSWORD 'Acme-initial-pass-2026'
       LIMITS (connections = 50, storage = '20GB', statement_timeout = '60s');
PartMeaning
acmeThe tenant's name, which is also its database's name.
OWNER acme_adminThe tenant's owner role, created with LOGIN and the password.
PASSWORD '…'The owner's password, as a string literal (write '' for a quote inside it).
LIMITS (…)Optional. Any of connections, storage and statement_timeout (Per-tenant limits). Without it, the tenant has no limits.

CREATE TENANT then, in order:

  1. records the tenant and its limits in the tenant directory (the limits apply at once);
  2. creates the owner role in the operator database;
  3. creates the database with that role as its OWNER, which (with the default roles.create_database_roles = owner) gives the new database only two roles: the owner and the operator role that ran the statement;
  4. drops the owner role from the operator database, so the owner can log in to its own database only.

Names follow SQL's rules: an unquoted name is folded to lower case (ACME becomes acme); a double-quoted name is kept as written. A name has 1 to 63 characters and no spaces, quotes or control characters.

The owner is a login role, not a superuser. To let the customer administer its own database fully, connect to the tenant's database as the operator role (CREATE TENANT copied it there) and make the owner a superuser of that database:

sql
-- connected to database acme as the operator role
ALTER ROLE acme_admin SUPERUSER;

Under strict tenant mode that makes acme_admin a superuser of acme alone.

The customer then connects to its own database:

sh
psql "host=db.example.com port=5432 dbname=acme user=acme_admin sslmode=require"

Change, list and drop tenants

Replace a tenant's limits:

sql
ALTER TENANT acme SET LIMITS (connections = 100, statement_timeout = '2min');

SET LIMITS replaces all of the tenant's limits: a limit not named is removed. The new limits apply to the next sessions at once, and the directory is saved before the statement returns.

List the tenants:

sql
SHOW TENANTS;
text
 tenant |   owner    | connections | max_bytes | statement_timeout_ms
--------+------------+-------------+-----------+----------------------
 acme   | acme_admin |         100 |           |               120000
(1 row)

An empty limit means none. max_bytes is the storage limit in bytes and statement_timeout_ms the statement timeout in milliseconds.

Drop a tenant and its database, with every row in it:

sql
DROP TENANT acme;
DROP TENANT IF EXISTS acme;

DROP TENANT removes the tenant from the directory and then runs DROP DATABASE. With IF EXISTS, a tenant that is not in the directory is skipped: the statement returns one row saying so instead of an error.

MOVE TENANT is recognised but refused: moving a whole database to another server is not built.

Per-tenant limits

Limit in LIMITS (…)ValueLine in the configuration fileWhat it does
connections = 50a whole numberlimit acme connections 50At most this many open sessions in the database. A PostgreSQL-protocol login past it gets 53300 too many connections for database "acme" (its limit is 50).
statement_timeout = '60s'a whole number with ms, s or min; no unit means millisecondslimit acme statement_timeout_ms 60000The statement timeout every PostgreSQL-protocol session of the database starts with. A client that asks for a shorter one in its startup parameters gets its own; one that asks for a longer one, or none, gets this one.
storage = '20GB'a whole number with B, KB, MB, GB or TB (powers of 1024); no unit means byteslimit acme max_bytes 21474836480Recorded and shown, not enforced.

Limits from CREATE TENANT and ALTER TENANT are kept in the tenant directory. For a database that has a directory entry, the entry's limits are used and the configuration file's limit lines for it are ignored. Use the limit lines for databases you create without CREATE TENANT; they change only with a restart.

Outbound host allowlist

Under strict tenant mode, outbound_hosts limits where the server opens connections: the source server of a PostgreSQL import, and S3 endpoints (backups and S3-backed storage). With no outbound_hosts line, nothing is held back.

text
outbound_hosts pg-legacy.internal s3.internal:9000
outbound_hosts *.amazonaws.com

A connection to a host not on the list fails with connecting to <host>:<port> is not allowed: the server's outbound_hosts does not list it. The list applies to the operator's own imports and backups too, so list every endpoint the server itself needs.

A worked setup for a database-as-a-service operator

One server, customers acme and globex, each with a database and an owner of its own.

1. Configure the server. /etc/rasterizeddb/enterprise.conf:

text
tenant_mode strict
operator_database postgres
tenants_file /var/lib/rasterizeddb/rdb_tenants
# Backups go to this S3 endpoint and nowhere else.
outbound_hosts s3.internal:9000

2. Start it with a TLS certificate for the customers' connections:

sh
export RASTERIZEDDB_PG_PASSWORD='operator-password-change-me'
rasterizeddb-enterprise \
  --location /var/lib/rasterizeddb \
  --protocol pgwire \
  --pg-addr 0.0.0.0 \
  --pg-cert /etc/rasterizeddb/server.crt \
  --pg-key /etc/rasterizeddb/server.key \
  --enterprise-config /etc/rasterizeddb/enterprise.conf

The operator role is the one the server creates on first start (--pg-user, rasterized by default) with that password.

3. Create the tenants as the operator:

sh
psql "host=db.example.com port=5432 dbname=postgres user=rasterized sslmode=require"
sql
CREATE TENANT acme OWNER acme_admin PASSWORD 'Acme-initial-pass-2026'
       LIMITS (connections = 50, statement_timeout = '60s');
CREATE TENANT globex OWNER globex_admin PASSWORD 'Globex-initial-pass-2026'
       LIMITS (connections = 20, statement_timeout = '30s');
SHOW TENANTS;

4. Hand each customer its connection string, for example host=db.example.com port=5432 dbname=acme user=acme_admin sslmode=require, and ask them to change the password with ALTER ROLE acme_admin PASSWORD '…'.

5. Check the isolation. Connected as acme_admin to acme:

sql
SHOW DATABASES;   -- lists acme only
SHOW CONFIG;      -- ERROR: process-wide state are reserved to the operator …

6. Later, raise a limit or remove a customer as the operator:

sql
ALTER TENANT acme SET LIMITS (connections = 100, statement_timeout = '60s');
DROP TENANT globex;

Keep /etc/rasterizeddb/enterprise.conf and the tenant directory file in your backups: neither is inside a database.

Limits

  • The storage limit is not enforced. storage and max_bytes are stored and shown only.
  • The statement timeout is a starting value. A session can still change it with SET statement_timeout or RESET statement_timeout. It and the connection limit are applied only to PostgreSQL-protocol connections.
  • Tenant statements are PostgreSQL-protocol only. Run them from psql or another PostgreSQL client, not over the native protocol.
  • CREATE TENANT records the tenant first. If a later step fails (for example the owner role already exists), the directory keeps the entry. Remove it with DROP TENANT; its DROP DATABASE step then reports that the database does not exist.
  • DROP TENANT removes the entry first. If its DROP DATABASE step fails, the database stays: drop it with DROP DATABASE as the operator.
  • The tenant directory is a local file. It is not part of any database, replication or backup.
  • pg_database shows only the session's own database under strict tenant mode, the operator's included. The operator lists every database with SHOW DATABASES.
  • Strict tenant mode refuses --pg-auth trust but does not check --pg-socket-auth: do not set a unix socket to trust on a tenant server.
  • MOVE TENANT is not built.
✓ page rendered in 146 µs axum + tera + htmx · no other javascript