Every protocol from one server
Runs one server that PostgreSQL, MySQL/MariaDB, SQL Server and Redis clients can all connect to, plus the native protocol, on the network and over TLS. Every protocol reaches the same databases and logs in as the same roles, so a table written through one is read through the others. Use it when applications written for different databases share one store. It runs in the foreground.
Before you start
- The server binary (see the install recipe).
- A certificate and key in PEM format for the host name clients use (the tls-certificates recipe makes them). Without one, every listener makes a self-signed certificate at start, which clients can encrypt to but cannot verify.
- A password for the role
rasterizedinRASTERIZEDDB_PG_PASSWORD. - Free ports: 5432, 61170, 3306, 1433 and 6379 by default. A port already used by a real PostgreSQL, MySQL, SQL Server or Redis on the same machine must be moved or turned off.
Settings
| bash variable | PowerShell parameter | Default | What it does |
|---|---|---|---|
RDB_BIN | -RdbBin | rasterizeddb / rasterizeddb.exe | The server binary. |
RDB_DATA | -Data | ./data | The database directory. |
RDB_LISTEN | -Listen | 0.0.0.0 | The address every listener binds. 127.0.0.1 keeps them to this machine. |
RDB_TLS_CERT, RDB_TLS_KEY | -CertPath, -KeyPath | none | The certificate and key. Given to the PostgreSQL and native listeners; MySQL, SQL Server and Redis use the PostgreSQL one. |
RDB_PG_PORT | -PgPort | 5432 | PostgreSQL. TLS is required. |
RDB_PORT | -Port | 61170 | Native protocol. Always TLS over TCP. |
RDB_MYSQL_PORT | -MysqlPort | 3306 | MySQL / MariaDB. off (bash) or 0 (PowerShell) leaves it closed. |
RDB_MSSQL_PORT | -MssqlPort | 1433 | SQL Server (TDS). off / 0 leaves it closed. |
RDB_KV_PORT | -KvPort | 6379 | Redis. TLS is required on it (--kv-tls on). off / 0 leaves it closed. |
RDB_LOG_LEVEL | -LogLevel | warn | Log level. |
RASTERIZEDDB_PG_PASSWORD | $env:RASTERIZEDDB_PG_PASSWORD | required | The role's password, used when the database is first created. |
Run it
export RASTERIZEDDB_PG_PASSWORD='use-a-long-secret'
RDB_TLS_CERT=./tls/server.pem RDB_TLS_KEY=./tls/server.key ./setup.sh$env:RASTERIZEDDB_PG_PASSWORD = 'use-a-long-secret'
.\setup.ps1 -CertPath .\tls\server.pem -KeyPath .\tls\server.keyThe script prints one connection command per protocol before the server starts. On Windows, also allow the ports in Windows Firewall for clients on other machines.
Check that it works
Write a row over PostgreSQL and read it over the others (replace db.example.com with your host):
export PGPASSWORD='use-a-long-secret'
psql "postgresql://rasterized@db.example.com:5432/postgres?sslmode=require" \
-c "CREATE TABLE greetings (id INT PRIMARY KEY, msg TEXT)" \
-c "INSERT INTO greetings VALUES (1, 'hello from postgres')"
mysql -h db.example.com -P 3306 -u rasterized -p --ssl-mode=REQUIRED postgres -e "SELECT msg FROM greetings"
sqlcmd -S db.example.com,1433 -U rasterized -d postgres -C -Q "SELECT msg FROM greetings"
redis-cli -h db.example.com -p 6379 --tls --insecure --askpass PINGsqlcmd -C trusts the server's certificate without checking it; with a certificate from your own authority, install its ca.pem as a trusted root instead. redis-cli --insecure does the same; --cacert ca.pem checks it.
Stop or undo
Ctrl+C stops the server cleanly. To keep it running in the background and across reboots, use the systemd-service or windows-service recipe and put these flags in RDB_ARGS / -ExtraArgs.
Limits
- A MySQL client's first login for a role must come over TLS: there is no RSA key exchange, so a first login over plain TCP is refused. Use
--ssl-mode=REQUIRED(or your driver's equivalent). - The SQL Server listener supports SQL Server authentication only, not Windows authentication.
- The Redis port is a key/value cache beside the tables, not a view of them; see the kv-cache-only recipe.
The files
#!/usr/bin/env bash
# RasterizedDB: one server speaking every client protocol at once, on the
# network, with TLS: PostgreSQL, the native protocol, MySQL, SQL Server and
# Redis, all on the same databases and roles. Runs in the foreground.
#
# Every setting can be overridden from the environment; set a port to "off"
# to leave that protocol closed, for example:
# RDB_MSSQL_PORT=off RDB_TLS_CERT=./tls/server.pem RDB_TLS_KEY=./tls/server.key ./setup.sh
set -euo pipefail
RDB_BIN="${RDB_BIN:-rasterizeddb}"
RDB_DATA="${RDB_DATA:-./data}"
RDB_LISTEN="${RDB_LISTEN:-0.0.0.0}"
RDB_TLS_CERT="${RDB_TLS_CERT:-}" # PEM certificate; without one each listener makes a self-signed one
RDB_TLS_KEY="${RDB_TLS_KEY:-}"
RDB_PG_PORT="${RDB_PG_PORT:-5432}"
RDB_PORT="${RDB_PORT:-61170}"
RDB_MYSQL_PORT="${RDB_MYSQL_PORT:-3306}"
RDB_MSSQL_PORT="${RDB_MSSQL_PORT:-1433}"
RDB_KV_PORT="${RDB_KV_PORT:-6379}"
RDB_LOG_LEVEL="${RDB_LOG_LEVEL:-warn}"
command -v "$RDB_BIN" >/dev/null 2>&1 || { echo "error: '$RDB_BIN' not found; set RDB_BIN to the server binary" >&2; exit 1; }
: "${RASTERIZEDDB_PG_PASSWORD:?set RASTERIZEDDB_PG_PASSWORD to the password for the role rasterized}"
export RASTERIZEDDB_PG_PASSWORD
mkdir -p "$RDB_DATA"
RDB_DATA="$(cd "$RDB_DATA" && pwd)"
args=(--location "$RDB_DATA" --protocol both --log-level "$RDB_LOG_LEVEL"
--pg-addr "$RDB_LISTEN" --pg-port "$RDB_PG_PORT" --pg-tls require
--rdb-addr "$RDB_LISTEN" --rdb-port "$RDB_PORT")
if [ -n "$RDB_TLS_CERT" ]; then
[ -f "$RDB_TLS_CERT" ] && [ -f "$RDB_TLS_KEY" ] || { echo "error: RDB_TLS_CERT and RDB_TLS_KEY must both be files" >&2; exit 1; }
# MySQL, SQL Server and Redis take the PostgreSQL certificate unless given their own.
args+=(--pg-cert "$RDB_TLS_CERT" --pg-key "$RDB_TLS_KEY" --rdb-cert "$RDB_TLS_CERT" --rdb-key "$RDB_TLS_KEY")
else
echo "warning: no RDB_TLS_CERT; every listener uses a self-signed certificate clients cannot verify" >&2
fi
[ "$RDB_MYSQL_PORT" = off ] || args+=(--mysql-addr "$RDB_LISTEN" --mysql-port "$RDB_MYSQL_PORT" --mysql-tls prefer)
[ "$RDB_MSSQL_PORT" = off ] || args+=(--mssql-addr "$RDB_LISTEN" --mssql-port "$RDB_MSSQL_PORT" --mssql-tls prefer)
[ "$RDB_KV_PORT" = off ] || args+=(--kv-addr "$RDB_LISTEN" --kv-port "$RDB_KV_PORT" --kv-tls on)
host="$(hostname -f 2>/dev/null || hostname)"
echo "PostgreSQL: psql \"postgresql://rasterized@$host:$RDB_PG_PORT/postgres?sslmode=require\""
echo "Native: $host:$RDB_PORT (rasterizeddb_client, user rasterized)"
[ "$RDB_MYSQL_PORT" = off ] || echo "MySQL: mysql -h $host -P $RDB_MYSQL_PORT -u rasterized -p --ssl-mode=REQUIRED postgres"
[ "$RDB_MSSQL_PORT" = off ] || echo "SQL Server: sqlcmd -S $host,$RDB_MSSQL_PORT -U rasterized -d postgres -C"
[ "$RDB_KV_PORT" = off ] || echo "Redis: redis-cli -h $host -p $RDB_KV_PORT --tls --insecure --askpass PING"
echo "Every protocol logs in as the same roles. Stop with Ctrl+C."
echo
exec "$RDB_BIN" "${args[@]}"#Requires -Version 5.1
<#
.SYNOPSIS
RasterizedDB: one server speaking every client protocol at once, on the
network, with TLS: PostgreSQL, the native protocol, MySQL, SQL Server and
Redis, all on the same databases and roles. Runs in the foreground.
.EXAMPLE
$env:RASTERIZEDDB_PG_PASSWORD = 'use-a-long-secret'
.\setup.ps1 -CertPath .\tls\server.pem -KeyPath .\tls\server.key
.EXAMPLE
.\setup.ps1 -MssqlPort 0 # 0 leaves a protocol closed
#>
[CmdletBinding()]
param(
[string]$RdbBin = $(if ($env:RDB_BIN) { $env:RDB_BIN } else { 'rasterizeddb.exe' }),
[string]$Data = $(if ($env:RDB_DATA) { $env:RDB_DATA } else { '.\data' }),
[string]$Listen = $(if ($env:RDB_LISTEN) { $env:RDB_LISTEN } else { '0.0.0.0' }),
# PEM certificate and key; without them each listener makes a self-signed one.
[string]$CertPath = $env:RDB_TLS_CERT,
[string]$KeyPath = $env:RDB_TLS_KEY,
[int]$PgPort = $(if ($env:RDB_PG_PORT) { [int]$env:RDB_PG_PORT } else { 5432 }),
[int]$Port = $(if ($env:RDB_PORT) { [int]$env:RDB_PORT } else { 61170 }),
[int]$MysqlPort = $(if ($env:RDB_MYSQL_PORT) { [int]$env:RDB_MYSQL_PORT } else { 3306 }),
[int]$MssqlPort = $(if ($env:RDB_MSSQL_PORT) { [int]$env:RDB_MSSQL_PORT } else { 1433 }),
[int]$KvPort = $(if ($env:RDB_KV_PORT) { [int]$env:RDB_KV_PORT } else { 6379 }),
[string]$LogLevel = $(if ($env:RDB_LOG_LEVEL) { $env:RDB_LOG_LEVEL } else { 'warn' })
)
$ErrorActionPreference = 'Stop'
$cmd = Get-Command $RdbBin -ErrorAction SilentlyContinue
if (-not $cmd) { throw "'$RdbBin' not found; pass -RdbBin with the path of rasterizeddb.exe." }
if (-not $env:RASTERIZEDDB_PG_PASSWORD) { throw 'Set $env:RASTERIZEDDB_PG_PASSWORD to the password for the role rasterized.' }
New-Item -ItemType Directory -Force -Path $Data | Out-Null
$Data = (Resolve-Path -LiteralPath $Data).Path
$serverArgs = @('--location', $Data, '--protocol', 'both', '--log-level', $LogLevel,
'--pg-addr', $Listen, '--pg-port', "$PgPort", '--pg-tls', 'require',
'--rdb-addr', $Listen, '--rdb-port', "$Port")
if ($CertPath) {
if (-not (Test-Path -LiteralPath $CertPath) -or -not (Test-Path -LiteralPath $KeyPath)) { throw 'CertPath and KeyPath must both be files.' }
$CertPath = (Resolve-Path -LiteralPath $CertPath).Path
$KeyPath = (Resolve-Path -LiteralPath $KeyPath).Path
# MySQL, SQL Server and Redis take the PostgreSQL certificate unless given their own.
$serverArgs += @('--pg-cert', $CertPath, '--pg-key', $KeyPath, '--rdb-cert', $CertPath, '--rdb-key', $KeyPath)
}
else {
Write-Warning 'No -CertPath: every listener uses a self-signed certificate clients cannot verify.'
}
if ($MysqlPort -gt 0) { $serverArgs += @('--mysql-addr', $Listen, '--mysql-port', "$MysqlPort", '--mysql-tls', 'prefer') }
if ($MssqlPort -gt 0) { $serverArgs += @('--mssql-addr', $Listen, '--mssql-port', "$MssqlPort", '--mssql-tls', 'prefer') }
if ($KvPort -gt 0) { $serverArgs += @('--kv-addr', $Listen, '--kv-port', "$KvPort", '--kv-tls', 'on') }
$h = [System.Net.Dns]::GetHostEntry('').HostName
Write-Host "PostgreSQL: psql `"postgresql://rasterized@${h}:$PgPort/postgres?sslmode=require`""
Write-Host "Native: ${h}:$Port (rasterizeddb_client, user rasterized)"
if ($MysqlPort -gt 0) { Write-Host "MySQL: mysql -h $h -P $MysqlPort -u rasterized -p --ssl-mode=REQUIRED postgres" }
if ($MssqlPort -gt 0) { Write-Host "SQL Server: sqlcmd -S $h,$MssqlPort -U rasterized -d postgres -C" }
if ($KvPort -gt 0) { Write-Host "Redis: redis-cli -h $h -p $KvPort --tls --insecure --askpass PING" }
Write-Host 'Every protocol logs in as the same roles. Open these ports in Windows Firewall for remote clients. Stop with Ctrl+C.'
Write-Host ''
& $cmd.Source @serverArgs
exit $LASTEXITCODE