title: PostgreSQL error 'Could not connect to server: No such file or directory'
body: <p>Like some others I am getting this error when I run rake db:migrate in my project or even try most database tasks for my <a href="http://en.wikipedia.org/wiki/Ruby_on_Rails" rel="noreferrer">Ruby on Rails</a> 3.2 applications.</p>
  
  <blockquote>
    <p>PGError (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"?</p>
  </blockquote>
  
  <p>I installed <a href="http://en.wikipedia.org/wiki/PostgreSQL" rel="noreferrer">PostgreSQL</a> with <a href="https://en.wikipedia.org/wiki/Homebrew_%28package_management_software%29" rel="noreferrer">Homebrew</a> a long time ago and following an attempted installation of <a href="http://en.wikipedia.org/wiki/MongoDB" rel="noreferrer">MongoDB</a> recently my PostgreSQL install has never been the same. I'm running OS&nbsp;X&nbsp;v10.6 Snow&nbsp;Leopard.</p>
  
  <p>What's wrong and how do I better understand how PostgreSQL is and should be setup on my Mac?</p>
  
  <p>So far (I think) this tells me that PostgreSQL is not running(?).</p>
  
  <pre><code>ps -aef|grep postgres                                                                                                   (ruby-1.9.2-p320@jct-ana) (develop) ✗
    501 17604 11329   0   0:00.00 ttys001    0:00.00 grep postgres
  </code></pre>
  
  <p>But does this tell me that PostgreSQL is running?</p>
  
  <pre><code>✪ launchctl load -w /usr/local/Cellar/postgresql/9.1.4/homebrew.mxcl.postgresql.plist                                                        (ruby-1.9.2-p136) 
  homebrew.mxcl.postgresql: Already loaded
  </code></pre>
  
  <p>How do I fix this? what am I not seeing?</p>
  
  <p>PS: <code>~/Library/LaunchAgents</code> includes two PostgreSQL .plist files. I am not sure if that's relevant.</p>
  
  <pre><code>org.postgresql.postgres.plist
  homebrew.mxcl.postgresql.plist
  </code></pre>
  
  <p>I tried the following and got a result as below.</p>
  
  <p><strong>$ psql -p 5432 -h localhost</strong>   </p>
  
  <pre><code>psql: 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?
  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" (fe80::1) and accepting
      TCP/IP connections on port 5432?
  </code></pre>
  
  <p>I've read since that this is occuring because OS&nbsp;X installs its own version of PostgreSQL and Homebrew installs a different version in a different place and the PostgreSQL commands are looking in the /tmp/ directory. You'll need to search more on Stack&nbsp;Overflow, but basically you symlink PostgreSQL so that anything looking in that tmp path actually finds the real path, if that makes sense.</p>
  
  <p>This is the link where I found a few more things to try, specifically doing the symlink as per above, <em><a href="https://stackoverflow.com/questions/17240516">Mac OSX Lion Postgres does not accept connections on /tmp/.s.PGSQL.5432</a></em>. I still wish someone would put together a decent explanation of the concepts behind installing PostgreSQL on OS&nbsp;X and why it's all so difficult.</p>
  
  <p>Latest insights to help with troubleshooting:</p>
  
  <pre><code>$ which psql // This tells you which PostgreSQL you are using when you run $ psql. 
  </code></pre>
  
  <p>Then run:</p>
  
  <pre><code>$ echo $PATH
  </code></pre>
  
  <p>The key thing to take into account is this:</p>
  
  <p><strong>Ensure that the path entry for the copy of PostgreSQL you want to run COMES BEFORE the path to the OS&nbsp;X system's PostgreSQL.</strong></p>
  
  <p>This is a core requirement which decides which PostgreSQL gets run and is what I'm told leads to most of these issues.</p>