PG :: ConnectionBad – 无法连接到服务器:连接被拒绝

每当我运行我的轨道4.0服务器,我得到这个输出。

Started GET "/" for 127.0.0.1 at 2013-11-06 23:56:36 -0500 PG::ConnectionBad - could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused Is the server running on host "localhost" (fe80::1) and accepting TCP/IP connections on port 5432? : activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:825:in `connect' activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:542:in `initialize' activerecord (4.0.0) lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in `new_connection' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in `checkout_new_connection' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `acquire_connection' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in `block in checkout' /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `checkout' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection' /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection' activerecord (4.0.0) lib/active_record/connection_handling.rb:79:in `retrieve_connection' activerecord (4.0.0) lib/active_record/connection_handling.rb:53:in `connection' activerecord (4.0.0) lib/active_record/migration.rb:792:in `current_version' activerecord (4.0.0) lib/active_record/migration.rb:800:in `needs_migration?' activerecord (4.0.0) lib/active_record/migration.rb:379:in `check_pending!' activerecord (4.0.0) lib/active_record/migration.rb:366:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__1613334440513032208__call__callbacks' activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks' actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call' better_errors (0.9.0) lib/better_errors/middleware.rb:84:in `protected_app_call' better_errors (0.9.0) lib/better_errors/middleware.rb:79:in `better_errors_call' better_errors (0.9.0) lib/better_errors/middleware.rb:56:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app' railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call' activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged' activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged' activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged' railties (4.0.0) lib/rails/rack/logger.rb:21:in `call' quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets' actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call' rack (1.5.2) lib/rack/methodoverride.rb:21:in `call' rack (1.5.2) lib/rack/runtime.rb:17:in `call' activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call' rack (1.5.2) lib/rack/lock.rb:17:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call' railties (4.0.0) lib/rails/engine.rb:511:in `call' railties (4.0.0) lib/rails/application.rb:97:in `call' rack (1.5.2) lib/rack/content_length.rb:14:in `call' thin (1.5.1) lib/thin/connection.rb:81:in `block in pre_process' thin (1.5.1) lib/thin/connection.rb:79:in `pre_process' thin (1.5.1) lib/thin/connection.rb:54:in `process' thin (1.5.1) lib/thin/connection.rb:39:in `receive_data' eventmachine (1.0.3) lib/eventmachine.rb:187:in `run' thin (1.5.1) lib/thin/backends/base.rb:63:in `start' thin (1.5.1) lib/thin/server.rb:159:in `start' rack (1.5.2) lib/rack/handler/thin.rb:16:in `run' rack (1.5.2) lib/rack/server.rb:264:in `start' railties (4.0.0) lib/rails/commands/server.rb:84:in `start' railties (4.0.0) lib/rails/commands.rb:78:in `block in <top (required)>' railties (4.0.0) lib/rails/commands.rb:73:in `<top (required)>' bin/rails:4:in `<main>' 

我正在运行小牛OS X 10.9,所以我不知道这是否是问题。 我尽我所能,但似乎没有任何工作。 我已经卸载并且现在多次安装postgres和pg gem。

这是我的database.yml文件

 development: adapter: postgresql encoding: unicode database: metals-directory_development pool: 5 username: password: template: template0 host: localhost port: 5432 test: &test adapter: postgresql encoding: unicode database: metals-directory_test pool: 5 username: password: template: template0 host: localhost port: 5432 staging: adapter: postgresql encoding: unicode database: metals-directory_production pool: 5 username: password: template: template0 host: localhost production: adapter: postgresql encoding: unicode database: metals-directory_production pool: 5 username: password: template: template0 host: localhost cucumber: <<: *test 

任何人都可以帮我吗?

