Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

psql: could not connect to server: No such file or directory (Mac OS X)

Upon restarting my Mac I got the dreaded Postgres error:

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

The reason this happened is because my macbook froze completely due to an unrelated issue and I had to do a hard reboot using the power button. After rebooting I couldn't start Postgres because of this error.

like image 260
FireDragon Avatar asked Nov 26 '12 21:11

FireDragon


People also ask

Can't connect to server No such file or directory psql?

When connecting to Postgres you might see this error: psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket <some socket> . This happens most often when Postgres' server daemon process is not running.

How do I connect to PostgreSQL database on Mac?

The default username for postgres is postgres. (If you are using Advanced Server it is enterprisedb.) On a Mac or Windows, you are able to connect to the default instance by simply hitting enter at the shell or command prompt when trying to run psql and keying in the password.

How do I check PostgreSQL status on Mac?

It is better to use another method like using psql to run a simple query and checking the exit code, e.g. psql -c 'SELECT 1' , or use pg_isready to check the connection status. psql -c "SELECT 1" -d {dbname} > /dev/null || postgres -D /usr/local/var/postgres >postgres.


4 Answers

WARNING: If you delete postmaster.pid without making sure there are really no postgres processes running you, could permanently corrupt your database. (PostgreSQL should delete it automatically if the postmaster has exited.).

SOLUTION: This fixed the issue--I deleted this file, and then everything worked!

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

--

and here is how I figured out why this needed to be deleted.

  1. I used the following command to see if there were any PG processes running. for me there were none, I couldn't even start the PG server:

    ps auxw | grep post
    
  2. I searched for the file .s.PGSQL.5432 that was in the error message above. i used the following command:

    sudo find / -name .s.PGSQL.5432 -ls
    

    this didn't show anything after searching my whole computer so the file didn't exist, but obviously psql "wanted it to" or "thought it was there".

  3. I took a look at my server logs and saw the following error:

    cat /usr/local/var/postgres/server.log
    

    at the end of the server log I see the following error:

    FATAL:  pre-existing shared memory block (key 5432001, ID 65538) is still in use
    HINT:  If you're sure there are no old server processes still running, remove the shared memory block or just delete the file "postmaster.pid".
    
  4. Following the advice in the error message, I deleted the postmaster.pid file in the same directory as server.log. This resolved the issue and I was able to restart.

So, it seems that my macbook freezing and being hard-rebooted caused Postgres to think that it's processes were still running even after reboot. Deleting this file resolved. Hope this helps others! Lots of people have similar issues but most the answers had to do with file permissions, whereas in my case things were different.

like image 132
FireDragon Avatar answered Oct 17 '22 23:10

FireDragon


None of the above worked for me. I had to reinstall Postgres the following way :

  • Uninstall postgresql with brew : brew uninstall postgresql

  • brew doctor (fix whatever is here)

  • brew cleanup

  • Remove all Postgres folders :

  • rm -r /usr/local/var/postgres

  • rm -r ~/Library/Application\ Support/Postgres

  • Reinstall postgresql with brew : brew install postgresql

  • Start server : brew services start postgresql

  • You should now have to create your databases... (createdb)

like image 31
ypicard Avatar answered Oct 17 '22 23:10

ypicard


If you're on macOS and installed postgres via homebrew, try restarting it with

brew services restart postgresql

If you're on Ubuntu, you can restart it with either one of these commands

sudo service postgresql restart

sudo /etc/init.d/postgresql restart
like image 99
Jagdish Barabari Avatar answered Oct 17 '22 23:10

Jagdish Barabari


Maybe this is unrelated but a similar error appears when you upgrade postgres to a major version using brew; using brew info postgresql found out this that helped:

To migrate existing data from a previous major version of PostgreSQL run:
brew postgresql-upgrade-database
like image 46
Crazy Barney Avatar answered Oct 17 '22 23:10

Crazy Barney