Search code examples
macospostgresqlosx-mountain-lion

PostgreSQL server won't start after system restore; OSX Mountain Lion


I previously installed PostgreSQL 9.2 on my Mac using the EnterpriseDB installer. As such I had amended .bash_profile to read export PATH=/opt/local/lib/postgresql92/bin:$PATH, and everything was working just fine.

Then I had a hard drive corruption and had to reformat my computer and reinstall OSX. Initially I had to reinstall Snow Leopard (that's the version of the recovery discs I had), and then re-upgrade to Mountain Lion (which I was running prior to my crash). I then used a Time Machine backup with Migration Assistant to restore my Users, Applications, and "Other Files".

Looking around everything seemed to be back where it was before the crash. However, now when I try to do anything PostgreSQL-related, I get the error:

psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

Reading around online I found that this might be simply because my PostgreSQL server had not been started when I performed the system restoration. The official docs say to use the following command:

$ postgres -D /usr/local/pgsql/data

But I don't have a folder at /usr/local/pgsql; the only directory with data I can find is /Library/PostgreSQL/9.2/data. So I switched to postgres by doing sudo su postgres and tried postgres -D /Library/PostgreSQL/9.2/data again, which gave:

2013-08-18 11:38:09 SGT FATAL:  could not create shared memory segment: Invalid argument
2013-08-18 11:38:09 SGT DETAIL:  Failed system call was shmget(key=5432001, size=32374784, 03600).
2013-08-18 11:38:09 SGT 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 32374784 bytes), reduce PostgreSQL's shared memory usage, perhaps by reducing shared_buffers or max_connections.
    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.

Where do I go from here? This whole thing is a bit strange; I don't remember ever having to start the server when I initially installed PostgreSQL...

EDIT: I also tried initdb -D /Library/PostgreSQL/9.2/data in case the db cluster was missing, but got:

initdb: directory "/Library/PostgreSQL/9.2/data" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/Library/PostgreSQL/9.2/data" or run initdb
with an argument other than "/Library/PostgreSQL/9.2/data".

So it should still be there, restored along with most of the other stuff on my system, right?


Solution

  • Your kernel parameters need tweaking. Here is the relevant documentation on kernel resources. Configure those according to your system specifications (memory) and you should be good.

    As a footnote, I would like to add that from Postgresql 9.3 onwards, the above tweaking will no longer be necessary.