Skip to content

Part B. PostgreSQL: install, tune, secure (Phase P2)

Terminal window
sudo apt install -y postgresql postgresql-contrib
psql --version
sudo systemctl status postgresql --no-pager

VERIFY — you should see: psql (PostgreSQL) 17.x on Debian 13, and the service active. (Any version 16 or higher works; tell Claude Code if it is lower.)

Create one drop-in file so the original stays untouched:

Show the code (36 lines)
Terminal window
sudo tee /etc/postgresql/17/main/conf.d/bideward.conf > /dev/null <<'EOF'
# --- network: none. Unix socket only ---
listen_addresses = ''
unix_socket_directories = '/var/run/postgresql'
# --- memory (2 GB box) ---
max_connections = 40
shared_buffers = 512MB
effective_cache_size = 1GB
work_mem = 8MB
maintenance_work_mem = 128MB
huge_pages = off
# --- write path ---
wal_buffers = 16MB
wal_compression = on
checkpoint_timeout = 15min
checkpoint_completion_target = 0.9
max_wal_size = 1GB
min_wal_size = 256MB
synchronous_commit = on
# --- disk is SSD ---
random_page_cost = 1.1
effective_io_concurrency = 200
# --- autovacuum tuned for small, hot tables ---
autovacuum_naptime = 20s
autovacuum_vacuum_scale_factor = 0.05
autovacuum_analyze_scale_factor = 0.05
autovacuum_vacuum_cost_limit = 1000
# --- safety and visibility ---
password_encryption = scram-sha-256
idle_in_transaction_session_timeout = 60s
log_min_duration_statement = 250
log_checkpoints = on
log_lock_waits = on
track_io_timing = on
shared_preload_libraries = 'pg_stat_statements'
EOF

What the important lines do: listen_addresses = ‘’ means PostgreSQL opens no network port at all; programs on the box use the socket file. synchronous_commit stays on by default; the hub turns it off only for the background flush of non-critical keys (Part E.4). The autovacuum lines keep the small, constantly updated state table from bloating.

Program Planned use Limit set
PostgreSQL (buffers plus connections) about 600 MB none (tuned above)
Bideward hub (Node, cache) about 400 to 500 MB MemoryMax 900M in systemd
Research service about 100 to 150 MB MemoryMax 400M
cloudflared about 40 MB MemoryMax 200M
System and cache about 300 MB
Swap 1 GB safety only

If Part I shows swap in steady use or the hub near its limit, upgrade to 4 GB (a Lightsail resize keeps data and needs a short stop).

Terminal window
sudo tee /etc/postgresql/17/main/pg_hba.conf > /dev/null <<'EOF'
local all postgres peer
local bideward all peer map=bidewardmap
local all all reject
host all all 0.0.0.0/0 reject
host all all ::/0 reject
EOF
sudo tee -a /etc/postgresql/17/main/pg_ident.conf > /dev/null <<'EOF'
bidewardmap bideward-hub bideward_hub
bidewardmap bideward-research bideward_research
bidewardmap admin bideward_dba
bidewardmap admin bideward_ro
EOF
sudo systemctl restart postgresql
sudo -u postgres psql -c "show listen_addresses;" -c "show shared_buffers;"

What this says: a program may connect only through the socket and only if the box user it runs as is allowed to act as that database role. No password exists to steal. VERIFY — you should see: an empty listen_addresses and 512MB.

B.5 Create the database, roles and schemas

Section titled “B.5 Create the database, roles and schemas”
Show the code (31 lines)
sudo -u postgres psql <<'EOF'
create role bideward_dba login;
create role bideward_hub login;
create role bideward_research login;
create role bideward_ro login;
create role bideward_owner nologin; -- used by the research schema's row rules
create database bideward owner bideward_dba template template0 encoding 'UTF8';
revoke all on database bideward from public;
grant connect on database bideward to bideward_hub, bideward_research, bideward_ro;
\c bideward
create extension if not exists citext;
create extension if not exists pgcrypto;
create extension if not exists pg_stat_statements;
revoke all on schema public from public;
create schema hub authorization bideward_dba;
create schema research authorization bideward_dba;
grant usage on schema hub to bideward_hub, bideward_ro;
grant usage on schema research to bideward_research, bideward_ro;
alter default privileges for role bideward_dba in schema hub grant select, insert, update, delete on tables to bideward_hub;
alter default privileges for role bideward_dba in schema hub grant usage, select on sequences to bideward_hub;
alter default privileges for role bideward_dba in schema hub grant select on tables to bideward_ro;
alter default privileges for role bideward_dba in schema research grant select, insert, update, delete on tables to bideward_research;
alter default privileges for role bideward_dba in schema research grant usage, select on sequences to bideward_research;
alter default privileges for role bideward_dba in schema research grant select on tables to bideward_ro;
alter role bideward_hub set search_path = hub;
alter role bideward_hub set statement_timeout = '15s';
alter role bideward_research set search_path = research;
alter role bideward_research set statement_timeout = '30s';
alter role bideward_ro set statement_timeout = '30s';
alter role bideward_ro set default_transaction_read_only = on;
EOF

EXPLAIN — what this does: bideward_dba owns the objects and runs the migrations (used only by you and Claude Code over Tailscale). bideward_hub is what the receiver uses: it can read and write rows in schema hub and cannot change table structure and cannot see schema research. bideward_research is the research service’s login for schema research only. bideward_ro can only read (for analysis and the dashboard’s heavy reports). The research service’s own tables keep their names (research_bots and so on) and now live in schema research.

The build (Part E) checks research/schema.mjs and lists any further role statement it needs; roles are always created here, never by a service.

Terminal window
sudo -u bideward-hub psql -U bideward_hub -d bideward -c "select current_user, current_schema();"
sudo -u bideward-research psql -U bideward_research -d bideward -c "select current_user, current_schema();"
psql -U bideward_dba -d bideward -c "select current_user;"

VERIFY — you should see: each command prints its own role (bideward_hub / hub, bideward_research / research, bideward_dba). If you see “peer authentication failed” the pg_ident line for that user is wrong; check B.4.


← Previous: Part A. Prepare and harden the Lightsail box (Phase P1) · Guide index · Next: Part C. The complete database design →