Unix sockets only
Serves applications on the same machine over unix sockets, with no TCP port open at all. PostgreSQL clients connect to the socket directory and log in with a password. Native clients are let in by the operating-system user they run as (peer): the kernel reports who is connecting, and a role with that name is logged in without a password. File permissions decide who may connect at all. It runs in the foreground.
There is no PowerShell version: the server offers no unix sockets on Windows. There, listen on 127.0.0.1 (the local-dev recipe) to keep connections on the machine.
Before you start
- Linux and the server binary (see the install recipe).
- A short socket directory. A socket's whole path, file name included, must fit in 107 bytes.
- To share the sockets with an application that runs as another user, a group both users belong to.
Settings
| Variable | Default | What it does |
|---|---|---|
RDB_BIN | rasterizeddb | The server binary. |
RDB_DATA | ./data | The database directory. |
RDB_SOCKET_DIR | $XDG_RUNTIME_DIR/rasterizeddb, else /tmp/rasterizeddb | Holds .s.PGSQL.<port> (PostgreSQL), rdb.sock (native) and kv.sock (Redis, when on). |
RDB_SOCKET_GROUP | none | A group whose members may connect. Sets the sockets to mode 660 and that group; without it they are 600, this user only. The server's user must belong to the group. |
RDB_PG_PORT | 5432 | Only names the PostgreSQL socket: clients pass the same port. |
RDB_PG_SOCKET_AUTH | scram-sha-256 | PostgreSQL socket login: scram-sha-256, md5, password, peer or trust. |
RDB_NATIVE_SOCKET_AUTH | peer | Native socket login: peer, scram-sha-256 or trust. |
RDB_KV_SOCKET | 0 | 1 also serves the Redis protocol on kv.sock. |
RDB_LOG_LEVEL | warn | Log level. |
RASTERIZEDDB_PG_PASSWORD | required | Password of the role rasterized, used when the database is first created. |
The server runs with --pg-tcp off and --rdb-tcp off, so neither protocol opens a port. trust is refused on a socket that users outside its owner and group can reach.
Run it
export RASTERIZEDDB_PG_PASSWORD='use-a-long-secret'
./setup.sh
# shared with the members of the group app:
RDB_SOCKET_DIR=/run/rasterizeddb RDB_SOCKET_GROUP=app ./setup.sh/run is writable only by root; create the directory first (sudo install -d -o "$USER" -g app /run/rasterizeddb).
Check that it works
Over the PostgreSQL socket, with the password:
PGPASSWORD='use-a-long-secret' psql "host=$XDG_RUNTIME_DIR/rasterizeddb port=5432 dbname=postgres user=rasterized" -c "SELECT 1"Peer login on the native socket needs a role named after the operating-system user. Create one for the user your application runs as (here app), then grant it what it needs:
PGPASSWORD='use-a-long-secret' psql "host=$XDG_RUNTIME_DIR/rasterizeddb port=5432 dbname=postgres user=rasterized" \
-c "CREATE USER app"A process running as app then connects with rasterizeddb_client to the address unix:<socket dir>/rdb.sock using Login::Peer, with no password. Confirm that nothing listens on TCP:
ss -ltnp | grep rasterizeddb || echo "no TCP listener"Stop or undo
Ctrl+C stops the server cleanly. A socket file left behind by a server that did not shut down is replaced at the next start; a path that another server is listening on, or that is not a socket, is refused.
Limits
- No TLS on unix sockets. Only root can watch a local socket, so none is needed.
- Under the systemd-service recipe, a socket directory outside
/var/lib/rasterizeddbneedsReadWritePaths=in a drop-in.
The files
#!/usr/bin/env bash
# RasterizedDB: serve applications on this machine over unix sockets only,
# with no TCP port open. PostgreSQL clients log in with a password; native
# clients are let in by the operating-system user they run as (peer).
# Runs in the foreground; Ctrl+C stops it cleanly.
#
# Every setting can be overridden from the environment, for example:
# RDB_SOCKET_DIR=/run/rasterizeddb RDB_SOCKET_GROUP=app ./setup.sh
set -euo pipefail
RDB_BIN="${RDB_BIN:-rasterizeddb}"
RDB_DATA="${RDB_DATA:-./data}"
RDB_SOCKET_DIR="${RDB_SOCKET_DIR:-${XDG_RUNTIME_DIR:-/tmp}/rasterizeddb}"
RDB_SOCKET_GROUP="${RDB_SOCKET_GROUP:-}" # a group whose members may connect too
RDB_PG_PORT="${RDB_PG_PORT:-5432}" # names the PostgreSQL socket: .s.PGSQL.<port>
RDB_PG_SOCKET_AUTH="${RDB_PG_SOCKET_AUTH:-scram-sha-256}"
RDB_NATIVE_SOCKET_AUTH="${RDB_NATIVE_SOCKET_AUTH:-peer}"
RDB_KV_SOCKET="${RDB_KV_SOCKET:-0}" # 1 also serves Redis on <dir>/kv.sock
RDB_LOG_LEVEL="${RDB_LOG_LEVEL:-warn}"
die() { echo "error: $*" >&2; exit 1; }
command -v "$RDB_BIN" >/dev/null 2>&1 || die "'$RDB_BIN' not found; set RDB_BIN to the server binary"
: "${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)"
mkdir -p "$RDB_SOCKET_DIR"
RDB_SOCKET_DIR="$(cd "$RDB_SOCKET_DIR" && pwd)"
pg_socket="$RDB_SOCKET_DIR/.s.PGSQL.$RDB_PG_PORT"
native_socket="$RDB_SOCKET_DIR/rdb.sock"
[ "${#pg_socket}" -le 107 ] || die "$pg_socket is longer than a socket address allows (107 bytes); use a shorter RDB_SOCKET_DIR"
if [ -n "$RDB_SOCKET_GROUP" ]; then
mode=660
group=(--socket-group "$RDB_SOCKET_GROUP")
chgrp "$RDB_SOCKET_GROUP" "$RDB_SOCKET_DIR"
chmod 0750 "$RDB_SOCKET_DIR"
else
mode=600
group=()
chmod 0700 "$RDB_SOCKET_DIR"
fi
args=(--location "$RDB_DATA" --protocol both --log-level "$RDB_LOG_LEVEL"
--pg-port "$RDB_PG_PORT" --pg-socket-dir "$RDB_SOCKET_DIR" --pg-socket-auth "$RDB_PG_SOCKET_AUTH" --pg-tcp off
--rdb-socket "$native_socket" --rdb-socket-auth "$RDB_NATIVE_SOCKET_AUTH" --rdb-tcp off
--socket-mode "$mode" ${group[@]+"${group[@]}"})
if [ "$RDB_KV_SOCKET" = 1 ]; then
args+=(--kv-socket "$RDB_SOCKET_DIR/kv.sock")
fi
echo "PostgreSQL: psql \"host=$RDB_SOCKET_DIR port=$RDB_PG_PORT dbname=postgres user=rasterized\""
echo "Native: unix:$native_socket ($RDB_NATIVE_SOCKET_AUTH: a role named after the connecting OS user)"
[ "$RDB_KV_SOCKET" != 1 ] || echo "Redis: redis-cli -s $RDB_SOCKET_DIR/kv.sock --askpass PING"
echo "Socket mode $mode${RDB_SOCKET_GROUP:+, group $RDB_SOCKET_GROUP}. No TCP port is open. Stop with Ctrl+C."
echo
exec "$RDB_BIN" "${args[@]}"