PostgreSQL cannot allocate memory on Mac OS X
When PostgreSQL is included as part of a Mac installer, you may find the following error in some machines during database initialization:
FATAL: could not create shared memory segment:
Cannot allocate memory
DETAIL: Failed system call was shmget(key=1,size=1499136, 03600)
...
This problem is related to the fact that some OS X systems are configured with an amount of shared memory which may not be enough to run postgreSQL depending on other applications that may be installed in the machine.
You can workaround this by modifying kernel parameters, running the following as root:
sysctl -w kern.sysv.shmmax=33554432
sysctl -w kern.sysv.shmmin=1
sysctl -w kern.sysv.shmmni=256
sysctl -w kern.sysv.shmseg=64
sysctl -w kern.sysv.shmall=8192
This can be made permanent adding the following lines to /etc/sysctl.conf.
kern.sysv.shmmax=33554432
kern.sysv.shmmin=1
kern.sysv.shmmni=256
kern.sysv.shmseg=64
kern.sysv.shmall=8192
You may consider showing a more specific error message that redirects the user to a detailed description about how to fix the issue. You can do this by checking the returned error message as follows:
<actionGroup>
<actionList>
<runProgram>
<abortOnError>0</abortOnError>
<showMessageOnError>0<showMessageOnError>
<runAs>postgres</runAs>
<program>${postgres_root_directory}/bin/initdb</program>
<programArguments>-E UTF8 -U postgres</programArguments>
</showMessageOnError></showMessageOnError></runProgram>
<throwError>
<text>There is not enough shared memory. PostgreSQL component requires a minimum shared memory segment
of 32MB. Please increase "shmmax" kernel parameter in /etc/sysctl.conf) or close any other PostgreSQL
instances before restarting installation. Additional information can be found in the installation guide.</text>
<ruleList>
<compareText text="${program_exit_code}" value="0" logic="does_not_equal" />
<compareText text="${program_stderr}" value="Cannot allocate memory" logic="contains" />
</ruleList>
</throwError>
<throwError>
<text>${program_stderr}</text>
<ruleList>
<compareText text="${program_exit_code}" value="0" logic="does_not_equal" />
</ruleList>
</throwError>
</actionList>
</actionGroup>
What EnterpriseDB includes in its readme file about shared memory is shown below:
PostgreSQL uses shared memory extensively for caching and inter-process communication. Unfortunately, the default configuration of Mac OS X does not allow suitable amounts of shared memory to be created to run the database server.
Before running the installation, please ensure that your system is configured to allow the use of larger amounts of shared memory. Note that this does not 'reserve' any memory so it is safe to configure much higher values than you might initially need. You can do this by editting the file /etc/sysctl.conf - e.g.
% sudo vi /etc/sysctl.conf
On a MacBook Pro with 2GB of RAM, the author's sysctl.conf contains:
kern.sysv.shmmax=1610612736
kern.sysv.shmall=393216
kern.sysv.shmmin=1
kern.sysv.shmmni=32
kern.sysv.shmseg=8
kern.maxprocperuid=512
kern.maxproc=2048
Note that (kern.sysv.shmall * 4096)
should be greater than or equal to
kern.sysv.shmmax. kern.sysv.shmmax
must also be a multiple of 4096.
Once you have edited (or created) the file, reboot before continuing with the installation. If you wish to check the settings currently being used by the kernel, you can use the sysctl utility:
% sysctl -a
The database server can now be installed.
For more information on PostgreSQL's use of shared memory, please see:
http://www.postgresql.org/docs/current/static/kernel-resources.html#SYSVIPC