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

fad*_*kin 253 ruby database-connection ruby-on-rails pg rails-postgresql

每次我运行我的rails 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>'
Run Code Online (Sandbox Code Playgroud)

我正在运行Mavericks 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
Run Code Online (Sandbox Code Playgroud)

谁能帮我?

Chr*_*ade 596

它可能像过时的PID文件一样简单.它可能会无声地失败,因为您的计算机没有完全完成关闭过程,这意味着postgres没有删除PID(进程ID)文件.

postgres使用PID文件来确保一次只运行一个服务器实例.因此,当它再次启动时,它会失败,因为已经有一个PID文件告诉postgres服务器的另一个实例已启动(即使它没有运行,它只是没有关闭并删除PID) .

  1. 要修复它,请删除/重命名PID文件.找到postgres数据目录.在使用自制程序的MAC上/usr/local/var/postgres/,它可能是其他系统/usr/local/var/log/.
  2. 要确保这是问题所在,请查看日志文件(/usr/var/postgres/).在最后几行,您将看到:

致命:锁文件"postmaster.pid"已经存在
提示:是否有另一个postmaster(PID 347)在数据目录"/ usr/local/var/postgres"中运行?

  1. 如果是这样, server.log
  2. 重启服务器.在使用launchctl(使用自制程序)的mac上,以下命令将重新启动服务器.

    launchctl unload homebrew.mxcl.postgresql.plist  
    launchctl load -w homebrew.mxcl.postgresql.plist
    
    Run Code Online (Sandbox Code Playgroud)

    或者在较新版本的Brew上

    brew services restart postgresql
    
    Run Code Online (Sandbox Code Playgroud)

  • 对于较新的Brew用户,#4的命令是"brew services restart postgresql" (30认同)
  • #2是一个很好的建议:"检查server.log".我的错误实际上是由升级到Yosemite造成的,服务器日志说:`致命:无法打开目录"pg_tblspc":没有这样的文件或目录`.这个答案帮助我解决了这个问题http://stackoverflow.com/questions/25970132/pg-tblspc-missing-after-installation-of-os-x-yosemite-beta (24认同)
  • 我只是赞成这个,因为它给了我足够的解决问题烧焦的地球风格.我卸载了pg(​​`gem uninstall pg`),卸载了postgres(`brew uninstall postgres`),然后修复了postgres文件夹,里面有一堆陈旧的东西(`rm -rf/usr/local/var/postgres) `).重新启动`brew install postgres`,`ln -sfv /usr/local/opt/postgresql/*.plist~/ Library/LaunchAgents`,`launchctl load~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist`,和`ARCHFLAGS =" - arch x86_64"gem install pg`.我不知道在尝试这项工作之前我尝试了多少其他解决方案. (19认同)
  • 对我来说(MAC,自制),日志文件是/usr/local/var/log/postgres.log。 (4认同)
  • postmaster.opts 文件是否与 postmaster.pid 文件相同? (2认同)
  • 每次停电我都会发现自己在这里哈哈哈哈 (2认同)
  • `rm /usr/local/var/postgres/postmaster.pid` 为我修复了它。 (2认同)
  • 组合对我有用:首先`rm /usr/local/var/postgres/postmaster.pid`然后brew服务重新启动postgresql(每次我的mac出现恐慌时我也发现自己在这里!) (2认同)

Jai*_*put 41

经过大量的搜索和分析,我找到了一个解决方案,如果你使用ubuntu只需在你的终端中写下这个命令并点击回车

sudo service postgresql restart
Run Code Online (Sandbox Code Playgroud)

这将重启你的PostgreSQL,希望这对你有帮助.

  • 谢谢1000次.我刚刚升级了我的ubuntu版本,这对我来说是固定的. (3认同)

tec*_*ams 33

当postgres未正常关闭时会出现此问题.以下是我通过三个简单步骤解决此问题的方法.

第1步:转到postgres目录

Mac用户会发现这一点/usr/local/var/postgres,其他人可能会看到/usr/var/postgres/.

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

rm postmaster.pid
Run Code Online (Sandbox Code Playgroud)

第3步:重新启动服务器

Mac用户

brew services restart postgresql
Run Code Online (Sandbox Code Playgroud)

