26

I'm trying to startup my PostgreSQL server on my local machine. But I got an error message saying:

FATAL:  could not create shared memory segment: Invalid argument
DETAIL:  Failed system call was shmget(key=5432001, size=9781248, 03600).
HINT:  This error usually means that PostgreSQL's request for a shared memory segment exceeded your kernel's SHMMAX parameter.  You can either reduce the request size or reconfigure the kernel with larger SHMMAX.  To reduce the request size (currently 9781248 bytes), reduce PostgreSQL's shared_buffers parameter (currently 1024) and/or its max_connections parameter (currently 13).
If the request size is already small, it's possible that it is less than your kernel's SHMMIN parameter, in which case raising the request size or reconfiguring SHMMIN is called for.
The PostgreSQL documentation contains more information about shared memory configuration.  

I search and looked at the docs but everything I tries about setting the kern.sysv.shmmax and kern.sysv.shmall is working. What are the right settings on Snow Leopard? I installed postgres with macports.

3
  • Try asking on serverfault.com.
    – pilcrow
    Jan 6, 2010 at 23:02
  • 2
    Here is the link to the same question on serverfault: serverfault.com/questions/10226/…
    – wallyqs
    Nov 21, 2010 at 6:46
  • Update: Postgres 9.3 and higher have been rewritten to require only a tiny amount of shared memory. When using >= 9.3, adjusting kernel shared memory limits should no longer be necessary. depesz.com/2012/07/12/… May 8, 2013 at 19:18

4 Answers 4

33

You have to increase the kernel's maximum shared memory allowance to something higher than what Postgres is trying to allocate. (You could also decrease the shared buffers or maximum connections settings in postgresql.conf to make Postgres ask for less memory, but the default values are already rather small for most use cases.)

To do this as a one-off, lasting until next reboot:

sudo sysctl -w kern.sysv.shmmax=12582912
sudo sysctl -w kern.sysv.shmall=12582912

Change the exact number as appropriate for your Postgres settings; it has to be larger than whatever Postgres says it's asking for in the log file. After doing both of these, you should be able to start Postgres.

To make the change persist across reboots, edit /etc/sysctl.conf and set the same values there.

1
  • 1
    FYI seems like adjusting kernel settings no longer necessary, as of Postgres 9.4 - see here: "PostgreSQL 9.4 and later use a different shared memory mechanism, so kernel settings will usually not have to be adjusted."
    – Yarin
    Jan 3, 2019 at 21:17
8

Apple documents how to adjust them for Snow Leopard here:

http://support.apple.com/kb/HT4022: Mac OS X Server v10.6: Adjusting Shared memory segment values

sysctl does allow you to change them temporarily.

4

I know this is an old question, but I think it might be worth noting that if you're just using PostgreSQL for development purposes you might be safe to just go into postgres.conf (the data directory after you've done initdb) and change the shared_buffers variable to something a little lower. It defaults to 28MB or something. But this way you're not messing around with system shared memory variables.

1
  • 3
    While this will work, even the default value is absurdly small for a modern system with multiple gigabytes of memory available. Postgres operates much faster with larger shared_buffers and workmem settings when dealing with datasets and queries of any complexity. There's nothing inherently wrong with changing system kernel settings, provided you are doing so intelligently and not just changing them randomly. Jan 1, 2013 at 21:39
2

Warning: this answer has been made obselete by newer versions of OS X. Please reference Paul Legato's answer below.

In Mac OS X you cannot change shmmax after the system has booted. You need to edit /etc/rc or /etc/sysctl.conf, and keep in mind that it needs to be a multiple of 4096. See here http://www.postgresql.org/docs/8.4/static/kernel-resources.html

4
  • 4
    this doesn't actually affect my system even after a reboot.
    – Paul
    Sep 24, 2011 at 5:53
  • 10
    This is not true. You can change shmmax and other kernel settings at runtime, without a reboot, with "sudo sysctl -w". May 17, 2012 at 2:30
  • I wonder how many people downvoting this answer realize that it has been made obsolete by newer versions of OS X.
    – alvherre
    Jul 2, 2013 at 2:53
  • 1
    It doesn't really matter, does it? Obsolete answers should not still be here without a big warning on them, and they certainly shouldn't continue to be marked as accepted.
    – JoshJordan
    Aug 27, 2013 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.