Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
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.
Find the postgres data directory. On a MAC using homebrew it is
/usr/local/var/postgres/
, other systems it might be/usr/var/postgres/
Remove pid file by running:
rm postmaster.pid
Restart postgress. On Mac, run:
brew services restart postgresql
Related videos on Youtube
Thibaud Clement
Updated on November 23, 2021Comments
-
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:
- PG Error could not connect to server: Connection refused Is the server running on port 5432?
- PG::ConnectionBad - could not connect to server: Connection refused
- psql: could not connect to server: Connection refused
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 anmvp
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 almost 8 yearsCan you update the question with the content of
postgresql.conf
? -
Shrabanee almost 2 yearsI 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 almost 8 yearsThanks 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 almost 8 yearsIf 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 almost 8 yearsOk. 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 almost 8 yearsgit fetch is not supposed to be deleting the database. What's happening if you run rake db:create ?
-
Paul Nelligan over 6 yearsBest solution for my case, which is that my machine shut down unexpectedly and restarting the service was not solving the issue - thanks!
-
Mirror318 about 6 yearsIf you're using homebrew,
brew services start postgres
orbrew services restart postgres
might do the trick -
qasimalbaqali over 5 yearsthank you! I ran with this issue for the second time, how did you understand that this would be the solution?
-
tito.300 almost 5 yearsAny equivalent solution for linux ubuntu dist?
-
Mwangi Kabiru almost 5 yearsThis worked for me in 2019 after hours of trying to resolve the issue.
-
Ajay Maity over 4 yearsThis worked for me when nothing else on the internet did. My machine did shut down unexpectedly. Thanks so much.
-
Developine over 4 yearsYes my system shut down unexpectedly and this solved my problem. thank you.
-
Sagar Ranglani over 4 years"Ran out of battery"... this is exactly what happened! :-O
-
Muhammad Usman Bashir about 4 yearsBest solution for my case, which is that my machine shut down unexpectedly and restarting the service was not solving the issue - thanks!
-
sophievda almost 4 yearsWith 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 almost 4 yearsIt just started doing this when I downloaded it the very first time. Your solution tho is 100% working and fixed it
-
Nicolas Bodin-Ripert over 3 yearsThx! Personally I had to remove in this way: rm /Users/<username>/Library/Application\ Support/Postgres/var-<versionId>/postmaster.pid
-
Houndjetode Noukpo Herve over 3 yearsWe are in dec 2020 and this solution work for me after a long time of research. thanks a lot
-
fatfrog over 2 yearsWhat is the cause of this? I never had these types of issues with MySql?
-
Masroor over 2 yearsthese installation paths are darn confusing. Finally, this one worked. Removing the previous installation files was the solution.
-
stevehs17 over 2 yearsThis worked for me in Dec 2021. Thank you!
-
Hamid Shoja over 2 yearsNo such file or directory
-
Zebraboard almost 2 yearsthank you so much. after so much search this ended up fixing my issue