29

After updating to Yosemite 10.10 I cannot connect to my postgresql database. I run the rails console and try and get the first user but get this error...

>     ➜  game_golf git:(master) ✗ rails c
>     Loading development environment (Rails 4.1.4)
>     [1] pry(main)> User.first
>     PG::ConnectionBad: 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?
>     from /Users/pauldornfeld/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `initialize'
>     [2] pry(main)>

What do I do! I have tried to reinstall postgresql, reinstall homebrew. Please help!

PDev
  • 410
  • 4
  • 10
  • 1
    Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? Have you tried to verify this? – mcfinnigan Oct 19 '14 at 08:44

3 Answers3

99

I found this.

cd /usr/local/var/postgres
mkdir pg_tblspc pg_twophase pg_stat_tmp

It worked for me.

daslicious
  • 1,535
  • 10
  • 18
  • 6
    Worked for me on PostgreSQL installed via Homebrew. – Space Oct 23 '14 at 14:56
  • Worked form me too. My postgres is installed at /usr/local/opt/postgres, and I only needed to create the pg_tblspc and pg_twophase dirs (pg_stat_tmp was already present). – realmikep Oct 25 '14 at 20:08
  • I also needed to `mkdir pg_stat` and as someone else mentioned pg_stat_tmp already existed. In my case, I had bought a new mbp with Yosemite and restored from a time machine created on Mavericks. – Brian Adkins Dec 05 '14 at 18:25
  • I'm sure glad I didn't install postgresapp – Will Farley Dec 05 '14 at 19:33
  • 2
    I just upgraded myself and tried this but it still wasn't working. I found that `pg_replslot` was also missing. I found it by tailing `server.log` which for me was located at`/usr/local/var/postgres/server.log`. Hope that either helps someone else or tells them how to find the source of the error. – Aaron Jan 11 '15 at 17:29
  • I had a few more errors this morning after a restart. Basically I just tailed the log again and made all the directories it complained about missing. – Aaron Jan 12 '15 at 14:14
  • For me `logfile` also had the following error: `FATAL: data directory "/usr/local/var/postgres" has group or world access DETAIL: Permissions should be u=rwx (0700).` so using `chmod` was the fix required. – Nigel Sheridan-Smith May 20 '15 at 05:42
9

Checking the server.log will show you what directories it's looking for. Adding them to your postgres directory should fix this. (Was going to comment above, but don't have enough reputation)

esc_rtn
  • 936
  • 9
  • 7
-5

You are better off using the easy method by installing the http://postgresapp.com/ app! I too had this same issue with 10.9.4. Also with 10.10.

brew install postgres will give issues when doing any OS X upgrades so avoid it. So install the app then:

Add this to your profile if the install do not work out of the box. For me I never had to add the below in my profile:

export PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH"

This line must be at the top of your profile:

export PATH=/usr/local/bin:$PATH

Good luck!

Sylar
  • 11,422
  • 25
  • 93
  • 166
  • Yes! it works thank you. Now thought I have to make sure the postgresql app is running in order to connect to the database. How come this was not necessary before but now it is? Did rails c automatically start the postgresql database or was it just always running without my knowledge prior? – PDev Oct 19 '14 at 16:57
  • I'm not really a guru at the moment but I know the brew installation of postgres database is located in /usr/local/pgsql/data – Sylar Oct 20 '14 at 10:46
  • 3
    You have to use Damien Jones' suggestion because the Yosemite upgrade deletes empty directories :( – Bron Davies Oct 24 '14 at 20:46
  • 2
    This answer will remain on top because it was accepted as correct. For anybody who installed Postgres through homebrew: the error was caused by the way Apple updates the OS. Nothing wrong with the homebrew install and not need to try an alternate installation. The answer from @daslicious provides an easy way to fix the issue. – Roy Prins Nov 06 '14 at 20:41
  • Though this is a valid solution, I'm still going to give it a downvote because stopping to use package management is not nearly a good a solution as the one provided below. – frediy Dec 26 '14 at 08:44