PostgreSQL cannot allocate memory on Mac OS X

asked 14 Sep '11, 11:12

Jake's gravatar image

Jake ♦♦
826222226235
accept rate: 98%


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:

Shared Memory

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

link

answered 14 Sep '11, 11:17

Jake's gravatar image

Jake ♦♦
826222226235
accept rate: 98%

1

Some additional notes to take into account on OS X:

If you change the /etc/sysctl.conf file, you need to restart the system so the changes are taken into account. To know if a variable have changed you can executed:

sysctl kern.sysv.shmall

If you don't want to restart the system you can execute the following (instead of changing the /etc/sysctl.conf file):

sysctl -w kern.sysv.shmall=newvalue

You can now check with sysctl kern.sysv.shmall that the new value is taken. Notice that these changes will not be kept after reboot (only the changes in sysctl.conf will be kept).

(03 Nov '11, 06:31) kaysa ♦♦
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×92
×51
×4
×3

Asked: 14 Sep '11, 11:12

Seen: 6,925 times

Last updated: 31 Mar '14, 07:48