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

Ruby on rails PG::ConnectionBad-无法连接到服务器:连接被拒绝,ruby-on-rails,ruby,ruby-on-rails-4,database-connection,pg,Ruby On Rails,Ruby,Ruby On Rails 4,Database Connection,Pg,每次运行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?

每次运行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>'
于2013-11-06 23:56:36-0500为127.0.0.1开始获取“/”
PG::ConnectionBad-无法连接到服务器:连接被拒绝
服务器是否在主机“localhost”(::1)上运行并接受
端口5432上的TCP/IP连接?
无法连接到服务器:连接被拒绝
服务器是否在主机“localhost”(127.0.0.1)上运行并接受
端口5432上的TCP/IP连接?
无法连接到服务器:连接被拒绝
服务器是否在主机“localhost”(fe80::1)上运行并接受
端口5432上的TCP/IP连接?
:
activerecord(4.0.0)lib/active\u record/connection\u adapters/postgresql\u adapter.rb:825:in'connect'
activerecord(4.0.0)lib/active\u record/connection\u adapters/postgresql\u adapter.rb:542:in'initialize'
activerecord(4.0.0)lib/active\u record/connection\u adapters/postgresql\u adapter.rb:41:in`postgresql\u connection'
activerecord(4.0.0)lib/active\u record/connection\u adapters/abstract/connection\u pool.rb:440:in'new\u connection'
activerecord(4.0.0)lib/active\u record/connection\u adapters/abstract/connection\u pool.rb:450:in'checkout\u new\u connection'
activerecord(4.0.0)lib/active\u record/connection\u adapters/abstract/connection\u pool.rb:421:in'acquire\u connection'
activerecord(4.0.0)lib/active\u record/connection\u adapters/abstract/connection\u 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\u record/connection\u adapters/abstract/connection\u 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\u record/connection\u adapters/abstract/connection\u pool.rb:264:in'connection'
activerecord(4.0.0)lib/active\u record/connection\u adapters/abstract/connection\u pool.rb:546:in'retrieve\u connection'
activerecord(4.0.0)lib/active\u record/connection\u handling.rb:79:in'retrieve\u connection'
activerecord(4.0.0)lib/active\u record/connection\u handling.rb:53:in'connection'
activerecord(4.0.0)lib/active\u record/migration.rb:792:in'current\u version'
activerecord(4.0.0)lib/active\u record/migration.rb:800:in'needs\u migration'
activerecord(4.0.0)lib/active\u record/migration.rb:379:in'check\u 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\u support/callbacks.rb:373:in``运行调用调用调用调用'
activesupport(4.0.0)lib/active\u support/callbacks.rb:80:in'run\u 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\u dispatch/middleware/debug\u exceptions.rb:17:in'call'
actionpack(4.0.0)lib/action\u dispatch/middleware/show\u 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\u support/taged\u logging.rb:67:in'block in taged'
activesupport(4.0.0)lib/active\u support/taged\u logging.rb:25:in'taged'
activesupport(4.0.0)lib/active\u support/taged\u logging.rb:67:in'taged'
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\u dispatch/middleware/request\u 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'
机架(1.5.2)lib/rack/lock.rb:17:in'call'
actionpack(4.0.0)lib/action\u 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'
机架(1.5.2)lib/rack/content_length.rb:14:in'call'
精简(1.5.1)lib/thin/connection.rb:81:in'block in pre_进程'
精简(1.5.1)lib/thin/connection.rb:79:in'pre_进程'
精简(1.5.1)lib/thin/connection.rb:54:in'process'
精简(1.5.1)lib/thin/connection.rb:39:in'receive_data'
eventmachine(1.0.3)lib/eventmachine.rb:187:in'run'
精简(1.5.1)lib/thin/backends/base.rb:63:in'start'
thin(1.5.1)lib/thin/server.rb:159:in'start'
机架(1.5.2)lib/rack/handler/thin.rb:16:in'run'
机架(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'
railties(4.0.0)lib/rails/commands.rb:73:in`'
箱子/轨道:4:in`'
我运行的是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
开发:
适配器:postgresql
编码:unicode
数据库:metals-U开发目录
游泳池:5
用户名:
密码:
模板:template0
主机:本地主机
港口:5432
测试:&测试
适配器:postgresql
编码:unicode
数据库:metals-U测试
游泳池:5
用户名:
密码:
模板:template0
主机:本地主机
港口:5432
登台:
适配器:postgresql
编码:unicode
数据库:metals-U生产目录
游泳池:5
用户名:
密码:
模板:template0
主机:本地主机
制作:
适配器
which psql
#=> /usr/bin/psql
sudo su - postgres
psql
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;
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
brew services restart postgresql
    launchctl unload homebrew.mxcl.postgresql.plist  
    launchctl load -w homebrew.mxcl.postgresql.plist
listen_addresses="localhost"
listen_addresses="*"
port=5432
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
}
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
rake db:create db:migrate  
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
gem uninstall pg
brew uninstall postgres
rm -rf /usr/local/var/postgres
brew install postgres
brew install services
brew services start postgresql
gem install pg
sudo service postgresql restart
bundle exec rake db:migrate RAILS_ENV=production
$ 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
$ cat /usr/local/var/postgres/PG_VERSION
9.5
$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
# or, with Homebrew...
$ brew services stop postgresql
$ initdb /usr/local/var/postgres9.6 -E utf8
$ ls /usr/local/Cellar/postgresql/
9.5.3   9.5.4   9.6.1
$ 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
$ mv /usr/local/var/postgres /usr/local/var/postgres9.5
$ mv /usr/local/var/postgres9.6 /usr/local/var/postgres
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
# or, if you're running a current version of Homebrew
$ brew services start postgresql
$ gem uninstall pg
$ gem install pg
brew cleanup postgresql
rm -rf /usr/local/var/postgres9.5/
pg_ctl -D /usr/local/var/postgres start && brew services start postgresql
psql postgres
postgres=# \du
CREATE ROLE POSTGRES WITH SUPERUSER CREATEDB CREATEUSER CREATEROLE REPLICATION BYPASSRLS ;
$ cd /usr/local/var/postgres/
$ rm postmaster.pid
ls -ls | grep post
rm /usr/local/var/postgres/postmaster.pid
brew services restart postgresql
cd /usr/local/var/
rm -rf postgres/
mkdir postgres
initdb --locale=C -E UTF-8 postgres/
brew services restart postgresql
brew postgresql-upgrade-database