$ run as a normal user# run as root (or with sudo)
The quick answer
For a dedicated server with 16 GB RAM:
sudo -u postgres psql -c "ALTER SYSTEM SET shared_buffers = '4GB';"
sudo -u postgres psql -c "ALTER SYSTEM SET effective_cache_size = '12GB';"
sudo -u postgres psql -c "ALTER SYSTEM SET work_mem = '32MB';"
sudo -u postgres psql -c "ALTER SYSTEM SET maintenance_work_mem = '1GB';"
systemctl restart postgresql
sudo -u postgres psql -c "SHOW shared_buffers;"Step by step
- See the current values and which need a restart:
sudo -u postgres psql -c "SELECT name, setting, unit, context FROM pg_settings WHERE name IN ('shared_buffers','effective_cache_size','work_mem','maintenance_work_mem','max_connections');"context = postmaster means restart needed; user can even be set per session.
- shared_buffers ≈ 25 % of RAM — PostgreSQL's own page cache. More is rarely better, because the OS cache also holds the data.
- effective_cache_size ≈ 50–75 % of RAM — no memory is reserved; it only tells the planner how much data is probably cached, so it chooses index scans more often.
- work_mem with care — memory per sort or hash step, per connection. One complex query can use it several times; 200 connections × 3 steps × 32 MB = 19 GB. Start at 16–64 MB and raise it only for heavy reports:
sudo -u postgres psql -d shop -c "SET work_mem = '256MB'; EXPLAIN (ANALYZE, BUFFERS) SELECT customer_id, sum(total) FROM orders GROUP BY customer_id ORDER BY 2 DESC;"Sort Method: external merge Disk: … means the sort did not fit in memory.
- maintenance_work_mem 512 MB – 2 GB — faster
VACUUM,CREATE INDEXand restores. - Fewer connections, not more memory — keep
max_connectionsaround 100–200 and put PgBouncer in front of busy apps. - Apply —
ALTER SYSTEMwritespostgresql.auto.conf. Reload for most settings (SELECT pg_reload_conf();), restart forshared_buffers. CheckSELECT name FROM pg_settings WHERE pending_restart;. - Protect against the OOM killer — set
vm.overcommit_memory = 2(with a suitablevm.overcommit_ratio) on dedicated servers, so a big query gets an error instead of the whole server being killed.
Check that it worked
SHOW shared_buffers; returns 4GB; pending_restart is empty. Over the next days the cache hit ratio stays high and temporary files go down:
Common problems
| Problem | Cause | Fix |
|---|---|---|
| PostgreSQL does not start after the change | shared_buffers larger than the kernel allows or than RAM. | Lower it; check the log; ALTER SYSTEM RESET shared_buffers if needed. |
| Server killed by OOM killer | work_mem × connections too high. | Lower work_mem; PgBouncer; vm.overcommit_memory = 2. |
| Setting has no effect | Needs a restart, or overridden in postgresql.auto.conf. | Check pending_restart and SELECT * FROM pg_file_settings WHERE name='…';. |
| Many temp files | Sorts and hashes spill to disk. | log_temp_files = 0 to find them; more work_mem for those queries only; indexes. |
| Hit ratio low after restart | Cache is cold. | Normal; it warms up (or use pg_prewarm). |
| Container/VM limit smaller than "RAM" | Values based on the host, not the cgroup limit. | Calculate from the container's memory limit. |
Go further
- Command pages: psql, free, sysctl.
- Next how-tos: Find slow PostgreSQL queries, See who is connected to a database, Check memory and CPU usage, Add swap space.
- Learn it: Data services at work — PostgreSQL. Full guide: PostgreSQL.
1Why is work_mem dangerous to set high?
It is used per sort or hash step in every connection, so many connections can use many times that amount at once.
2Does effective_cache_size reserve memory?
No, it is only a hint for the planner about how much data the OS cache probably holds.