PostgreSQL error 'Could not connect to server: No such file or directory'

Check there is no postmaster.pid in your postgres directory, probably /usr/local/var/postgres/

remove this and start server.

Check - https://github.com/mperham/lunchy is a great wrapper for launchctl.


For me, this works

rm /usr/local/var/postgres/postmaster.pid

"Postgres.app" is a better fix if you are on OS X


Here is the fix:

  1. Stop the database
  2. cd /var
  3. sudo rm -r pgsql_socket
  4. sudo ln -s /tmp pgsql_socket
  5. chown _postgres:_postgres pgsql_socket
  6. Restart PostgreSQL (not your computer)

More information is available at "postgresql 9.0.3. on Lion Dev Preview 1".


This worked for me for the error you mentioned. Do one of these:

  1. Possibly change your default port specified in the postgres.conf file, if you have used something other than the default port 5432 while installing.

  2. Change the port number in postgresql.conf and restart the DB server.

  3. Instead of psql type the full command:

    psql -p 5432 -h localhost   
    
    • server name and the port no

If you are running Homebrew, uninstall Postgresql end pg gems:*

$ gem uninstall pg
$ brew uninstall postgresql

Download and run the following script to fix permission on /usr/local:* https://gist.github.com/rpavlik/768518

$ ruby fix_homebrew.rb

Then install Postgres again and pg gem:*

$ brew install postgresql  
$ initdb /usr/local/var/postgres -E utf8

To have launchd start postgresql at login run:

$ ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents 

Or start manually.

Install pg gem

$ gem install pg

I hope have helped