简体   繁体   English

连接被拒绝(PGError)(postgresql 和 rails)

[英]Connection refused (PGError) (postgresql and rails)

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>'

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?

port港口

You may be trying to connect to the wrong port.您可能正在尝试连接到错误的端口。
Standard port is 5432 .标准端口是5432 Check how (and whether at all) you started your postgres server:检查您是如何(以及是否)启动您的 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.conf got used, which says (among many other settings):在我的示例中,使用了/etc/postgresql/9.1/main/postgresql.conf设置,其中说(在许多其他设置中):

port = 5432

Or run:或者运行:

netstat -nlp | grep postgres

Or just 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.如果您创建新的数据库集群,PostgreSQL 会选择下一个空闲端口。 Since you installed repeatedly, you may have confused port numbers.由于您反复安装,您可能混淆了端口号。

listen_addresses

Or you just forgot to allow TCP/IP connections .或者您只是忘记允许 TCP/IP 连接 Related answers:相关回答:

This may resolve the issue :-这可能会解决问题:-

  • Get the hba.conf file address by using command SHOW config_file;使用命令SHOW config_file;获取 hba.conf 文件地址SHOW config_file; and SHOW hba_file;SHOW hba_file; in sql prompt在 sql 提示符下

  • Now open hba.conf and add this host all all 0.0.0.0/0 trust现在打开hba.conf并将此host all all 0.0.0.0/0 trust添加到host all all 0.0.0.0/0 trust

  • Now open postgresql.conf and add listen_address = '*'现在打开postgresql.conf并添加listen_address = '*'

In my case, the problem was caused by the upgrade of postgresql-9.4 to postgresql-9.5 in Ubuntu 16.04.就我而言,问题是由 Ubuntu 16.04 中的 postgresql-9.4 升级到 postgresql-9.5 引起的。 Since, There were two versions installed at some point in time, the later installed version ie postgresql-9.5's config changed to default port of 5433 (instead of standard default 5432 ).由于在某个时间点安装了两个版本,后来安装的版本即 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.yml file) and kept failing.当我的 Rails 项目尝试连接到 postgresql 的默认端口时出现问题(因为该端口没有在database.yml文件中明确定义)并且一直失败。 Even after removal of postgresql-9.4 later.即使在删除 postgresql-9.4 之后。

Solution changing postgresql's config file更改 postgresql 配置文件的解决方案

The solution is changing the port in the configuration of updated postgresql (9.5).解决方案是在更新的 postgresql (9.5) 的配置中更改端口。 To do this open the file /etc/postgresql/9.5/main/postgresql.conf and change the line为此,请打开文件/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.yml file by explicitly mention the new port (that ie 5433 ) without changing the postgresql's config file.或者,您可以通过明确提及新端口(即5433 )来更改 rails 的database.yml文件,而无需更改 postgresql 的配置文件。 To do this, simply add a line like this为此,只需添加这样的一行

port: 5433

and restart the rails server.并重新启动 rails 服务器。

sudo -u postgres pg_ctlcluster 9.3 main stop

followed by其次是

sudo -u postgres pg_ctlcluster 9.3 main restart

worked for me为我工作

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."我收到了这条连接到 Jira(从 Jira 端)的错误消息,解决方案是更改“数据库服务器的主机名或 IP 地址”。 to "localhost".到“本地主机”。 If you're running locally you can't just put in your local ip address.如果您在本地运行,则不能只输入本地 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 需要编辑,因此身份验证方法是“信任”。

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

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

  2. enable the default port as 5432, port = 5432启用默认端口为 5432, port = 5432

Also in pg_hba.conf, /var/lib/pgsql/data/pg_hba.conf , try the authentication method as trust同样在 pg_hba.conf, /var/lib/pgsql/data/pg_hba.conf ,尝试身份验证方法为 trust

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Heroku推问题第2部分 - Postgresql - PGError关系不存在 - Ruby on Rails - Heroku Push Problem part 2 - Postgresql - PGError Relations does not exist - Ruby on Rails PGError:错误:关系“delayed_jobs”不存在(Postgresql,rails 3.04,delayed_job错误) - PGError: ERROR: relation “delayed_jobs” does not exist (Postgresql, rails 3.04, delayed_job error) Rails db:create using PostgreSQL return PG::ConnectionBad: 无法连接到服务器:连接被拒绝 - Rails db:create using PostgreSQL return PG::ConnectionBad: could not connect to server: Connection refused 如何在Docker上将Rails与Postgresql连接? 正在获取:无法连接到服务器:连接被拒绝 - How to connect rails with postgresql on docker? Getting: could not connect to server: Connection refused 连接拒绝开发Rails服务器 - Connection refused to development Rails server Dockernized Nginx +(Rails + Unicorn)+ Redis + PostgreSQL:connect()失败(111:连接被拒绝) - Dockernized Nginx + (Rails+Unicorn)+Redis+PostgreSQL:connect() failed (111: Connection refused) 闲置后出现“ PGError:与服务器无连接” - “PGError: no connection to the server” after idle Rails邮件程序连接被拒绝 - localhost - Rails mailer Connection refused - localhost 独角兽,nginx连接在rails 4中被拒绝 - unicorn, nginx connection refused in rails 4 Rails docker postgres 连接被拒绝 - Rails docker postgres Connection refused
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM