Fixing the postgresql initdb fatal shared memory error on Leopard

When doing the post-install setup of postgresql default database using initdb, you may hit this error:

FATAL:  could not create shared memory segment: Cannot allocate memory
DETAIL:  Failed system call was shmget(key=1, size=1646592, 03600).

You can either change the postgresql configuration to use less shared memory, or increase the system setting.

I suggest the latter, because this problem seems to stem from other running apps using some of the shared memory allowance, and I'd be nervous about them running out anyway if postgresql was still using a decent amount. IMHO, the default OS X limit is way too low.

Easy to fix; run:

sudo sysctl -w kern.sysv.shmall=65536
sudo sysctl -w kern.sysv.shmmax=16777216

(The first one's the critical one for me, but second seems appropriate too.)

This changes the limits in the currently-running kernel. To make these values stick across reboots, add them to /etc/sysctl.conf, like this:

kern.sysv.shmall=65536
kern.sysv.shmmax=16777216

(Create this file if it doesn't already exist – it doesn't on fresh Leopard installs.)