Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

259,194

Solution 1

You most likely ran out of battery and your postgresql server didn't shutdown correctly.

The easiest workaround is to download the official postgresql app and launch it: it will force the server to start (http://postgresapp.com/)

Solution 2

run postgres -D /usr/local/var/postgres and you should see something like:

 FATAL:  lock file "postmaster.pid" already exists
 HINT:   Is another postmaster (PID 379) running in data directory "/usr/local/var/postgres"?

Then run kill -9 PID in HINT

And you should be good to go.

Solution 3

Most likely it's because your system shutdown unexpectedly

Try

postgres -D /usr/local/var/postgres

You might see

FATAL:  lock file "postmaster.pid" already exists
HINT:  Is another postmaster (PID 449) running in data directory "/usr/local/var/postgres"?

Then try

kill -9 PID

example

kill -9 419

And it should start postgres normally

Solution 4

The postgresql server might be down and the solution might be as simple as running:

sudo service postgresql start

which fixed the issue for me.

Solution 5

This could be caused by the pid file created for postgres which has not been deleted due to unexpected shutdown. To fix this, remove this pid file.

  1. Find the postgres data directory. On a MAC using homebrew it is /usr/local/var/postgres/, other systems it might be /usr/var/postgres/

  2. Remove pid file by running:

    rm postmaster.pid
    
  3. Restart postgress. On Mac, run:

    brew services restart postgresql
    
Share:
259,194

Related videos on Youtube

Thibaud Clement
Author by

Thibaud Clement

Updated on November 23, 2021

Comments

  • Thibaud Clement
    Thibaud Clement over 2 years

    Before anything, please note that I have found several similar questions on Stack Overflow and articles all over the web, but none of those helped me fix my issue:

    Now, here is the issue:

    • I have a Rails app that works like a charm.
    • With my collaborator, we use GitHub to work together.
    • We have a master and an mvp branches.
    • I recently updated my git version with Homebrew (Mac).
    • We use Foreman to start our app locally.

    Now, when I try to launch the app locally, I get the following error:

    PG::ConnectionBad at /
    could not connect to server: Connection refused
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?
    could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?
    

    I tried to reboot my computers several times.

    I also checked the content of /usr/local/var/postgres:

    PG_VERSION      pg_dynshmem     pg_multixact    pg_snapshots    pg_tblspc       postgresql.conf
    base            pg_hba.conf     pg_notify       pg_stat         pg_twophase     postmaster.opts
    global          pg_ident.conf   pg_replslot     pg_stat_tmp     pg_xlog         server.log
    pg_clog         pg_logical      pg_serial       pg_subtrans     postgresql.auto.conf
    

    As you can see, there is no postmaster.pid file in there.

    Any idea how I could fix this?

    • Pavan
      Pavan almost 8 years
      Can you update the question with the content of postgresql.conf?
    • Shrabanee
      Shrabanee almost 2 years
      I know this is a very old question. But today I faced the same problem in Mac and brew reinstall postgresql@13 resolved the problem for me. (It is a requirement to use v13 for my project)
  • Thibaud Clement
    Thibaud Clement almost 8 years
    Thanks for your answer. I just downloaded the app and launched it. If I shut it down before launching my server, then I still get the same error. If I don't shut it down, then I get an error message stating that my "database does not exist". Can this be related?
  • Graham Slick
    Graham Slick almost 8 years
    If your database does not exist, you should run rake db:create . But this is most likely not related ... Did you just fork a repo or something like this ? You shouldn't shut it down before launching your server using 'rails s' !! The app has to be running the whole time you want to access your site on a localhost
  • Thibaud Clement
    Thibaud Clement almost 8 years
    Ok. No, I did not fork a repo, it is the same repo I was working with. But I ran git fetch, maybe this is the problem.
  • Graham Slick
    Graham Slick almost 8 years
    git fetch is not supposed to be deleting the database. What's happening if you run rake db:create ?
  • Paul Nelligan
    Paul Nelligan over 6 years
    Best solution for my case, which is that my machine shut down unexpectedly and restarting the service was not solving the issue - thanks!
  • Mirror318
    Mirror318 about 6 years
    If you're using homebrew, brew services start postgres or brew services restart postgres might do the trick
  • qasimalbaqali
    qasimalbaqali over 5 years
    thank you! I ran with this issue for the second time, how did you understand that this would be the solution?
  • tito.300
    tito.300 almost 5 years
    Any equivalent solution for linux ubuntu dist?
  • Mwangi Kabiru
    Mwangi Kabiru almost 5 years
    This worked for me in 2019 after hours of trying to resolve the issue.
  • Ajay Maity
    Ajay Maity over 4 years
    This worked for me when nothing else on the internet did. My machine did shut down unexpectedly. Thanks so much.
  • Developine
    Developine over 4 years
    Yes my system shut down unexpectedly and this solved my problem. thank you.
  • Sagar Ranglani
    Sagar Ranglani over 4 years
    "Ran out of battery"... this is exactly what happened! :-O
  • Muhammad Usman Bashir
    Muhammad Usman Bashir about 4 years
    Best solution for my case, which is that my machine shut down unexpectedly and restarting the service was not solving the issue - thanks!
  • sophievda
    sophievda almost 4 years
    With this error the other client application 'pgAdmin'did not run the server nor used the 5432 port. But fixed it by reinstalling PostgreSQL DBMS.
  • 0xD1x0n
    0xD1x0n almost 4 years
    It just started doing this when I downloaded it the very first time. Your solution tho is 100% working and fixed it
  • Nicolas Bodin-Ripert
    Nicolas Bodin-Ripert over 3 years
    Thx! Personally I had to remove in this way: rm /Users/<username>/Library/Application\ Support/Postgres/var-<versionId>/postmaster.pid
  • Houndjetode Noukpo Herve
    Houndjetode Noukpo Herve over 3 years
    We are in dec 2020 and this solution work for me after a long time of research. thanks a lot
  • fatfrog
    fatfrog over 2 years
    What is the cause of this? I never had these types of issues with MySql?
  • Masroor
    Masroor over 2 years
    these installation paths are darn confusing. Finally, this one worked. Removing the previous installation files was the solution.
  • stevehs17
    stevehs17 over 2 years
    This worked for me in Dec 2021. Thank you!
  • Hamid Shoja
    Hamid Shoja over 2 years
    No such file or directory
  • Zebraboard
    Zebraboard almost 2 years
    thank you so much. after so much search this ended up fixing my issue