8

I am having this issue third or forth time where after restarting my Mac (Yosemite and same thing with previous OSX version) postgres server doesn't start. I had installed my postgres following this link. Any idea why this consistently occurs after every restart?

typing psql it gives

psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
user2066049
  • 1,371
  • 1
  • 12
  • 26

2 Answers2

34

I just fixed this on my mac. I used homebrew to install postgres. These are the steps I took to get up and running again.

First check the status of the postgresql service:

pg_ctl -D /usr/local/var/postgres status

if you see this pg_ctl: no server running, unload the launch agent:

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

then move your existing postgres data (move rather than delete - cheap insurance):

mv /usr/local/var/postgres /usr/local/var/postgres-deletemewhenitsallgood

then initialize your postgres database:

initdb /usr/local/var/postgres -E utf8

then load your launch agent:

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

OR start it:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

Now check the status again:

pg_ctl -D /usr/local/var/postgres status

if you see something like this, you are good to go.

pg_ctl: server is running (PID: 61295)
/usr/local/Cellar/postgresql/bin/postgres "-D" "/usr/local/var/postgres"

Then you can go into your postgres database:

psql -d postgres

Also after that you need to add postgress Role. To Resolve missing postgres Role error

Taimoor Changaiz
  • 10,250
  • 4
  • 49
  • 53
m79lkm
  • 2,960
  • 1
  • 22
  • 22
  • 1
    This sequence of commands assumes that the asker is OK to loose all the database data and restart from scratch, which was never mentioned in the question. – Daniel Vérité Jan 31 '15 at 18:23
  • The OP said after installing it and restarting several times they could not connect because of the error. If there is data that needs to be saved it is still on the filesystem and can be recovered. – m79lkm Jan 31 '15 at 20:57
  • 1
    @m79lkm AWESOMEEEE!! It worked perfectly for me after being stuck on this issue for 2 days. Keep up the good work champ! – nodyor90z Mar 18 '17 at 22:27
  • Although this answer helped me, it would be interesting to understand if there is a duplication of psql installations, one for brew and other for osx. I'm lost in this aspect. – villancikos Apr 02 '17 at 11:52
  • What were the events before you saw this problem? This is such a regular occurrence to me, I have SO threads bookmarked. Why do I have to move around the entire Postgres datastore just to restart my server? Any ideas on what caused this or how to prevent it? I guess for me, I updated Mac (which took my computer HOSTAGE as Apple tends to do). – allthesignals Apr 18 '17 at 17:01
5

To start postgres server on mac:

install postgres with brew

brew install postgres

ensure you have permissions for /usr/local/postgres

sudo chown -R `whoami` /usr/local

then init the db:

initdb /usr/local/var/postgres

To have launchd start postgresql at login:

ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents

To load postgresql:

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

To check your version:

psql --version

And to access postgres

psql -d postgres

svnm
  • 22,878
  • 21
  • 90
  • 105
  • `ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents` got it working for me. Thanks – Ryan Mar 22 '17 at 13:40