每次我运行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>'

我运行的是Mavericks OS X 10.9,所以我不知道这是否是问题所在。我已经尽力了,但似乎都没用。我已经卸载和安装postgres和pg gem多次了。

这是我的数据库。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并不能解决这个问题,这对我来说是有效的:

cd /usr/local/var/
rm -rf postgres/
mkdir postgres
initdb --locale=C -E UTF-8 postgres/
brew services restart postgresql

这要归功于@spirito_libero在这个线程上的解决方案。

其他回答

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

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

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

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

我今晚在开发一个rails应用程序时遇到了这个问题,我已经开发了一段时间。我的问题归结于我的postgresql服务器没有运行。

我转到屏幕顶部(我用的是Mac),点击小大象图标,然后点击“开始”。

我们的服务器没有打开。

希望这能为某些人提供一个简单的解决方案。

我的问题出在申请上。yml文件。我在heroku上的数据库url没有使用5342端口。检查你的heroku配置var DATABASE_URL。确保它与应用程序中的完全匹配。Yml为适用的数据库。

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

正如@Magne所描述的,PostgreSQL的主要/次要版本升级(例如9.5 -> 9.6或9 -> 10)后,会出现PG::ConnectionBad - could not connect to server: Connection refused错误。

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

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

你可以检查这是否是问题通过检查最新的酿造公式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小于最新的酿造公式,并且差异是主要/次要版本更改,那么这可能是您的问题。

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

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

步骤1。确保PostgreSQL关闭:

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

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

$ 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 flag指定当前数据目录 -D flag指定要创建的新数据目录 -b指定旧的二进制文件 -B指定要升级到的新二进制文件

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

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

步骤6。将新创建的数据目录移动到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/,并进行了一些补充)