【问题标题】:Connection refused (PGError) (postgresql and rails)连接被拒绝 (PGError) (postgresql 和 rails)
【发布时间】:2013-05-30 02:11:33
【问题描述】:

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

(Mac OSX 10.8.3) (红宝石 2.0.0p195(2013-05-14 修订版 40734)[x86_64-darwin12.3.0]) (轨道 3.2.11) (psql (PostgreSQL) 9.2.2 ) **随自制软件安装

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

我在同一个文件夹中有 Postgresql 9.1 和 9.2,只是将 9.1 移到了垃圾箱中。

这是我在终端中运行“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>'

【问题讨论】:

    标签: ruby-on-rails postgresql postgresql-9.1 postgresql-9.2 rails-postgresql


    【解决方案1】:

    错误信息是有帮助的:

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

    端口

    您可能尝试连接到错误的端口。
    标准端口是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.

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

    port = 5432
    

    运行:

    netstat -nlp | grep postgres
    

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

    ls -lA /var/run/postgresql/
    

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

    listen_addresses

    或者你只是forgot to allow TCP/IP connections。相关答案:

    【讨论】:

    • 非常感谢!很有帮助
    【解决方案2】:

    这可能会解决问题:-

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

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

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

    【讨论】:

    • unrecognized configuration parameter "listen_address" in file "/etc/postgresql/10/main/postgresql.conf" line 59
    【解决方案3】:

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

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

    解决办法修改postgresql的配置文件

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

    port = 5433                             # (change requires restart)
    

    port = 5432                             # (change requires restart)
    

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

    更改 Rails 的 `database.yml' 文件的解决方案

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

    port: 5433
    

    并重新启动 rails 服务器。

    【讨论】:

    • 这对于知道 database.yml 默认使用 rails 端口 5432 非常有用。也很好地解释了为什么多个版本的 postgres 的端口号不同
    【解决方案4】:
    sudo -u postgres pg_ctlcluster 9.3 main stop
    

    紧随其后

    sudo -u postgres pg_ctlcluster 9.3 main restart
    

    为我工作

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:

        在 postgresql.conf 中,/var/lib/pgsql/data/postgresql.conf

        1. 更改listen_addresses = 'localhost' to listen_addresses = '*'

        2. 启用默认端口为5432,port = 5432

        同样在 pg_hba.conf, /var/lib/pgsql/data/pg_hba.conf, 尝试将身份验证方法设为信任

        【讨论】:

          猜你喜欢
          • 2017-08-05
          • 2012-06-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-23
          • 2017-08-08
          • 2021-03-23
          相关资源
          最近更新 更多