Occasionally my shared memory segment settings in my /etc/rc file aren’t applied correctly during startup and I get the following error when I try to start Postgres:
FATAL: could not create shared memory segment: Cannot allocate memory DETAIL: Failed system call was shmget(key=5432001, size=33554432, 03600). HINT: This error usually means that PostgreSQL's request for a shared memory segment exceeded available memory or swap space. To reduce the request size (currently 33554432 bytes), reduce PostgreSQL's shared_buffers parameter (currently 3584) and/or its max_connections parameter (currently 103). The PostgreSQL documentation contains more information about shared memory configuration.
I would expect my shared memory size to be 167772160 instead of 33554432 since my /etc/rc file looks like this:
sysctl -w kern.sysv.shmmax=167772160 sysctl -w kern.sysv.shmmin=1 sysctl -w kern.sysv.shmmni=32 sysctl -w kern.sysv.shmseg=8 sysctl -w kern.sysv.shmall=65536
Restarting my machine helps, but doing that is just painful.
This may be obvious, but I learned that you can apply the sysctl command even if it doesn’t happen during startup. So all I had to do to fix my Postgres problem was:
> sudo sysctl -w kern.sysv.shmmax=16777216 kern.sysv.shmmax: 33554432 -> 167772160 > sudo sysctl -w kern.sysv.shmseg=8 kern.sysv.shmseg: 64 -> 8 > sudo sysctl -w kern.sysv.shmall=65536 kern.sysv.shmall: 8192 -> 65536
To see a list of all the kernel variables, type sysctl -A. Check out the sysctl man page for more details.