Postgres could not connect to server
Had a similar problem; a pid file was blocking postgres from starting up. To fix it:
$ rm /usr/local/var/postgres/postmaster.pid
$ brew services restart postgresql
and then all is well.
UPDATE:
For Apple M1 (Big Sur) users, do this instead:
$ rm /opt/homebrew/var/postgres/postmaster.pid
$ brew services restart postgresql
psql: error: connection to server on socket /tmp/.s.PGSQL.5432 failed: No such file or directory
Below was the workaround if you prefer to stay in v13.3, if you are on 14.2 you can simply change the port postgresql is listening to. Read their docs about it here.
Note that this solution works for the setup that I have (take a look at the original post for my setup details ie. OS & how what I'd use to install Postgres)
- open your postgresql.conf file in the following directory and use
any text editor you want. below uses vscode/vim.
vscode
sudo code . /usr/local/var/postgres/postgresql.conf
vim
sudo vi /usr/local/var/postgres/postgresql.conf
- change port to 5432 and restart your machine.
v13.3 solution
it appears that the upgrade really messed up my postgres since per Nagev's answer it's listening to port 5433 instead of 5432. I downgraded to v13.3 to fix this issue.
brew uninstall postgresql
brew install postgresql@13
brew services start postgresql@13
brew link postgresql@13 --force
psql: could not connect to server: No such file or directory (Mac OS X)
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.
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
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".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".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.
Related Topics
Elegant Chained 'Or's for Tests on Same Variable in Ruby
Selenium-Webdriver Ruby --> How to Wait for Images to Be Fully Loaded After Click
Rails Active Admin CSS Conflicting with Twitter Bootstrap CSS
Bundle Install Issue with Libv8 and Rails
Ruby Ssl Error - Sslv3 Alert Unexpected Message
Sass::Syntaxerror: File to Import Not Found or Unreadable: Compass in Production
How to Push to Faye Server from Rails Controller
What Do I Need to Do to Get the Blog to Work in Rails 4.2
Uninitialized Constant "Controller Name"
Form_For "First Argument in Form Cannot Contain Nil or Be Empty" Error
How to Improve Jruby Load Time
Rails Form Validation Conditional Bypass
What Is the Best Practice When It Comes to Testing "Infinite Loops"