Ruby-on-rails 连接被拒绝(PGError)(postgresql 和 rails)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16904997/
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 22:06:27  来源:igfitidea点击:

Connection refused (PGError) (postgresql and rails)

ruby-on-railspostgresqlpostgresql-9.1postgresql-9.2rails-postgresql

提问by Daniel Gaeta

I keep getting this error when i try to run my localhost using "$rails s":

当我尝试使用“$rails s”运行我的本地主机时,我不断收到此错误:

(Mac OSX 10.8.3) (ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.3.0]) (Rails 3.2.11) (psql (PostgreSQL) 9.2.2 ) **installed with homebrew

(Mac OSX 10.8.3) (ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.3.0]) (Rails 3.2.11) (psql (PostgreSQL) 9.2.2 ) **与自制软件一起安装

I have been doing a lot of uninstalling postgresql and reinstalling so I have a hunch that there may be conflicting libraries somewhere...i just dont know where to start.

我一直在卸载 postgresql 并重新安装,所以我预感到某处可能存在冲突的库……我只是不知道从哪里开始。

I had Postgresql 9.1 and 9.2 in the same folder and just moved 9.1 into the trash.

我将 Postgresql 9.1 和 9.2 放在同一个文件夹中,然后将 9.1 移到了垃圾箱中。

Here is the output when I run "rails s" in the terminal

这是我在终端中运行“rails s”时的输出

