Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to Postgres server running through brew services

People also ask

How do you check if Postgres is running on Mac?

psql -c "SELECT 1" -d {dbname} > /dev/null || postgres -D /usr/local/var/postgres >postgres. log 2>&1 & if you want to check and start postgres in one go (handy for automation scripts).


I had the same error and I fixed it by removing the process pid file:

rm -f /usr/local/var/postgres/postmaster.pid


I ran into this problem today. postgres stopped accepting connections though homebrew thought it was running.

To fix it I ran,

brew services restart -vvv postgresql

Output from this command,

==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Generated plist for postgresql:
   <?xml version="1.0" encoding="UTF-8"?>
   <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
   <plist version="1.0">
   <dict>
     <key>KeepAlive</key>
     <true/>
     <key>Label</key>
     <string>homebrew.mxcl.postgresql</string>
     <key>ProgramArguments</key>
     <array>
       <string>/usr/local/opt/postgresql/bin/postgres</string>
       <string>-D</string>
       <string>/usr/local/var/postgres</string>
     </array>
     <key>RunAtLoad</key>
     <true/>
     <key>WorkingDirectory</key>
     <string>/usr/local</string>
     <key>StandardErrorPath</key>
     <string>/usr/local/var/log/postgres.log</string>
   </dict>
   </plist>

Then I thought, hmm maybe there's something in that log file,

tail -n 10 /usr/local/var/log/postgres.log

Sure enough,

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

So, I removed that file

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

And everything started working again.


In my case the postmaster.pid file wasn't even there. Got it working by upgrading postgres.

brew update
brew upgrade

Then, because I upgraded the major version from 10 to 11, I also had to run this:

brew postgresql-upgrade-database

(source https://github.com/facebook/react-native/issues/18760#issuecomment-410533581)


I would combine the two answers from Wilson and Grasshopper here.

You can check the plist file for the postgres service using brew services list to find the location of the file and just opening it up in you favourite editor.

You should see the value of StandardErrorPath listed as:

<key>StandardErrorPath</key>
<string>/usr/local/var/log/postgres.log</string>

And then you should tail the end of the log file using tail -n 100 /usr/local/var/log/postgres.log

In my case the error was the following:

2017-12-06 11:51:16.078 GMT [85476] FATAL: lock file "postmaster.pid" already exists 2017-12-06 11:51:16.078 GMT [85476] HINT: Is another postmaster (PID 601) running in data directory "/usr/local/var/postgres"?

This was because I had to hard shutdown my Mac and postgres didn't get a chance to cleanup the PID file. Just remove the PID file rm /usr/local/var/postgres/postmaster.pid and start postgres brew services start postgresql

A word of warning: do not delete this PID file unless you are sure that postgres is not running. You can do this by running brew services stop postgresql and then waiting for the result of brew services list to show posgres is in the stopped state.