[GIS] Unable to connect to PostGIS databases after Mac OS X Yosemite upgrade

osxpostgispostgresql

I upgraded to Mac OS 10.10 Yosemite and am unable to connect to PostGIS databases (PostgreSQL version 9.3 and PostGIS installed from KyngChaos). Typing psql my_database returns this message:

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"?

The responses to this question suggest some postgres directories get removed during a Yosemite upgrade, but following the recommended solutions doesn't fix my error.

As requested, here are my conf settings (just showing those that aren't commented out):

listen_addresses = '*'
port = 5432
max_connections = 20
shared_buffers = 128MB
log_destination = 'stderr'
logging_collector = on
log_directory = '../var'
log_filename = 'postgresql-%Y-%m-%d.log'
log_line_prefix = '%t'
log_timezone = 'US/Pacific'
datestyle = 'iso, mdy'
timezone = 'US/Pacific'
lc_messages = 'en_US'
lc_monetary = 'en_US'
lc_numeric = 'en_US'
lc_time = 'en_US'
default_text_search_config = 'pg_catalog.english'

Best Answer

I can't leave a comment due to low reputation, so will extend @pistachionut's answer as a separate one.

In my case the contents of /usr/local/pgsql/data are owned by user _postgres. Thus, to recreate missing directories it is necessary to run the commands as that user:

sudo -u _postgres mkdir -p /usr/local/pgsql/data/{pg_tblspc,pg_twophase,pg_stat_tmp}/
sudo -u _postgres touch /usr/local/pgsql/data/{pg_tblspc,pg_twophase,pg_stat_tmp}/.keep

If this is not done, you will see errors in the log file such as the following:

2015-01-05 09:54:21 GMTLOG:  could not open temporary statistics file "pg_stat_tmp/global.tmp": Permission denied

After the folders are created, Postgres can be launched with no need to restart your Mac:

sudo -u _postgres /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data start