它可以像一个陈旧的PID文件一样简单。 这可能会失败,因为你的电脑没有完成关机过程,这意味着postgres没有删除PID(进程ID)文件。 PID文件被postgres使用,以确保一次只有一个服务器实例在运行。 所以当它重新开始时,它会失败,因为已经有一个PID文件告诉postgres服务器的另一个实例已经启动了(即使它没有运行,它也没有closures并删除PID) 。

  1. 要解决它删除/重命名PID文件。 findpostgres数据目录。 在使用自制软件的MAC上,它是/usr/local/var/postgres/ ,其他系统可能是/usr/var/postgres/
  2. 要确定这是问题,请查看日志文件( server.log )。 在最后一行你会看到: FATAL: lock file "postmaster.pid" already exists HINT: Is another postmaster (PID 347) running in data directory "/usr/local/var/postgres"?
  3. 如果是这样, rm postmaster.pid
  4. 重新启动您的服务器。 在使用launchctl(使用自制软件)的Mac上,以下命令将重新启动服务器。

     launchctl unload homebrew.mxcl.postgresql.plist launchctl load -w homebrew.mxcl.postgresql.plist 

    或更新版本的Brew

     brew services restart postgresql 

我已经设法解决这个问题,按照克里斯·斯莱德的答案,但重新启动服务器,我不得不使用下面的命令

launchctl unload〜/ Library / LaunchAgents / homebrew.mxcl.postgresql.plist

launchctl加载〜/ Library / LaunchAgents / homebrew.mxcl.postgresql.plist

我在这里find了 (pjammer的答案在底部)

经过大量的search和分析,我发现如果你使用的是Ubuntu的解决scheme,只需在terminal上写下这个命令,然后按回车

 sudo service postgresql restart 

这将重新启动你的PostgreSQL,希望这会对你有所帮助。

要用Postgres修复这些types的问题,并在Mac OSX上使用Postgres,这可能是迄今为止我发现的最好和最简单的解决scheme:

http://postgresapp.com/

只需下载,安装并开心:)

你的系统中安装了postgresql吗? 如果不是,那么请看安装postgresql 。 在您成功地将postgresql集成到您的系统后,您可以在系统terminal中input类似的内容:

 which psql #=> /usr/bin/psql 

之后,你需要像这样在postgresql中创build一个用户和数据库:

 sudo su - postgres psql 

那么你可以在你的terminal中看到以下内容

 postgres=# 

键入:

 CREATE USER yourname WITH PASSWORD 'passwordhere'; CREATE DATABASE metals-directory_production WITH OWNER yourname; GRANT ALL PRIVILEGES ON DATABASE metals-directory_production TO yourname; 

在你这样做之后,你需要纠正你的database.yml。 可能你需要这样的东西:

 development: adapter: postgresql encoding: unicode database: metals-directory_development pool: 5 username: yourname password: passwordhere ### password you have specified within psql host: localhost port: 5432 ### you can configure it in file postgresql.conf 

另外,如果你有postgresql的问题,检查pg_hba.conf是个好主意

  1. 卸载pg …

     gem uninstall pg 
  2. 卸载postgres …

     brew uninstall postgres 
  3. Nuke postgres文件夹,这可能是一堆陈旧的东西在…徘徊…

     rm -rf /usr/local/var/postgres 
  4. 重新启动(也许不必要)

  5. 重新安装pg …

     brew install postgres 
  6. 我在克里斯·斯莱德的回答中的评论开始艰难的方式,现在我使用酿造服务,这已经在很多方面简化了我的生活…

     brew install services 
  7. …并开始与它…

     brew services start postgresql 
  8. 重新安装gem…

     gem install pg 

和bobsyouruncle。

检查文件postgresql.conf (在Ubuntu上是在/ etc / postgresql / XX / main / postgresql.conf中),并find说

 listen_addresses="localhost" 

尝试改变它

 listen_addresses="*" 

它会接受每个IP,然后检查所说的行

 port=5432 

并检查一下你的database.yml是否是同一个端口,默认情况下在我的postgresql-9.2上用5433代替5432 ,别忘了重启 postgres服务器, 祝你好运

