A local development server
Starts one server for development on your own machine: PostgreSQL on 127.0.0.1:5432 and the native protocol on 127.0.0.1:61170, without TLS, reachable from this machine only. It runs in the foreground until you press Ctrl+C.
Before you start
- The server binary, from the install script, on
PATHor named byRDB_BIN/-RdbBin. - Ports 5432 and 61170 free. If a PostgreSQL server already uses 5432, pick another port with
RDB_PG_PORT. - A PostgreSQL client such as
psql, or therasterizeddb_clientcrate for the native protocol.
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. memory keeps everything in memory and writes no files. |
RDB_PG_PORT | -PgPort | 5432 | PostgreSQL port. |
RDB_PORT | -Port | 61170 | Native protocol port. |
RDB_USER | -User | rasterized | The role created on the first start. |
RDB_LOG_LEVEL | -LogLevel | info | off, error, warn, info, debug or trace. |
RASTERIZEDDB_PG_PASSWORD | $env:RASTERIZEDDB_PG_PASSWORD | made up and printed | The role's password. |
The password is read from the environment, never passed on the command line, where other users could see it in the process list. It only sets the password when the data directory is new. An existing directory keeps its roles: change a password there with ALTER USER rasterized PASSWORD '…'.
Run it
export RASTERIZEDDB_PG_PASSWORD='change-me'
./setup.sh$env:RASTERIZEDDB_PG_PASSWORD = 'change-me'
.\setup.ps1If PowerShell refuses to run a downloaded script, run Unblock-File .\setup.ps1 first.
The script prints the connection string before the server starts:
PostgreSQL: postgresql://rasterized:change-me@127.0.0.1:5432/postgres?sslmode=disable
Native: 127.0.0.1:61170 (user rasterized, same password)Check that it works
In a second terminal:
psql "postgresql://rasterized:change-me@127.0.0.1:5432/postgres?sslmode=disable" \
-c "CREATE TABLE notes (id INT PRIMARY KEY, body TEXT)" \
-c "INSERT INTO notes VALUES (1, 'hello')" \
-c "SELECT * FROM notes" id | body
----+-------
1 | hello
(1 row)Stop or undo
Press Ctrl+C in the server's terminal. The server finishes what it is doing, writes a checkpoint and exits. To start over with an empty database, stop it and delete the data directory.
Limits
- No TLS and no network access: this is for one developer's machine. See the production-single-node recipe for a server other machines can reach.
- With
RDB_DATA=memoryeverything, the roles included, is gone when the process exits.
The files
#!/usr/bin/env bash
# RasterizedDB: a server for development on this machine only. PostgreSQL on
# 127.0.0.1:5432 and the native protocol on 127.0.0.1:61170, no TLS.
# Runs in the foreground; Ctrl+C stops it cleanly.
#
# Every setting can be overridden from the environment, for example:
# RDB_PG_PORT=55432 ./setup.sh
set -euo pipefail
RDB_BIN="${RDB_BIN:-rasterizeddb}"
RDB_DATA="${RDB_DATA:-./data}" # the database directory; RDB_DATA=memory keeps everything in memory
RDB_PG_PORT="${RDB_PG_PORT:-5432}"
RDB_PORT="${RDB_PORT:-61170}"
RDB_USER="${RDB_USER:-rasterized}"
RDB_LOG_LEVEL="${RDB_LOG_LEVEL:-info}"
command -v "$RDB_BIN" >/dev/null 2>&1 || {
echo "error: '$RDB_BIN' not found; install it (see the install script) or set RDB_BIN to its path" >&2
exit 1
}
# The first start creates the role RDB_USER with this password. Set
# RASTERIZEDDB_PG_PASSWORD yourself, or one is made up and printed.
if [ -z "${RASTERIZEDDB_PG_PASSWORD:-}" ]; then
RASTERIZEDDB_PG_PASSWORD="dev-$(od -An -N6 -tx1 /dev/urandom | tr -d ' \n')"
echo "RASTERIZEDDB_PG_PASSWORD was not set; using $RASTERIZEDDB_PG_PASSWORD"
echo "(it only takes effect on a new data directory; an existing one keeps its role's password)"
fi
export RASTERIZEDDB_PG_PASSWORD
if [ "$RDB_DATA" = memory ]; then
where=(--in-memory)
else
mkdir -p "$RDB_DATA"
RDB_DATA="$(cd "$RDB_DATA" && pwd)"
where=(--location "$RDB_DATA")
fi
echo
echo "PostgreSQL: postgresql://$RDB_USER:$RASTERIZEDDB_PG_PASSWORD@127.0.0.1:$RDB_PG_PORT/postgres?sslmode=disable"
echo "Native: 127.0.0.1:$RDB_PORT (user $RDB_USER, same password)"
echo "Stop with Ctrl+C."
echo
exec "$RDB_BIN" "${where[@]}" \
--protocol both \
--pg-addr 127.0.0.1 --pg-port "$RDB_PG_PORT" --pg-tls disable --pg-user "$RDB_USER" \
--rdb-addr 127.0.0.1 --rdb-port "$RDB_PORT" \
--log-level "$RDB_LOG_LEVEL"#Requires -Version 5.1
<#
.SYNOPSIS
RasterizedDB: a server for development on this machine only. PostgreSQL on
127.0.0.1:5432 and the native protocol on 127.0.0.1:61170, no TLS.
Runs in the foreground; Ctrl+C stops it cleanly.
.EXAMPLE
.\setup.ps1
.\setup.ps1 -Data memory -PgPort 55432
#>
[CmdletBinding()]
param(
[string]$RdbBin = $(if ($env:RDB_BIN) { $env:RDB_BIN } else { 'rasterizeddb.exe' }),
# The database directory; 'memory' keeps everything in memory.
[string]$Data = $(if ($env:RDB_DATA) { $env:RDB_DATA } else { '.\data' }),
[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 }),
[string]$User = $(if ($env:RDB_USER) { $env:RDB_USER } else { 'rasterized' }),
[string]$LogLevel = $(if ($env:RDB_LOG_LEVEL) { $env:RDB_LOG_LEVEL } else { 'info' })
)
$ErrorActionPreference = 'Stop'
$cmd = Get-Command $RdbBin -ErrorAction SilentlyContinue
if (-not $cmd) { throw "'$RdbBin' not found; install it (see the install script) or pass -RdbBin with its path." }
# The first start creates the role $User with this password. Set
# $env:RASTERIZEDDB_PG_PASSWORD yourself, or one is made up and printed.
if (-not $env:RASTERIZEDDB_PG_PASSWORD) {
$env:RASTERIZEDDB_PG_PASSWORD = 'dev-' + (-join (1..12 | ForEach-Object { '{0:x}' -f (Get-Random -Maximum 16) }))
Write-Host "RASTERIZEDDB_PG_PASSWORD was not set; using $env:RASTERIZEDDB_PG_PASSWORD"
Write-Host '(it only takes effect on a new data directory; an existing one keeps its role''s password)'
}
if ($Data -eq 'memory') {
$where = @('--in-memory')
}
else {
New-Item -ItemType Directory -Force -Path $Data | Out-Null
$Data = (Resolve-Path -LiteralPath $Data).Path
$where = @('--location', $Data)
}
$serverArgs = $where + @(
'--protocol', 'both',
'--pg-addr', '127.0.0.1', '--pg-port', "$PgPort", '--pg-tls', 'disable', '--pg-user', $User,
'--rdb-addr', '127.0.0.1', '--rdb-port', "$Port",
'--log-level', $LogLevel
)
Write-Host ''
Write-Host "PostgreSQL: postgresql://${User}:$($env:RASTERIZEDDB_PG_PASSWORD)@127.0.0.1:$PgPort/postgres?sslmode=disable"
Write-Host "Native: 127.0.0.1:$Port (user $User, same password)"
Write-Host 'Stop with Ctrl+C.'
Write-Host ''
& $cmd.Source @serverArgs
exit $LASTEXITCODE