Postgresql adapter (pg): could not connect to server
Solution 1
Try adding host: localhost
to your database.yml. (Based on: https://stackoverflow.com/a/10793186/919641)
Solution 2
Your Pg
gem was compiled against the PostgreSQL libpq pre-installed in Mac OS X and you're using the psql
that you installed in a newer version, or vice versa.
This can be worked around by specifying a TCP/IP connection, by adding localhost
to database.yml
, but it's better to compile the Pg
gem against the libpq
for the server you're actually running. To do that, you should be able to set the PATH
environment variable to the folder with the correct pg_config
in it before compiling. In your case that'll be somewhere within Postgres.app
.
Solution 3
If it does not work even after adding host: localhost
, remove the postmaster.pid
rm /usr/local/var/postgres/postmaster.pid
Solution 4
you should add host: localhost
to your db config...
Solution 5
Since This was the first post that appeared in my search result, I have decided to post the updated fix for this. Since none of the above suggestion worked for me.
brew postgresql-upgrade-database
It upgraded postgresql data and moved my old version. I was on 9.6 instead of 10.4
Found the solution here
sparkle
Updated on September 23, 2020Comments
-
sparkle over 3 years
I get this error every this I run my Rails app (It cannot connect to my local Postgresql)
/Users/leonardo/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/ active_record/connection_adapters/postgresql_adapter.rb:1208:in `initialize': could not connect to server: No such file or directory (PG::Error) Is the server running locally and accepting connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I'm using Postgres.app that it's correctly running.
If I run
$ psql
I can login properly to Postgresql console.
$ which psql /Applications/Postgres.app/Contents/MacOS/bin/psql
Gemfile
source 'https://rubygems.org' ruby "1.9.3" gem 'rails', '3.2.11' gem "pg"
database.yml
development: adapter: postgresql encoding: unicode username: leonardo password: database: zapping port: 5432
Postgresql (Console)
$ psql leonardo=# \l
-
Rizon over 11 yearsCraig, can you please elaborate this one? Where and which env. variable should I set exactly?
-
wik over 10 yearsre-installation of the pg gem were also required
-
haslo over 10 yearsThanks, this answer helped me fix it. The steps I took were simple: 1)
gem uninstall pg
, 2)bundle install
, done. -
rwheadon about 10 yearsThis solution, ultimately in my case, would have fixed my issue with Rails not being able to start. I went a little different approach on the way to realizing this. I actually created a symlink as described in This Post
-
BvuRVKyUVlViVIc7 about 9 yearsIm not sure why i get downvoted, I answered 5 minutes before! the accepted answer (which has the same solution like my post)?
-
Jason Kim almost 8 yearsThanks. In my case, I had to add
host: db
. -
Mike Vallano over 6 yearsThis solved the problem that I had. Even though running
brew services start postgresql
said it was successful, runningps x | grep postgres
showed no server running. I followed the steps here (which include removing thepostmaster.pid
) and it solved the problem for me: coderwall.com/p/zf-fww/… -
Joshua F. Rountree over 6 yearsThis works if your computer crashes and you do a forced shutdown but postgres wasn't properly terminated, sometimes the pid file will get stuck there... removing it helps get it going again.
-
SexxLuthor over 6 yearsPerfect answer, and although updating
database.yml
works too, I think this is probably the best and most correct solution for this problem. If you've, say, installed Postgresql via Homebrew after installingpg
, you might see this error. Then follow @haslo's instructions above. -
Craig Ringer over 6 yearsNo no no do NOT do this! It's only OK if you verify that no postgres processes are still running. It should rarely if ever be necessary; it can only arise if some other process happens to get assigned the pid the postmaster had.
-
Pradyumna Shembekar over 6 yearsShould be okay for local postgres connections.
-
Christoph about 6 yearsThis fixed it for me. I had uninstalled/reinstall postgres and the gem needed to be rebuilt.
-
David Greco over 5 yearsThis was my problem, I don't understand the reply above from @CraigRinger what is the harm in this? It was immediately rebuilt for me by the service when it finally started.
-
Craig Ringer over 5 years@DavidGreco
postmaster.pid
serves as an interlock that stops one postgres from starting while another is still writing to the database file. If you remove it, you might have two (or more) postgres processes writing to the same database without coordinating with each other, resulting in massive database corruption. So do not do it unless you have made absolutely sure no other postgres processes are surviving and accessing the same data directory.