如上所示,我只是打开了我的Mac上的Postgres应用程序,单击打开Psql,closures了psql窗口,重新启动我的terminal,我的rails服务器,它再次工作,没有更多的错误。

相信大象: http : //postgresapp.com/

如@Magne所述,错误PG::ConnectionBad - could not connect to server: Connection refused可以在PostgreSQL的主要/次要版本升级(例如9.5 -> 9.69 -> 10 )之后提交。

在PostgreSQL 9.6版本发布后,我运行了brew upgrade postgresql后的brew upgrade postgresql ,我得到了这个错误。 问题是主要/次要版本升级需要额外的步骤将旧date迁移到新版本。

如何检查这是你的问题

您可以通过检查最新的酿造公式PostgreSQL版本安装自制软件来检查这是否是问题…

 $ brew info postgresql /usr/local/Cellar/postgresql/9.5.4_1 (3,147 files, 35M) Poured from bottle on 2016-10-14 at 13:33:28 /usr/local/Cellar/postgresql/9.6.1 (3,242 files, 36.4M) * Poured from bottle on 2017-02-06 at 12:41:00 

…然后将其与当前的PG_VERSION进行比较

 $ cat /usr/local/var/postgres/PG_VERSION 9.5 

如果PG_VERSION小于最新的brew配方,差异是主要/次要版本更改,那么这可能是您的问题。

如何解决(即如何升级数据)

下面的说明是从9.5升级到9.6。 根据您的升级情况更改版本号

第一步 :确保PostgreSQLclosures:

 $ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist # or, with Homebrew... $ brew services stop postgresql 

第二步:build立一个新的原始数据库:

 $ initdb /usr/local/var/postgres9.6 -E utf8 

第3步。检查旧的和新的二进制版本是什么:

 $ ls /usr/local/Cellar/postgresql/ 9.5.3 9.5.4 9.6.1 

请注意,在这个例子中,我从9.5.4二进制升级到9.6.1二进制

步骤4.使用pg_upgrade实用程序将当前数据迁移到新数据库。

 $ pg_upgrade \ -d /usr/local/var/postgres \ -D /usr/local/var/postgres9.6 \ -b /usr/local/Cellar/postgresql/9.5.4/bin/ \ -B /usr/local/Cellar/postgresql/9.6.1/bin/ \ -v 
  • -d标志指定当前的数据目录
  • -D标志指定要创build的新数据目录
  • -b指定旧的二进制文件
  • -B指定了我们要升级的新的二进制文件

第5步。移开旧的数据目录

 $ mv /usr/local/var/postgres /usr/local/var/postgres9.5 

第6步。将新创build的数据目录移动到PostgreSQL预期的位置

 $ mv /usr/local/var/postgres9.6 /usr/local/var/postgres 

第7步。再次启动PostgreSQL

 $ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist # or, if you're running a current version of Homebrew $ brew services start postgresql 

第8步。如果您使用的是Rails的pg gem,则应该通过卸载并重新安装gem来重新编译(如果您不使用pg gem,请跳过此步骤)

 $ gem uninstall pg $ gem install pg 

第9步。(可选)在您确认一切正常后,您可以使用以下命令重新运行一些磁盘空间:

 brew cleanup postgresql 

…如果你真的很勇敢,可以用下面的命令删除旧的PostgreSQL数据目录

 rm -rf /usr/local/var/postgres9.5/ 