Danny$ rails s
^[b=> Booting Thin
=> Rails 3.2.11 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `initialize': could        not connect to server: Connection refused (PGError)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5433?
could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5433?
could not connect to server: Connection refused
Is the server running on host "localhost" (fe80::1) and accepting
TCP/IP connections on port 5433?
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `new'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `connect'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:326:in `initialize'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `new_connection'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `checkout_new_connection'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:247:in `block (2 levels) in checkout'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `loop'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `block in checkout'
from /Users/Danny/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:239:in `checkout'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:102:in `block in connection'
from /Users/Danny/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:101:in `connection'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/model_schema.rb:308:in `clear_cache!'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-3.2.11/lib/active_record/railtie.rb:97:in `block (2 levels) in <class:Railtie>'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:418:in `_run__3642716200177086041__prepare__4186317719333288752__callbacks'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:405:in `__run_callback'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-3.2.11/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-3.2.11/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/application/finisher.rb:47:in `block in <module:Finisher>'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `run'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `each'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/application.rb:136:in `initialize!'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /Users/Danny/Dropbox/programming/coding/f_app/config/environment.rb:7:in `<top (required)>'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `block in require'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
from /Users/Danny/Dropbox/programming/coding/f_app/config.ru:3:in `block in <main>'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.4.5/lib/rack/builder.rb:51:in `instance_eval'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.4.5/lib/rack/builder.rb:51:in `initialize'
from /Users/Danny/Dropbox/programming/coding/f_app/config.ru:in `new'
from /Users/Danny/Dropbox/programming/coding/f_app/config.ru:in `<main>'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.4.5/lib/rack/builder.rb:40:in `eval'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.4.5/lib/rack/builder.rb:40:in `parse_file'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.4.5/lib/rack/server.rb:200:in `app'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/commands/server.rb:46:in `app'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.4.5/lib/rack/server.rb:304:in `wrapped_app'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/rack-1.4.5/lib/rack/server.rb:254:in `start'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/commands/server.rb:70:in `start'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/commands.rb:55:in `block in <top (required)>'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/commands.rb:50:in `tap'
from /Users/Danny/.rvm/gems/ruby-2.0.0-p195/gems/railties-3.2.11/lib/rails/commands.rb:50:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

回答by Erwin Brandstetter

The error message is instrumental:

错误消息是有用的:

could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5433?
could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5433?

port

港口

You may be trying to connect to the wrong port.
Standard port is 5432. Check how (and whether at all) you started your postgres server:

您可能正在尝试连接到错误的端口。
标准端口是5432。检查您是如何(以及是否)启动您的 postgres 服务器的:

postgres@db:~$ ps -auxww | grep ^postgres
... <stripped more lines>
postgres  1274  0.0  0.3 1437240 57308 ?       S    May27   5:01 /usr/lib/postgresql/9.1/bin/postgres -D /var/lib/postgresql/9.1/main -c config_file=/etc/postgresql/9.1/main/postgresql.conf

The manual has related information here.

手册在这里有相关信息。

In my example, settings from /etc/postgresql/9.1/main/postgresql.confgot used, which says (among many other settings):

在我的示例中,使用了设置/etc/postgresql/9.1/main/postgresql.conf,其中说(在许多其他设置中):

port = 5432

Orrun:

或者运行:

netstat -nlp | grep postgres

Orjust look here (at least in Debian or Ubuntu):

或者只是看这里(至少在 Debian 或 Ubuntu 中):

ls -lA /var/run/postgresql/

PostgreSQL picks the next free port if you create a new database cluster. Since you installed repeatedly, you may have confused port numbers.

如果您创建新的数据库集群,PostgreSQL 会选择下一个空闲端口。由于您反复安装,您可能混淆了端口号。

listen_addresses

listen_addresses

Or you just forgot to allow TCP/IP connections. Related answers:

或者您只是忘记允许 TCP/IP 连接。相关回答:

回答by Ankush Sahu

This may resolve the issue :-

这可能会解决问题:-

  • Get the hba.conf file address by using command SHOW config_file;and SHOW hba_file;in sql prompt

  • Now open hba.confand add this host all all 0.0.0.0/0 trust

  • Now open postgresql.confand add listen_address = '*'

  • 使用命令SHOW config_file;SHOW hba_file;sql提示符获取hba.conf文件地址

  • 现在打开hba.conf并添加这个host all all 0.0.0.0/0 trust

  • 现在打开postgresql.conf并添加listen_address = '*'

回答by Anwar

In my case, the problem was caused by the upgrade of postgresql-9.4 to postgresql-9.5 in Ubuntu 16.04. Since, There were two versions installed at some point in time, the later installed version i.e. postgresql-9.5's config changed to default port of 5433(instead of standard default 5432).

就我而言,问题是由 Ubuntu 16.04 中的 postgresql-9.4 升级到 postgresql-9.5 引起的。因为,在某个时间点安装了两个版本,后来安装的版本,即 postgresql-9.5 的配置更改为默认端口5433(而不是标准默认5432)。

The problem occurred when my Rails project tried to connect to the default port of postgresql (since the port was not defined explicitly defined in database.ymlfile) and kept failing. Even after removal of postgresql-9.4 later.

当我的 Rails 项目尝试连接到 postgresql 的默认端口时出现问题(因为该端口未在database.yml文件中明确定义)并且一直失败。即使在删除 postgresql-9.4 之后。

Solution changing postgresql's config file

更改 postgresql 配置文件的解决方案

The solution is changing the port in the configuration of updated postgresql (9.5). To do this open the file /etc/postgresql/9.5/main/postgresql.confand change the line

解决方案是在更新的 postgresql (9.5) 的配置中更改端口。为此,请打开文件/etc/postgresql/9.5/main/postgresql.conf并更改行

port = 5433                             # (change requires restart)

to

port = 5432                             # (change requires restart)

and then restart the server with sudo service postgresql restart.

然后用 重启服务器sudo service postgresql restart

Solution with changing Rails' `database.yml' file

更改 Rails 的“database.yml”文件的解决方案

Alternatively, you can change rails' database.ymlfile by explicitly mention the new port (that ie 5433) without changing the postgresql's config file. To do this, simply add a line like this

或者,您可以database.yml通过明确提及新端口(即5433)来更改 rails 的文件,而无需更改 postgresql 的配置文件。为此,只需添加这样的一行

port: 5433

and restart the rails server.

并重新启动 rails 服务器。

回答by Chaitanya Yadav

sudo -u postgres pg_ctlcluster 9.3 main stop

followed by

其次是

sudo -u postgres pg_ctlcluster 9.3 main restart

worked for me

对我来说有效

回答by Dark Matter

I got this error message hooking up to Jira (from the Jira side) and the solution was to change the "Hostname or IP address of the database server." to "localhost". If you're running locally you can't just put in your local ip address.

我收到了这条连接到 Jira(从 Jira 端)的错误消息,解决方案是更改“数据库服务器的主机名或 IP 地址”。到“本地主机”。如果您在本地运行,则不能只输入本地 IP 地址。

Also the next issue for a local install is the password always fails even though that's correct because pg_hba.conf needs to be edited so the authentication method is "trust".

此外,本地安装的下一个问题是密码总是失败,即使这是正确的,因为 pg_hba.conf 需要编辑,因此身份验证方法是“信任”。

回答by Chandra

In postgresql.conf, /var/lib/pgsql/data/postgresql.conf :
1. change the listen_addresses = 'localhost' to listen_addresses = '*'
2. enable the default port as 5432, port = 5432
Also in pg_hba.conf, /var/lib/pgsql/data/pg_hba.conf: try the authentication method as trust

在 postgresql.conf 中,/var/lib/pgsql/data/postgresql.conf :
1. 将 listen_addresses = 'localhost' 更改为 listen_addresses = '*'
2. 启用默认端口为 5432,端口 = 5432
同样在 pg_hba.conf 中, /var/lib/pgsql/data/pg_hba.conf: 尝试认证方式为trust