Linux用户

sudo service postgresql restart
Run Code Online (Sandbox Code Playgroud)

最后重新启动你的应用程序,你很高兴.


And*_*rdi 32

我已经设法通过遵循Chris Slade的答案来解决问题,但是要重新启动服务器,我必须使用以下命令:

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

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Run Code Online (Sandbox Code Playgroud)

我在这里找到(pjammer的答案在底部)


Mih*_*kov 25

你的系统中安装了postgresql吗?如果没有,请观看安装postgresql.将postgresql成功集成到系统后,可以在系统终端中键入类似的内容:

which psql
#=> /usr/bin/psql
Run Code Online (Sandbox Code Playgroud)

之后你需要在postgresql中创建一个用户和数据库,如下所示:

sudo su - postgres
psql
Run Code Online (Sandbox Code Playgroud)

然后,您可以在终端中看到以下内容

postgres=#
Run Code Online (Sandbox Code Playgroud)

输入:

CREATE USER yourname WITH PASSWORD 'passwordhere';
CREATE DATABASE metals-directory_production  WITH OWNER yourname;
GRANT ALL PRIVILEGES ON DATABASE metals-directory_production TO yourname;
Run Code Online (Sandbox Code Playgroud)

执行此操作后,您需要更正您的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
Run Code Online (Sandbox Code Playgroud)

另外如果你有postgresql的问题,最好检查一下pg_hba.conf


K M*_*lam 23

要使用Postgres解决这些类型的问题并与Postgres一起使用Mac OSX,这可能是迄今为止我发现的最佳和最简单的解决方案:

http://postgresapp.com/

只需下载,安装并快乐:)


IAm*_*NaN 19

  1. 卸载pg:

    gem uninstall pg
    Run Code Online (Sandbox Code Playgroud)
  2. 卸载postgres:

    brew uninstall postgres
    Run Code Online (Sandbox Code Playgroud)
  3. Nuke postgres文件夹可能会留下一堆陈旧的东西:

    rm -rf /usr/local/var/postgres
    Run Code Online (Sandbox Code Playgroud)
  4. 重启(可能没必要)

  5. 重新安装pg:

    brew install postgres
    Run Code Online (Sandbox Code Playgroud)
  6. 我在克里斯斯莱德的回答中的评论很难开始,现在我使用brew服务,它在很多方面简化了我的生活:

    brew install services
    Run Code Online (Sandbox Code Playgroud)
  7. 并开始使用它:

    brew services start postgresql
    Run Code Online (Sandbox Code Playgroud)
  8. 重新安装gem:

    gem install pg
    Run Code Online (Sandbox Code Playgroud)

和bobsyouruncle.

  • 小心这种方法......这是一种焦土方法,很可能会破坏你目前拥有的任何本地dbs.看看https://github.com/copiousfreetime/launchy你可以做什么`吃午饭停止postgres`然后'lunchy start postgres` (3认同)

Ale*_*xis 13

检查文件postgresql.conf(ubuntu/etc/postgresql/XX/main/postgresql.conf中)并查找说明的行:

listen_addresses="localhost"
Run Code Online (Sandbox Code Playgroud)

尝试将其更改为:

listen_addresses="*"
Run Code Online (Sandbox Code Playgroud)

它将接受每个IP,接下来检查说:

port=5432
Run Code Online (Sandbox Code Playgroud)

并检查你的database.yml是否是同一个端口,默认情况下我的postgresql-9.2使用5433而不是5432,不要忘记重新启动 postgres服务器,

祝好运!


Nei*_*son 11

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

brew upgrade postgresql在PostgreSQL版本9.6发布后运行后出现此错误.问题是主要/次要版本升级需要额外的步骤才能将旧日期迁移到新版本.

如何检查这是否是您的问题

您可以通过检查与自制软件一起安装的最新brew公式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
Run Code Online (Sandbox Code Playgroud)

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

$ cat /usr/local/var/postgres/PG_VERSION
9.5
Run Code Online (Sandbox Code Playgroud)

如果PG_VERSION小于最新的brew公式,并且差异是主要/次要版本更改,那么这可能是您的问题.

如何修复(即如何升级数据)

