Part B. PostgreSQL: install, tune, secure (Phase P2)
B.1 Install
Section titled “B.1 Install”sudo apt install -y postgresql postgresql-contribpsql --versionsudo systemctl status postgresql --no-pagerVERIFY — 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.)
B.2 Configuration for a 2 GB box
Section titled “B.2 Configuration for a 2 GB box”Create one drop-in file so the original stays untouched:
Show the code (36 lines)
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 = 40shared_buffers = 512MBeffective_cache_size = 1GBwork_mem = 8MBmaintenance_work_mem = 128MBhuge_pages = off# --- write path ---wal_buffers = 16MBwal_compression = oncheckpoint_timeout = 15mincheckpoint_completion_target = 0.9max_wal_size = 1GBmin_wal_size = 256MBsynchronous_commit = on# --- disk is SSD ---random_page_cost = 1.1effective_io_concurrency = 200# --- autovacuum tuned for small, hot tables ---autovacuum_naptime = 20sautovacuum_vacuum_scale_factor = 0.05autovacuum_analyze_scale_factor = 0.05autovacuum_vacuum_cost_limit = 1000# --- safety and visibility ---password_encryption = scram-sha-256idle_in_transaction_session_timeout = 60slog_min_duration_statement = 250log_checkpoints = onlog_lock_waits = ontrack_io_timing = onshared_preload_libraries = 'pg_stat_statements'EOFWhat 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.
B.3 Memory budget (2 GB)
Section titled “B.3 Memory budget (2 GB)”| 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).
B.4 Who may connect (pg_hba and pg_ident)
Section titled “B.4 Who may connect (pg_hba and pg_ident)”sudo tee /etc/postgresql/17/main/pg_hba.conf > /dev/null <<'EOF'local all postgres peerlocal bideward all peer map=bidewardmaplocal all all rejecthost all all 0.0.0.0/0 rejecthost all all ::/0 rejectEOFsudo tee -a /etc/postgresql/17/main/pg_ident.conf > /dev/null <<'EOF'bidewardmap bideward-hub bideward_hubbidewardmap bideward-research bideward_researchbidewardmap admin bideward_dbabidewardmap admin bideward_roEOFsudo systemctl restart postgresqlsudo -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 rulescreate 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 bidewardcreate 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;EOFEXPLAIN — 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.
B.6 Test each login
Section titled “B.6 Test each login”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 →