14

Since upgrading OSX to version 10.7.3, when I attempt to run a Django "syncdb" command, I receive the following psycopg2 error from Postgresql 8.4.2:

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

I'm able to open psql without incident, but when I attempt to run "pg_ctl status", I receive this error as well:

pg_ctl: could not open PID file "/Library/PostgreSQL/8.4/data/postmaster.pid": Permission denied

Is there a correlation?

Huuuze
  • 15,528
  • 25
  • 72
  • 91
  • pg_ctl is a seperate issue, it must be run as the postgres user otherwise it can't access the pid file. When you run psql do you specify a host or ip with -h? – Eelke Feb 04 '12 at 07:20
  • No, I do not specify a host. Prior to 10.7.3, I never had to. – Huuuze Feb 05 '12 at 00:44
  • I've noticed that adding "-h localhost" to my "psql -U postgres mydatabase" commands fixes the problem. Any thoughts on that front? – Huuuze Feb 07 '12 at 22:46

7 Answers7

33

I had the same problem too: updated osx to 10.7.3 and after I restarted the postgres server I got the same issue when I tried to run $ ./manage.py dbshell that goes through psycopg2, but didn't have problem connecting to postgres using psql.

To me the problem appears to be related just to "connections on Unix domain socket", because psycopg2 uses the newly 10.7.3 osx updated postgres library.

I tried to reinstall psycopg2, but nothing was changed.

So, I changed the HOST value on the databases section of my django settings.py, and instead of having an empty string I put 'localhost'.

and it works perfectly!

bafio
  • 431
  • 3
  • 5
  • I changed the "DATABASE_HOST" value, but the problem persists. – Huuuze Feb 07 '12 at 21:59
  • Do you have in your postgresql.conf this variable setted as: `listen_addresses = 'localhost'` or `listen_addresses = '*'` otherwise postgres doesn't allow you to connect via 'network' And of course you have to check if everything's correct in pg_hba.conf too – bafio Feb 09 '12 at 15:58
  • 1
    I just made the suggested change, restarted Postgresql, and the problem persists. – Huuuze Feb 10 '12 at 16:41
  • This worked for me after upgrading from 10.6 to 10.8 with no restart of Postgres. – Tom Jul 26 '12 at 18:25
  • 2
    Very nice. I had the same issue. Changed the HOST string to localhost and boom! Worked like a charm. That was frustrating. Thanks for the tip! – mchandler Mar 16 '13 at 05:16
  • adding localhost fixed it for me in 10.8 – Michael Jun 19 '13 at 11:51
  • adding localhost fixed it for me in 10.8 as well, awesome dude – castiel Jul 10 '13 at 13:34
  • Thank you! I don't think there is ANY way I would've found a fix to this on my own. – pjreddie Sep 24 '13 at 22:24
8

Eelke 's problem was the same as mine. The quickest fix for this would be to add

export PGHOST=/tmp

an environment variable in your .bashrc/.zshrc/etc file (depending on which you use).

The best fix would be to set the specific path in your apps config file.

Nathaniel E.
  • 231
  • 2
  • 5
  • i think this works because it changes which psql you use from the problematic apple one, to the one you get when you install postgres maybe? – Pat B Jul 21 '12 at 17:19
  • Not a bash wizard myself, but it might have to read `export PGHOST="/tmp"` – kontur Sep 30 '14 at 17:58
4

Did a bit of testing on my Mac and I noticed that there are two versions on my system for several commands. One version is in /usr/bin the other is in /Library/PostgreSQL/9.0/bin. The version in /usr/bin is expecting /var/pgsql_socket/.s.PGSQL.5432, the right version for my install expects /tmp/.s.PGSQL.5432

The version in /usr/bin is 9.0.5 and it has been installed by Apple.

Unfortunatly I do not know much about django, but my bet is it is either calling a command in the wrong path or it is loading a library from the wrong location.

Eelke
  • 20,897
  • 4
  • 50
  • 76
2

This did the trick for me in the end...

Change the HOST value in the database dictionary in settings.py to this:

'HOST': '/tmp',

Source: http://jeffammons.net/2011/09/fixing-postgres-on-mac-10-7-tiger-for-django

NICCAI
  • 3,148
  • 2
  • 16
  • 8
2

You can fix the problem by creating a symlink:

 ln -s /tmp/.s.PGSQL.5432 /var/pgsql_socket/
Kris Pypen
  • 157
  • 1
  • 6
1

Reinstalling psycopg2 worked for me

m0y
  • 137
  • 2
  • 9
0

I ran into the same issue just after upgrading to OSX 10.7.3; reinstalling PostgreSQL solved the problem and did not delete my existing data.