(这个答案是基于一个优秀的博客文章https://keita.blog/2016/01/09/homebrew-and-postgresql-9-5/与一些补充);

把“host:localhost”放在database.yml文件中,然后运行这个命令:rake db:create db:migrate

这个问题出现在postgres没有正常closures的时候。 这是我如何解决这个问题在三个简单的步骤。

第1步:转到您的postgres目录

Mac用户可以在/usr/local/var/postgresfind它,其他人可能会看到/usr/var/postgres/

步骤2:通过运行此命令删除.pid文件。

 rm postmaster.pid 

第3步:重新启动您的服务器

Mac用户

 brew services restart postgresql 

Linux用户

 sudo service postgresql restart 

最后重新启动你的应用程序,你很好去。

如果在升级postgres到一个新的主要版本(f.ex 9.3.09.4.0或更高版本)的brew upgrade之后遇到这个问题,那么执行下面的步骤:

@ dmitrygusev的修复https://github.com/Homebrew/homebrew/issues/35240

以下官方[Postgresql]迁移指南帮助:

 brew switch postgres 9.3.5 # presuming you already installed 9.4.1 pg_dumpall > outputfile launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist mv /usr/local/var/postgres /usr/local/var/postgres.old brew switch postgres 9.4.1 initdb -D /usr/local/var/postgres psql -d postgres -f outputfile 

就这样。 检查导入是否正常,然后删除备份:

 rm outputfile rm -Rf /usr/local/var/postgres.old 

这里的问题是postgres的主要版本升级,有必要重新创build/迁移您的数据库。 并可能chown目录或手动调用initdb

另请参阅: 如何将PostgreSQL从版本9.5升级到版本9.6而不丢失数据?


一些其他的提示,可能会方便,以防您不使用自制软件:

如何手动停止PG服务器:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log stop

如何手动启动PG服务器:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

find你的postgres文件,它可能位于/usr/local/var/postgres//usr/var/postgres/ ,然后删除该文件夹中的postmaster.pid文件。

我只是键入sudo服务postgresql重新启动 ,一切都工作。

我有同样的问题,这个解释为我解决: http : //blog.55minutes.com/2013/09/postgresql-93-brew-upgrade/

关键一步是看我的/usr/local/var/postgres/server.log的尾部,让我看看真正的问题是什么,这是我没有完全完成升级postgres的过程

这绝对是@Chris Slade的回答,帮助了我。

我写了一个脚本来杀死那些剩余的进程,如果有用的话:

 kill_postgres() { if [[ $* -eq "" ]]; then echo "Usage: 'kill_postgres <db_name>' to kill remaining instances (Eg. 'kill_postgres my_app_development')" else gksudo echo "Granted sudo" pids="$(ps xa | grep postgres | grep $* | awk '{print $1}' | xargs)" if [[ $pids -eq "" ]]; then echo "Nothing to kill" else for pid in "${pids[@]}" do echo "Killing ${pid}" sudo kill $pid echo "Killed ${pid}" done kill_postgres $* fi fi } 

我的问题是在我的application.yml文件。 我在heroku上的数据库url没有使用端口5342.检查你的herokuconfigurationvar DATABASE_URL。 确保与您的application.yml中的适用数据库完全匹配。

在生产(开发一切正常),我有同样的问题,在我的情况下,数据库服务器不在应用程序在同一台机器上,所以最后什么工作只是通过写作迁移:

 bundle exec rake db:migrate RAILS_ENV=production 

然后重新启动服务器,一切正常。

我知道这是晚了,但可能会帮助别人。 我有同样的问题。 原来,我会postgres 9.1和9.5的两个版本。 我卸载9.1和9.5,并再次安装9.5,它为我工作。

我有同样的问题。 我检查/ var / log / postgresql中的PostgreSQL日志文件的最后一行。 文件“/etc/postgresql/9.5/main/postgresql.conf”中有一个无法识别的configuration参数。 在postgresql.conf中注释错误行解决了我的问题。

您可能重新启动了您的计算机,并忘记启动Postgres应用程序。

我停止了rails服务器,运行rake db:migrate并启动了我的rails s

在更新postgresql的brew upgrade之后,我遇到了这个错误。 我发现如何解决这个伟大的职位我的问题。 我能够得到postgres备份和运行,甚至迁移到我所有的现有数据库。 https://coderwall.com/p/ti4amw/how-to-launch-postgresql-after-upgrade

Interesting Posts