Ruby-on-rails Postgresql 适配器 (pg):无法连接到服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14225038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:16:38  来源:igfitidea点击:

Postgresql adapter (pg): could not connect to server

ruby-on-railspostgresqlrails-postgresqlpg

提问by sparkle

I get this error every this I run my Rails app (It cannot connect to my local Postgresql)

我每次运行 Rails 应用程序时都会收到此错误(它无法连接到我的本地 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.appthat it's correctly running.

我正在使用正确运行的Postgres.app

If I run

如果我跑

$ psql

I can login properly to Postgresql console.

我可以正确登录到 Postgresql 控制台。

$ 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

数据库.yml

development:
  adapter: postgresql
  encoding: unicode
  username: leonardo
  password: 
  database: zapping
  port: 5432  

Postgresql(Console)

Postgresql(控制台)

$ psql
leonardo=# \l

enter image description here

在此处输入图片说明

回答by pjam

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

尝试添加host: localhost到您的 database.yml。(基于:https: //stackoverflow.com/a/10793186/919641

回答by Craig Ringer

Your Pggem was compiled against the PostgreSQL libpq pre-installed in Mac OS X and you're using the psqlthat you installed in a newer version, or vice versa.

您的Pggem 是针对 Mac OS X 中预装的 PostgreSQL libpq 编译的,并且您使用的psql是在较新版本中安装的 PostgreSQL ,反之亦然。

This can be worked around by specifying a TCP/IP connection, by adding localhostto database.yml, but it's better to compile the Pggem against the libpqfor the server you're actually running. To do that, you should be able to set the PATHenvironment variable to the folder with the correct pg_configin it before compiling. In your case that'll be somewhere within Postgres.app.

这可以通过指定 TCP/IP 连接、添加localhost到 来解决database.yml,但最好Pg针对libpq您实际运行的服务器编译gem 。为此,您应该能够在编译之前将PATH环境变量设置为正确的文件夹pg_config。在您的情况下,它将在Postgres.app.

回答by Pradyumna Shembekar

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

如果添加后仍然不起作用host: localhost,请删除postmaster.pid

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

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

回答by BvuRVKyUVlViVIc7

you should add host: localhostto your db config...

你应该添加host: localhost到你的数据库配置...

回答by Mr. Rene

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

它升级了 postgresql 数据并移动了我的旧版本。我在 9.6 而不是 10.4

Found the solution here

这里找到了解决方案

回答by L. Stepanek

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。Postgres 最近一定在我的电脑上停止运行了,所以我必须通过启动 postgres 服务器来确保它正在运行

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

回答by Jared Menard

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

我有这个问题。这里的评论之一帮助我解决了这个问题。

Thanks, this answer helped me fix it. The steps I took were simple: 1) gem uninstall pg, 2) bundle install, done. – haslo Dec 3 '13 at 20:27

谢谢,这个答案帮助我解决了这个问题。我采取的步骤很简单:1)gem uninstall pg,2)bundle install,完成。– 哈斯洛 2013 年 12 月 3 日 20:27

gem uninstall pg
bundle install

回答by paneer_tikka

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:

我在 Mac 上遇到了同样的问题。原来我的路径上的 psql 无法正常工作。尝试通过键入以下内容启动 psql:

~/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 pgadminand it was connecting fine, I knew it wasn't an issue with the server.

如果您看到这一点,则表明您路径上的 psql 二进制文件正在尝试使用套接字进行连接,但无法连接(无论出于何种原因)。由于我已经下载了pgadmin并且连接正常,我知道这不是服务器的问题。

Fixed itby adding the right version of pgsql to my PATH:

通过将正确版本的 pgsql 添加到我的 PATH 来修复它

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

Now psql (and rails) are happy!

现在 psql(和 rails)很高兴!

回答by the-teacher

find / -name 'postgresql.conf'

find / -name 'postgresql.conf'

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

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

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

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

FROM:unix_socket_directories = '/tmp'

来自:unix_socket_directories = '/tmp'

TO:unix_socket_directories = '/var/pgsql_socket'

至:unix_socket_directories = '/var/pgsql_socket'

sudo mkdir /var/pgsql_socket

须藤 mkdir /var/pgsql_socket

sudo chmod 777 /var/pgsql_socket

须藤 chmod 777 /var/pgsql_socket

回答by jmontross

For heroku this is all you need.

对于heroku,这就是你所需要的。

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