PostgreSQL 16 on a 4 GB VPS: The Exact 6 Settings That Stop Disk Bloat, Autovacuum Stalls & Connection Exhaustion
Stock PostgreSQL 16 Docker images ship tuned for a 1998 512 MB workstation (`shared_buffers=128MB`, `work_mem=4MB`) while n8n writes heavy JSONB execution payloads continuously. On a 4 GB VPS, apply 6 targeted overrides (`shared_buffers=512MB`, `effective_cache_size=1536MB`, `work_mem=8MB`, `checkpoint_completion_target=0.9`, `max_connections=60`, `autovacuum_vacuum_scale_factor=0.05`) alongside `EXECUTIONS_DATA_PRUNE=true` so database size stays flat under 400 MB.
1. Why Untuned Postgres Still Gets Sluggish at Week 6
Moving n8n off SQLite onto PostgreSQL 16 eliminates `SQLITE_BUSY` immediately, which lulls many builders into ignoring Postgres forever. Six weeks later, opening the n8n "Executions" tab takes 8 seconds, and `df -h` shows `/var/lib/docker/volumes` eating 28 GB of a 40 GB VPS disk.
The culprit is how PostgreSQL MVCC handles `EXECUTIONS_DATA_PRUNE=true`. Deleting 20,000 old execution rows does NOT shrink files on disk immediately—it marks tuples as dead until `autovacuum` reclaims them for reuse. With Postgres’s default `autovacuum_vacuum_scale_factor = 0.2` (20% of table rows!), a huge `execution_data` table accumulates gigabytes of dead JSONB TOAST bloat before autovacuum even wakes up.
2. The 6 Exact PostgreSQL 16 Overrides for a 4 GB KVM Node
You do NOT want to give Postgres 2 GB of `shared_buffers` on a 4 GB machine running n8n/OpenClaw—that starves Node.js and invites Exit Code 137. Instead, size Postgres deliberately to sit in a tight ~300–600 MB envelope while letting Linux page cache accelerate reads:
| Parameter | Stock Default | 4 GB VPS Tuned | Why It Matters |
|---|---|---|---|
| shared_buffers | 128 MB | 512 MB | Holds hot workflow definitions & active indexes in RAM |
| effective_cache_size | 4 GB | 1536 MB | Honest planner hint accounting for Node.js RAM share |
| work_mem | 4 MB | 8 MB | Speeds up Execution-history ORDER BY sorts without OOM |
| max_connections | 100 | 60 | Caps per-connection RAM overhead under webhook storms |
| checkpoint_completion_target | 0.9 | 0.9 | Spreads WAL fsync writes smoothly across shared KVM I/O |
| autovacuum_vacuum_scale_factor | 0.20 (20%) | 0.05 (5%) | Reclaims pruned n8n JSONB dead tuples 4× sooner |
3. Drop-In Docker Compose Command Flags (Zero Custom Config File Needed)
You do not even need to mount a custom `postgresql.conf`. Pass all six flags cleanly via `command:` inside your `postgres:16-alpine` Compose service, and verify with `SHOW shared_buffers;`:
postgres:
image: postgres:16-alpine
restart: unless-stopped
command:
- "postgres"
- "-c"
- "shared_buffers=512MB"
- "-c"
- "effective_cache_size=1536MB"
- "-c"
- "work_mem=8MB"
- "-c"
- "max_connections=60"
- "-c"
- "checkpoint_completion_target=0.9"
- "-c"
- "autovacuum_vacuum_scale_factor=0.05"
mem_limit: 900m