In my case Postgres was managed through Homebrew Services (i.e. started via brew services start postgresql@10 Terminal command for the Postgres 10 that I use), and for that setup I had to discover a couple of essential steps to do before I could apply any advice in this thread. So I want to share just that piece as it may help someone who has the same setup.
NOTE: all the commands below are to be run in Terminal.
To give a quick background: After upgrading to macOS Big Sur I discovered that Postgres wasn't working and running psql results in the error mentioned in the original question above. I tried to start Postgres (via the brew services start postgresql@10 command), this resulted in a message Service postgresql@10 already started. If I tried to restart it (via the brew services restart postgresql@10) I got a message that it was stopped and then started successfully. But! This was a misleading message, and I spent quite some time searching for config issues etc. before discovering that the service was not started successfully in reality.
So, the way to investigate this is:
- Make sure the service is started by running the brew services start postgresql@10(the latter argument may be different depending on what your Homebrew package name is e.g.postgresql@12or plainpostgresql).
- Run brew services list. This is the command that gives you the true state of the service. In my case it said that Postgres' status is error:
Name         Status  User  Plist
postgresql@10 error   Denis /Users/Denis/Library/LaunchAgents/homebrew.mxcl.postgresql@10.plist
redis         started Denis /Users/Denis/Library/LaunchAgents/homebrew.mxcl.redis.plist
- To investigate further open the config shown in the same command output in Plist column (I used nano /Users/Denis/Library/LaunchAgents/homebrew.mxcl.postgresql@10.plistto check it).
- In the config look for the StandardErrorPathkey, and open the file located in the value of that key, i.e. in the<string>tag following the key. In my case it was/usr/local/var/log/postgresql@10.log.
- Open that log and check the latest error (I used nano /usr/local/var/log/postgresql@10.logand thenAlt+/to go to the end of the file).
- Voila. That is the real error to investigate, which you can then look for in the previous answers or google for. I'm not covering the rest here, as the goal of this answer is to show how to find the real error if you use Homebrew Services to run Postgres. (In my case it was the lock file "postmaster.pid" already existsalready covered in the previous answers, plus the path to check right in the error message, in my case/usr/local/var/postgresql@10).