Postgresql adapter (pg): could not connect to server

匿名 (未验证) 提交于 2019-12-03 01:49:02

问题:

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 

回答1:

Try adding host: localhost to your database.yml. (Based on: https://stackoverflow.com/a/10793186/919641)



回答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.



回答3:

If it does not work even after adding host: localhost, remove the postmaster.pid

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



回答4:

you should add host: localhost to your db config...



回答5:

I had the same problem on a Mac. Turns out that the psql I had on my path wasn't working correctly. Try launching psql by typing:

~/projects/some_project/project-rails$ psql   psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"? 

If you see this, it indicates the psql binary on your path is attempting to connect using socket and unable to do so (for whatever reason). Since I had already downloaded pgadmin and it was connecting fine, I knew it wasn't an issue with the server.

Fixed it by adding the right version of pgsql to my PATH:

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

Now psql (and rails) are happy!



回答6:

I had this same issue. You have to actually run / start postgres. Postgres must have stopped running on my computer recently, so I had to make sure it's running by starting the postgres server

postgres -D /usr/local/var/postgres 

Then the following commands (which were causing me the same error you had) all worked:

bundle exec rake db:create db:migrate bundle exec rspec 


回答7:

find / -name 'postgresql.conf'

netstat -an | grep 5432 # => /tmp/.s.PGSQL.5432

vi /Users/admin/Library/Application\ Support/Postgres93/var/postgresql.conf

FROM: unix_socket_directories = '/tmp'

TO: unix_socket_directories = '/var/pgsql_socket'

sudo mkdir /var/pgsql_socket

sudo chmod 777 /var/pgsql_socket



回答8:

For heroku this is all you need.

heroku addons:create heroku-postgresql  production:  adapter: postgresql  encoding: unicode  host: localhost  # For details on connection pooling, see rails configuration guide  # http://guides.rubyonrails.org/configuring.html#database-pooling  pool: 5 


回答9:

I had this issue. One of the comments here helped me fix the issue.

gem uninstall pg bundle install 


回答10:

I just had the problem that the postgres app wasn't running on my mac...



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!