Run as a systemd service
Installs the server as the systemd service rasterizeddb on Linux: it starts at boot, restarts after a failure, runs as its own unprivileged system user, and keeps its data in /var/lib/rasterizeddb. PostgreSQL (5432) and the native protocol (61170) listen on localhost until you add flags that open them up.
There is no PowerShell version: systemd is Linux only. On Windows, see the windows-service recipe.
Before you start
- A Linux distribution with systemd, and root access (
sudo). - The server binary (see the install recipe). The script copies it to
/usr/local/bin/rasterizeddb. setup.shandrasterizeddb.servicein the same directory.
Settings
| Variable | Default | What it does |
|---|---|---|
RDB_BIN | rasterizeddb | The binary to install. sudo resets PATH, so give a path, such as RDB_BIN=$HOME/.local/bin/rasterizeddb. |
RASTERIZEDDB_PG_PASSWORD | required on the first install | Password of the role rasterized, written to /etc/rasterizeddb/rasterizeddb.env (mode 0600, owned by root). |
RDB_ARGS | empty | Extra server flags, written to the environment file as RASTERIZEDDB_ARGS. |
RDB_SERVICE_USER | rasterizeddb | The system user the service runs as. Created if missing. |
RDB_START | 1 | 0 installs and enables the service without starting it. |
What the script writes:
| File | What it is |
|---|---|
/usr/local/bin/rasterizeddb | The server binary. |
/etc/systemd/system/rasterizeddb.service | The unit, from rasterizeddb.service. |
/etc/rasterizeddb/rasterizeddb.env | The password and RASTERIZEDDB_ARGS. Written once and kept on later runs. |
/var/lib/rasterizeddb | The data directory, created by systemd (StateDirectory=), mode 0700. |
Run it
export RASTERIZEDDB_PG_PASSWORD='use-a-long-secret'
sudo --preserve-env=RASTERIZEDDB_PG_PASSWORD RDB_BIN="$HOME/.local/bin/rasterizeddb" ./setup.shTo have other machines reach it from the start, add the flags on the first install:
sudo --preserve-env=RASTERIZEDDB_PG_PASSWORD RDB_BIN="$HOME/.local/bin/rasterizeddb" \
RDB_ARGS="--pg-addr 0.0.0.0 --rdb-addr 0.0.0.0" ./setup.shThe production-single-node recipe does this with your own TLS certificate.
Check that it works
systemctl status rasterizeddb
journalctl -u rasterizeddb -n 50
PGPASSWORD='use-a-long-secret' psql "postgresql://rasterized@127.0.0.1:5432/postgres?sslmode=require" -c "SELECT 1"The PostgreSQL listener requires TLS by default. Without a certificate of your own the server makes a self-signed one at each start, which sslmode=require accepts.
Change settings
Edit /etc/rasterizeddb/rasterizeddb.env (for example RASTERIZEDDB_ARGS=--pg-addr 0.0.0.0 --log-level info), then:
sudo systemctl restart rasterizeddbThe password in the file creates the role when the database is first made. After that, change it with SQL: ALTER USER rasterized PASSWORD '…'.
A directory outside /var/lib/rasterizeddb that the server must write to, such as a second disk given with --storage-location, needs a drop-in, because the unit makes the rest of the file system read-only:
sudo systemctl edit rasterizeddb[Service]
ReadWritePaths=/mnt/disk2/rasterizeddbStop or undo
sudo systemctl stop rasterizeddb # a clean stop: the server writes a checkpoint first
sudo systemctl disable rasterizeddbTo remove it completely, which deletes the data too:
sudo systemctl disable --now rasterizeddb
sudo rm /etc/systemd/system/rasterizeddb.service
sudo systemctl daemon-reload
sudo rm -r /etc/rasterizeddb /var/lib/rasterizeddb /usr/local/bin/rasterizeddb
sudo userdel rasterizeddbLimits
- The unit gives a stop 120 seconds (
TimeoutStopSec=120) before systemd kills the process. A killed server loses no committed transaction (with the default--synchronous-commit on); the next start replays the log. - The data directory is fixed at
/var/lib/rasterizeddb. To use another, change--locationandStateDirectory=in the unit, or pointReadWritePaths=at it.
The files
#!/usr/bin/env bash
# RasterizedDB: install the server as a systemd service that starts at boot,
# runs as its own system user and keeps its data in /var/lib/rasterizeddb.
# Run as root, from the directory that holds rasterizeddb.service.
#
# Every setting can be overridden from the environment, for example:
# sudo RDB_BIN=./rasterizeddb RASTERIZEDDB_PG_PASSWORD='…' ./setup.sh
set -euo pipefail
RDB_BIN="${RDB_BIN:-rasterizeddb}" # the binary to install
RDB_ARGS="${RDB_ARGS:-}" # extra server flags, written to the environment file on first install
RDB_SERVICE_USER="${RDB_SERVICE_USER:-rasterizeddb}"
RDB_START="${RDB_START:-1}" # 0 installs and enables without starting
die() { echo "error: $*" >&2; exit 1; }
[ "$(id -u)" -eq 0 ] || die "run this as root (sudo)"
command -v systemctl >/dev/null 2>&1 || die "systemd is needed"
here="$(cd "$(dirname "$0")" && pwd)"
[ -f "$here/rasterizeddb.service" ] || die "rasterizeddb.service must be next to this script"
src="$(command -v "$RDB_BIN" 2>/dev/null)" || die "'$RDB_BIN' not found; set RDB_BIN to the server binary"
# The binary, where the unit expects it.
if [ "$(readlink -f "$src")" != "/usr/local/bin/rasterizeddb" ]; then
install -m 0755 "$src" /usr/local/bin/rasterizeddb
echo "Installed /usr/local/bin/rasterizeddb"
fi
# The service user: no login, no home outside the data directory.
if ! id "$RDB_SERVICE_USER" >/dev/null 2>&1; then
nologin="$(command -v nologin || echo /usr/sbin/nologin)"
useradd --system --user-group --home-dir /var/lib/rasterizeddb --no-create-home \
--shell "$nologin" --comment "RasterizedDB server" "$RDB_SERVICE_USER"
echo "Created system user $RDB_SERVICE_USER"
fi
# The environment file: the password and any extra flags. Kept on later runs.
install -d -m 0755 /etc/rasterizeddb
env_file=/etc/rasterizeddb/rasterizeddb.env
if [ ! -f "$env_file" ]; then
: "${RASTERIZEDDB_PG_PASSWORD:?set RASTERIZEDDB_PG_PASSWORD to the password for the role rasterized}"
case "$RASTERIZEDDB_PG_PASSWORD" in
*"'"* | *$'\n'*) die "the password may not contain a single quote or a newline" ;;
esac
umask 077
cat > "$env_file" <<EOF
# RasterizedDB service settings, read by rasterizeddb.service.
# Password of the role rasterized, for both the PostgreSQL protocol (5432)
# and the native protocol (61170). Used when the database is first created.
RASTERIZEDDB_PG_PASSWORD='$RASTERIZEDDB_PG_PASSWORD'
# Further server flags (rasterizeddb --help lists them). Both protocols
# listen on localhost only unless you add, for example:
# RASTERIZEDDB_ARGS=--pg-addr 0.0.0.0 --rdb-addr 0.0.0.0
RASTERIZEDDB_ARGS=$RDB_ARGS
EOF
chmod 0600 "$env_file"
echo "Wrote $env_file"
else
echo "Keeping the existing $env_file"
fi
# The unit, under the service user chosen above.
sed -e "s/^User=.*/User=$RDB_SERVICE_USER/" -e "s/^Group=.*/Group=$RDB_SERVICE_USER/" \
"$here/rasterizeddb.service" > "/etc/systemd/system/rasterizeddb.service"
chmod 0644 "/etc/systemd/system/rasterizeddb.service"
systemctl daemon-reload
if [ "$RDB_START" = 1 ]; then
if systemctl is-active --quiet rasterizeddb; then
systemctl restart rasterizeddb # pick up a new binary or unit
fi
systemctl enable --now rasterizeddb
sleep 2
systemctl --no-pager --lines=5 status rasterizeddb || true
else
systemctl enable rasterizeddb
fi
echo
echo "Service: systemctl status rasterizeddb / journalctl -u rasterizeddb"
echo "PostgreSQL: postgresql://rasterized@127.0.0.1:5432/postgres?sslmode=require"
echo "Native: 127.0.0.1:61170 (user rasterized)"
echo "The password is the one in $env_file."[Unit]
Description=RasterizedDB server
Documentation=https://github.com/milen-denev/rasterizeddb
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=rasterizeddb
Group=rasterizeddb
EnvironmentFile=/etc/rasterizeddb/rasterizeddb.env
ExecStartPre=/bin/sh -c 'test -n "$$RASTERIZEDDB_PG_PASSWORD" || { echo "set RASTERIZEDDB_PG_PASSWORD in /etc/rasterizeddb/rasterizeddb.env" >&2; exit 1; }'
ExecStart=/usr/local/bin/rasterizeddb --location /var/lib/rasterizeddb --protocol both $RASTERIZEDDB_ARGS
StateDirectory=rasterizeddb
StateDirectoryMode=0700
# A clean stop (SIGTERM) ends with a checkpoint: give it time.
KillSignal=SIGTERM
TimeoutStopSec=120
Restart=on-failure
RestartSec=5
LimitNOFILE=1048576
NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
PrivateDevices=yes
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectControlGroups=yes
RestrictSUIDSGID=yes
LockPersonality=yes
# Directories outside /var/lib/rasterizeddb that the server writes to
# (--storage-location, unix sockets) need a drop-in with ReadWritePaths=<dir>.
[Install]
WantedBy=multi-user.target