One server, many customers
Runs the enterprise server in strict tenant mode: each customer gets a database of its own with its own administrator, and nothing that belongs to the whole server is reachable from a customer's database. Other customers' databases are invisible to it; process-wide SHOWs, query statistics, other sessions' statements, file paths, outbound connections and the Redis port need the operator. Limits on connections, storage and statement time can be set per customer. setup.sh / setup.ps1 writes the configuration file and starts the server; create-tenant.sh / create-tenant.ps1 adds a customer.
Before you start
- The enterprise server binary, 1.1.0 or later:
rasterizeddb-enterprisefrom therasterizeddb-enterprise-<version>-<platform>release archive (rasterizeddb_core_enterprisein a build from source; see Installing the enterprise edition), under its own licence (LICENSE-POLYFORM-SB). The GPL server has no tenant mode; the script checks--helpfor--enterprise-config. psqlfor adding customers.- A password for the operator role
rasterizedinRASTERIZEDDB_PG_PASSWORD. Strict mode refuses to start with--pg-auth trust.
The configuration file
enterprise.conf in this folder shows every setting. The script writes one like it into RDB_CONFIG_DIR, with the paths filled in, if none is there:
| Line | What it does |
|---|---|
tenant_mode strict | Turns strict tenant mode on (off turns it off). |
operator_database postgres | The operator's database. Only a superuser connected to it administers the server. The script uses the server's default database, postgres. |
tenants_file <path> | Where CREATE TENANT keeps the tenant directory. Required for CREATE TENANT. |
limit <database> connections <N> | Most sessions at once in that database. |
limit <database> statement_timeout_ms <MS> | Longest a statement there may run. |
limit <database> max_bytes <BYTES> | Most storage that database may use. |
outbound_hosts <host> … | The only hosts the server may connect out to (host, host:port or *.suffix). Without it, nothing is held back. |
# starts a comment. The file is read at start.
Settings
| bash variable | PowerShell parameter | Default | What it does |
|---|---|---|---|
RDB_BIN | -RdbBin | rasterizeddb-enterprise / rasterizeddb-enterprise.exe | The enterprise server. |
RDB_DATA | -Data | ./data | The data directory; each customer's database is a subdirectory. |
RDB_CONFIG_DIR | -ConfigDir | ./enterprise | Holds enterprise.conf and the tenant directory tenants. |
RDB_LISTEN | -Listen | 127.0.0.1 | Where PostgreSQL and the native protocol listen. |
RDB_TLS_CERT, RDB_TLS_KEY | -CertPath, -KeyPath | none | Your certificate. |
create-tenant:
| bash variable | PowerShell parameter | Default | What it does |
|---|---|---|---|
RDB_PG_URL | -PgUrl | postgresql://rasterized@127.0.0.1:5432/postgres?sslmode=require | The operator database. |
PGPASSWORD | $env:PGPASSWORD | required | The operator's password. |
RDB_TENANT | -Tenant | required | The customer's database. |
RDB_TENANT_OWNER | -Owner | required | Its administrator role. |
RDB_TENANT_PASSWORD | $env:RDB_TENANT_PASSWORD | required | The administrator's password. |
RDB_TENANT_CONNECTIONS | -Connections | none | Connection limit. |
RDB_TENANT_STORAGE | -Storage | none | Storage limit, such as 20GB (KB, MB, GB, TB). |
RDB_TENANT_STATEMENT_TIMEOUT | -StatementTimeout | none | Statement time limit, such as 60s. |
Run it
export RASTERIZEDDB_PG_PASSWORD='use-a-long-secret'
RDB_BIN=./rasterizeddb-enterprise ./setup.shIn a second terminal, add a customer:
export PGPASSWORD='use-a-long-secret'
RDB_TENANT=acme RDB_TENANT_OWNER=acme_admin RDB_TENANT_PASSWORD='acme-secret-123' \
RDB_TENANT_CONNECTIONS=50 RDB_TENANT_STORAGE=20GB RDB_TENANT_STATEMENT_TIMEOUT=60s ./create-tenant.sh$env:RASTERIZEDDB_PG_PASSWORD = 'use-a-long-secret'
.\setup.ps1 -RdbBin .\rasterizeddb-enterprise.exe
# second window:
$env:PGPASSWORD = 'use-a-long-secret'; $env:RDB_TENANT_PASSWORD = 'acme-secret-123'
.\create-tenant.ps1 -Tenant acme -Owner acme_admin -Connections 50 -Storage 20GB -StatementTimeout 60sThe script sends this statement, which you can also run yourself as the operator:
CREATE TENANT acme OWNER acme_admin PASSWORD 'acme-secret-123'
LIMITS (connections = 50, storage = '20GB', statement_timeout = '60s');It creates the database acme with acme_admin as its owner and only role, and records the limits.
Check that it works
PGPASSWORD='use-a-long-secret' psql "postgresql://rasterized@127.0.0.1:5432/postgres?sslmode=require" -c "SHOW TENANTS"
PGPASSWORD='acme-secret-123' psql "postgresql://acme_admin@127.0.0.1:5432/acme?sslmode=require" \
-c "CREATE TABLE notes (id INT PRIMARY KEY, body TEXT)" -c "SELECT count(*) FROM notes"SHOW TENANTS lists each tenant with its owner and limits (tenant, owner, connections, max_bytes, statement_timeout_ms). acme_admin exists only in acme: it cannot log in to the operator database.
Manage customers
ALTER TENANT acme SET LIMITS (connections = 100, storage = '50GB');
DROP TENANT acme; -- drops the database and its data
DROP TENANT IF EXISTS acme;Limits take effect at once.
Stop or undo
Ctrl+C stops the server. DROP TENANT removes a customer and its data.
Limits
MOVE TENANT(moving a customer's database to another shard) is not built yet and is refused.- Tenant and administrator names here are letters, digits and
_; they are folded to lower case unless double-quoted in SQL. - The tenant directory is a plain file at
tenants_file, rewritten on every change. Back it up together withenterprise.conf.
The files
#!/usr/bin/env bash
# RasterizedDB Enterprise: one server shared by many customers, each in a
# database of its own (strict tenant mode). Writes the enterprise
# configuration file if there is none and starts the enterprise server in the
# foreground. Add customers with create-tenant.sh.
# Needs release 1.1.0 or later.
#
# Every setting can be overridden from the environment, for example:
# RDB_BIN=./rasterizeddb-enterprise RDB_LISTEN=0.0.0.0 ./setup.sh
set -euo pipefail
RDB_BIN="${RDB_BIN:-rasterizeddb-enterprise}"
RDB_DATA="${RDB_DATA:-./data}"
RDB_CONFIG_DIR="${RDB_CONFIG_DIR:-./enterprise}" # enterprise.conf and the tenant directory
RDB_LISTEN="${RDB_LISTEN:-127.0.0.1}"
RDB_TLS_CERT="${RDB_TLS_CERT:-}"
RDB_TLS_KEY="${RDB_TLS_KEY:-}"
die() { echo "error: $*" >&2; exit 1; }
command -v "$RDB_BIN" >/dev/null 2>&1 || die "'$RDB_BIN' not found; set RDB_BIN to the enterprise server binary"
case "$("$RDB_BIN" --help)" in
*--enterprise-config*) ;;
*) die "$RDB_BIN is not the enterprise server (it has no --enterprise-config)" ;;
esac
: "${RASTERIZEDDB_PG_PASSWORD:?set RASTERIZEDDB_PG_PASSWORD for the operator role rasterized}"
export RASTERIZEDDB_PG_PASSWORD
mkdir -p "$RDB_DATA" "$RDB_CONFIG_DIR"
RDB_DATA="$(cd "$RDB_DATA" && pwd)"
RDB_CONFIG_DIR="$(cd "$RDB_CONFIG_DIR" && pwd)"
conf="$RDB_CONFIG_DIR/enterprise.conf"
if [ ! -f "$conf" ]; then
umask 077
cat > "$conf" <<EOF
# RasterizedDB enterprise configuration, written by the enterprise-tenants setup script.
tenant_mode strict
operator_database postgres
tenants_file $RDB_CONFIG_DIR/tenants
# limit <database> connections <N> | statement_timeout_ms <MS> | max_bytes <BYTES>
# outbound_hosts <host[:port]|*.suffix> ...
EOF
echo "Wrote $conf"
else
echo "Using $conf"
fi
args=(--enterprise-config "$conf" --location "$RDB_DATA" --protocol both
--pg-default-db postgres --pg-auth scram-sha-256 --pg-addr "$RDB_LISTEN" --rdb-addr "$RDB_LISTEN")
if [ -n "$RDB_TLS_CERT" ]; then
args+=(--pg-cert "$RDB_TLS_CERT" --pg-key "$RDB_TLS_KEY" --rdb-cert "$RDB_TLS_CERT" --rdb-key "$RDB_TLS_KEY")
fi
echo "Operator: psql \"postgresql://rasterized@$RDB_LISTEN:5432/postgres?sslmode=require\" -c 'SHOW TENANTS'"
echo "Customers: RDB_TENANT=acme RDB_TENANT_OWNER=acme_admin RDB_TENANT_PASSWORD=… ./create-tenant.sh"
echo "Stop with Ctrl+C."
echo
exec "$RDB_BIN" "${args[@]}"#Requires -Version 5.1
<#
.SYNOPSIS
RasterizedDB Enterprise: one server shared by many customers, each in a
database of its own (strict tenant mode). Writes the enterprise
configuration file if there is none and starts the enterprise server in the
foreground. Add customers with create-tenant.ps1.
Needs release 1.1.0 or later.
.EXAMPLE
$env:RASTERIZEDDB_PG_PASSWORD = 'use-a-long-secret'
.\setup.ps1 -RdbBin .\rasterizeddb-enterprise.exe
#>
[CmdletBinding()]
param(
[string]$RdbBin = $(if ($env:RDB_BIN) { $env:RDB_BIN } else { 'rasterizeddb-enterprise.exe' }),
[string]$Data = $(if ($env:RDB_DATA) { $env:RDB_DATA } else { '.\data' }),
# enterprise.conf and the tenant directory.
[string]$ConfigDir = $(if ($env:RDB_CONFIG_DIR) { $env:RDB_CONFIG_DIR } else { '.\enterprise' }),
[string]$Listen = $(if ($env:RDB_LISTEN) { $env:RDB_LISTEN } else { '127.0.0.1' }),
[string]$CertPath = $env:RDB_TLS_CERT,
[string]$KeyPath = $env:RDB_TLS_KEY
)
$ErrorActionPreference = 'Stop'
$cmd = Get-Command $RdbBin -ErrorAction SilentlyContinue
if (-not $cmd) { throw "'$RdbBin' not found; pass -RdbBin with the enterprise server binary." }
if (-not ((& $cmd.Source --help) -match '--enterprise-config')) { throw "$RdbBin is not the enterprise server (it has no --enterprise-config)." }
if (-not $env:RASTERIZEDDB_PG_PASSWORD) { throw 'Set $env:RASTERIZEDDB_PG_PASSWORD for the operator role rasterized.' }
New-Item -ItemType Directory -Force -Path $Data, $ConfigDir | Out-Null
$Data = (Resolve-Path -LiteralPath $Data).Path
$ConfigDir = (Resolve-Path -LiteralPath $ConfigDir).Path
$conf = Join-Path $ConfigDir 'enterprise.conf'
if (-not (Test-Path -LiteralPath $conf)) {
@(
'# RasterizedDB enterprise configuration, written by the enterprise-tenants setup script.',
'tenant_mode strict',
'operator_database postgres',
"tenants_file $(Join-Path $ConfigDir 'tenants')",
'# limit <database> connections <N> | statement_timeout_ms <MS> | max_bytes <BYTES>',
'# outbound_hosts <host[:port]|*.suffix> ...'
) | Set-Content -LiteralPath $conf -Encoding ascii
Write-Host "Wrote $conf"
}
else {
Write-Host "Using $conf"
}
$serverArgs = @('--enterprise-config', $conf, '--location', $Data, '--protocol', 'both',
'--pg-default-db', 'postgres', '--pg-auth', 'scram-sha-256', '--pg-addr', $Listen, '--rdb-addr', $Listen)
if ($CertPath) {
$c = (Resolve-Path -LiteralPath $CertPath).Path
$k = (Resolve-Path -LiteralPath $KeyPath).Path
$serverArgs += @('--pg-cert', $c, '--pg-key', $k, '--rdb-cert', $c, '--rdb-key', $k)
}
Write-Host "Operator: psql `"postgresql://rasterized@${Listen}:5432/postgres?sslmode=require`" -c 'SHOW TENANTS'"
Write-Host 'Customers: .\create-tenant.ps1 -Tenant acme -Owner acme_admin (password from $env:RDB_TENANT_PASSWORD)'
Write-Host 'Stop with Ctrl+C.'
Write-Host ''
& $cmd.Source @serverArgs
exit $LASTEXITCODE#!/usr/bin/env bash
# RasterizedDB Enterprise: add a customer to a server in strict tenant mode.
# Creates the database RDB_TENANT with only its administrator RDB_TENANT_OWNER
# in it, and records its limits in the tenant directory.
# Needs release 1.1.0 or later.
#
# PGPASSWORD=<operator password> RDB_TENANT=acme RDB_TENANT_OWNER=acme_admin \
# RDB_TENANT_PASSWORD='…' RDB_TENANT_CONNECTIONS=50 ./create-tenant.sh
set -euo pipefail
RDB_PG_URL="${RDB_PG_URL:-postgresql://rasterized@127.0.0.1:5432/postgres?sslmode=require}" # the operator database
RDB_TENANT="${RDB_TENANT:-}" # required: the customer's database
RDB_TENANT_OWNER="${RDB_TENANT_OWNER:-}" # required: its administrator role
RDB_TENANT_CONNECTIONS="${RDB_TENANT_CONNECTIONS:-}" # e.g. 50
RDB_TENANT_STORAGE="${RDB_TENANT_STORAGE:-}" # e.g. 20GB
RDB_TENANT_STATEMENT_TIMEOUT="${RDB_TENANT_STATEMENT_TIMEOUT:-}" # e.g. 60s
die() { echo "error: $*" >&2; exit 1; }
command -v psql >/dev/null 2>&1 || die "psql is needed"
[ -n "${PGPASSWORD:-}" ] || die "set PGPASSWORD to the operator's password"
[ -n "$RDB_TENANT" ] && [ -n "$RDB_TENANT_OWNER" ] || die "set RDB_TENANT and RDB_TENANT_OWNER"
: "${RDB_TENANT_PASSWORD:?set RDB_TENANT_PASSWORD to the password of the administrator}"
for v in "$RDB_TENANT" "$RDB_TENANT_OWNER"; do
[[ "$v" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] || die "$v: use letters, digits and _ only"
done
case "$RDB_TENANT_PASSWORD$RDB_TENANT_STORAGE$RDB_TENANT_STATEMENT_TIMEOUT" in *"'"*) die "values may not contain a single quote" ;; esac
limits=""
[ -z "$RDB_TENANT_CONNECTIONS" ] || limits="${limits:+$limits, }connections = $RDB_TENANT_CONNECTIONS"
[ -z "$RDB_TENANT_STORAGE" ] || limits="${limits:+$limits, }storage = '$RDB_TENANT_STORAGE'"
[ -z "$RDB_TENANT_STATEMENT_TIMEOUT" ] || limits="${limits:+$limits, }statement_timeout = '$RDB_TENANT_STATEMENT_TIMEOUT'"
# On standard input, so the password does not appear in the process list.
statement="CREATE TENANT $RDB_TENANT OWNER $RDB_TENANT_OWNER PASSWORD '$RDB_TENANT_PASSWORD'${limits:+ LIMITS ($limits)};"
printf '%s\n' "$statement" | psql "$RDB_PG_URL" -v ON_ERROR_STOP=1 -X -q
psql "$RDB_PG_URL" -X -c "SHOW TENANTS"
host="$(printf '%s' "$RDB_PG_URL" | sed -E 's#^[a-z]+://([^@/]*@)?([^/?]*).*#\2#')"
echo "The customer connects with: postgresql://$RDB_TENANT_OWNER@$host/$RDB_TENANT?sslmode=require"#Requires -Version 5.1
<#
.SYNOPSIS
RasterizedDB Enterprise: add a customer to a server in strict tenant mode.
Creates the database -Tenant with only its administrator -Owner in it, and
records its limits in the tenant directory.
Needs release 1.1.0 or later.
.DESCRIPTION
The operator's password comes from $env:PGPASSWORD and the administrator's
from $env:RDB_TENANT_PASSWORD, never from the command line.
.EXAMPLE
$env:PGPASSWORD = '<operator password>'; $env:RDB_TENANT_PASSWORD = '…'
.\create-tenant.ps1 -Tenant acme -Owner acme_admin -Connections 50 -Storage 20GB -StatementTimeout 60s
#>
[CmdletBinding()]
param(
# The operator database.
[string]$PgUrl = $(if ($env:RDB_PG_URL) { $env:RDB_PG_URL } else { 'postgresql://rasterized@127.0.0.1:5432/postgres?sslmode=require' }),
[Parameter(Mandatory = $true)][string]$Tenant,
[Parameter(Mandatory = $true)][string]$Owner,
[int]$Connections = 0,
# For example 20GB.
[string]$Storage = '',
# For example 60s.
[string]$StatementTimeout = ''
)
$ErrorActionPreference = 'Stop'
if (-not (Get-Command psql -ErrorAction SilentlyContinue)) { throw 'psql is needed.' }
if (-not $env:PGPASSWORD) { throw "Set `$env:PGPASSWORD to the operator's password." }
if (-not $env:RDB_TENANT_PASSWORD) { throw "Set `$env:RDB_TENANT_PASSWORD to the administrator's password." }
foreach ($v in @($Tenant, $Owner)) {
if ($v -notmatch '^[A-Za-z_][A-Za-z0-9_]*$') { throw "${v}: use letters, digits and _ only." }
}
if (($env:RDB_TENANT_PASSWORD + $Storage + $StatementTimeout).Contains("'")) { throw 'Values may not contain a single quote.' }
$limits = @()
if ($Connections -gt 0) { $limits += "connections = $Connections" }
if ($Storage) { $limits += "storage = '$Storage'" }
if ($StatementTimeout) { $limits += "statement_timeout = '$StatementTimeout'" }
$statement = "CREATE TENANT $Tenant OWNER $Owner PASSWORD '$($env:RDB_TENANT_PASSWORD)'"
if ($limits) { $statement += " LIMITS ($($limits -join ', '))" }
# On standard input, so the password does not appear in the process list.
"$statement;" | & psql $PgUrl -v ON_ERROR_STOP=1 -X -q
if ($LASTEXITCODE -ne 0) { throw 'CREATE TENANT failed; the error is above.' }
& psql $PgUrl -X -c 'SHOW TENANTS'
$hostPart = $PgUrl -replace '^[a-z]+://([^@/]*@)?([^/?]*).*$', '$2'
Write-Host "The customer connects with: postgresql://$Owner@$hostPart/${Tenant}?sslmode=require"# RasterizedDB enterprise configuration: strict tenant mode.
# One setting per line; '#' starts a comment. Paths should be absolute.
# The setup script writes a file like this one, with the paths filled in.
# One database per customer. Only a superuser connected to the operator
# database administers the process; a tenant's own superuser administers its
# database alone, and other tenants' databases are invisible to it.
tenant_mode strict
# The operator's database: the server's default database (--pg-default-db).
operator_database postgres
# Where CREATE TENANT keeps the tenant directory (rewritten on every change).
tenants_file /var/lib/rasterizeddb-tenants/tenants
# Limits for a database by name, applied at once:
# limit <database> connections <N>
# limit <database> statement_timeout_ms <MS>
# limit <database> max_bytes <BYTES>
# limit acme connections 50
# limit acme statement_timeout_ms 60000
# limit acme max_bytes 21474836480
# Hosts the server may connect out to under strict mode (host, host:port or
# *.suffix). Without this line, nothing is held back.
# outbound_hosts pg.internal:5432 *.s3.eu-central-1.amazonaws.com