以下说明适用于从9.5升级到9.6.根据您自己的升级更改版本号

步骤1.确保PostgreSQL已关闭:

$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
# or, with Homebrew...
$ brew services stop postgresql
Run Code Online (Sandbox Code Playgroud)

步骤2.创建一个新的原始数据库:

$ initdb /usr/local/var/postgres9.6 -E utf8
Run Code Online (Sandbox Code Playgroud)

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

$ ls /usr/local/Cellar/postgresql/
9.5.3   9.5.4   9.6.1
Run Code Online (Sandbox Code Playgroud)

请注意,在此示例中,我将从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
Run Code Online (Sandbox Code Playgroud)
  • -d flag指定当前数据目录
  • -D flag指定要创建的新数据目录
  • -b 指定旧的二进制文件
  • -B 指定我们要升级到的新二进制文件

步骤5.移除旧数据目录

$ mv /usr/local/var/postgres /usr/local/var/postgres9.5
Run Code Online (Sandbox Code Playgroud)

步骤6.将新创建的数据目录移动到PostgreSQL期望的目录

$ mv /usr/local/var/postgres9.6 /usr/local/var/postgres
Run Code Online (Sandbox Code Playgroud)

步骤7.再次启动PostgreSQL

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

步骤8.如果您正在为Rails使用pg gem,则应通过卸载并重新安装gem来重新编译(如果您不使用pg gem,请跳过此步骤)

$ gem uninstall pg
$ gem install pg
Run Code Online (Sandbox Code Playgroud)

步骤9.(可选)在您确信一切正常后,您可以使用以下命令重新获得一些磁盘空间:

brew cleanup postgresql
Run Code Online (Sandbox Code Playgroud)

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

rm -rf /usr/local/var/postgres9.5/
Run Code Online (Sandbox Code Playgroud)

(这个答案基于一篇优秀的博客文章https://keita.blog/2016/01/09/homebrew-and-postgresql-9-5/并附加一些内容)


小智 7

如上所述,我刚刚在我的Mac上打开了Postgres应用程序,点击了Open Psql,关闭了psql窗口,在我的终端中重新启动了我的rails服务器,它再次正常工作,没有更多的错误.

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


小智 7

这才是真正帮助我的.

$ cd /usr/local/var/postgres/
$ rm postmaster.pid
Run Code Online (Sandbox Code Playgroud)

参考:http: //alumni.lewagon.org/questions/60


Bas*_*ile 6

在Osx Movaje上更新Mac后,我遇到了同样的问题。

我找到了这个解决方案:

首先尝试在终端中的以下命令行:

brew services restart postgresql
Run Code Online (Sandbox Code Playgroud)

如果没有变化:

ps aux | grep postgres
Run Code Online (Sandbox Code Playgroud)

如果仍然没有变化:

ls -ls | grep post
Run Code Online (Sandbox Code Playgroud)

最后一个修复它的命令,通过从root执行删除了postgres锁定文件:

rm /usr/local/var/postgres/postmaster.pid
Run Code Online (Sandbox Code Playgroud)

然后 :

brew services restart postgresql
Run Code Online (Sandbox Code Playgroud)

来自berziiii:https : //github.com/ga-wdi-boston/capstone-project/issues/325

希望会有所帮助:)

问候 !!


Abe*_*bel 5

放入host: localhost文件database.yml并运行此命令:

rake db:create db:migrate  
Run Code Online (Sandbox Code Playgroud)


Mag*_*gne 5

如果你做了以后打这个问题,brew upgrade它升级到Postgres的一个新的主要版本(f.ex 9.3.09.4.0或更高),那么这样做:

@ 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
Run Code Online (Sandbox Code Playgroud)

就这样.检查导入是否顺利,然后删除备份:

rm outputfile
rm -Rf /usr/local/var/postgres.old
Run Code Online (Sandbox Code Playgroud)

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

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


如果您不使用Homebrew,可能会提供其他一些技巧:

如何手动停止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


Awa*_*qat 5

找到它可能位于/usr/local/var/postgres/或位于的 postgres 文件/usr/var/postgres/,然后删除该postmaster.pid文件夹中存在的文件。


归档时间:

查看次数:

220843 次

最近记录:

6 年 前