Last Updated: February 25, 2016
·
4.287K
· brandon-beacher

PostgreSQL `shared memory segment` error

A freshly homebrewed postgresql on OS X only comes with 20 connections.

If you have lots of worker processes, etc. it's easy to exceed this limit.

When you increase the limit (perhaps to 40) and restart postgresql it will fail to start with the following message:

FATAL:  could not create shared memory segment: Cannot allocate memory
DETAIL:  Failed system call was shmget(key=5432001, size=4833280, 03600).
HINT:  This error usually means that PostgreSQL's request for a shared memory segment exceeded available memory or swap space, or exceeded your kernel's SHMALL parameter.  You can either reduce the request size or reconfigure the kernel with larger SHMALL.  To reduce the request size (currently 4833280 bytes), reduce PostgreSQL's shared memory usage, perhaps by reducing shared_buffers or max_connections.
The PostgreSQL documentation contains more information about shared memory configuration.

The solution is to grab the two values (4833280 and 3600) and do two things:

First, update the currently running values:

sudo sysctl -w kern.sysv.shmmax=4833288
sudo sysctl -w kern.sysv.shmall=3600

Second, ensure these values persist across restarts:

Create and edit /etc/sysctl.conf, inserting these two lines:

kern.sysv.shmmax=4833280
kern.sysv.shmall=3600