当我运行rake:db从我的rails应用程序迁移时,我得到了这个输出:
rake aborted! PG::ConnectionBad: 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? /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `initialize' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `new' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `connect' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:568:in `initialize' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `new' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:435:in `new_connection' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:445:in `checkout_new_connection' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:416:in `acquire_connection' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:351:in `block in checkout' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:350:in `checkout' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:541:in `retrieve_connection' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/connection_handling.rb:113:in `retrieve_connection' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/connection_handling.rb:87:in `connection' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/migration.rb:910:in `initialize' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/migration.rb:807:in `new' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/migration.rb:807:in `up' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/migration.rb:785:in `migrate' /home/ubuntu/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/railties/databases.rake:34:in `block (2 levels) in' /home/ubuntu/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval' /home/ubuntu/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in ` '
在我的gemfile中我有
gem 'pg'
在我的database.yml我有
default: &default adapter: postgresql encoding: unicode pool: 5 development: <<: *default database: fake_lunch_hub_development test: <<: *default database: fake_lunch_hub_test production: <<: *default database: fake_lunch_hub_production username: fake_lunch_hub password: <%= ENV['FAKE_LUNCH_HUB_DATABASE_PASSWORD'] %>
这个设置用于完美地连接到数据库,但我做了很多重构和重命名(项目不再被称为fake_lunch_hub),我将项目从我的本地机器(mac)移到亚马逊EC2实例(ubuntu)上.
Stackoverflow上有许多其他问题,它们使用psql提供解决方案,但是当我运行命令时
psql
我明白了:
The program 'psql' can be found in the following packages: * postgresql-client-common * postgres-xc-client Try: sudo apt-get install
我宁愿不下载一个新的postgres包,因为我已经从gem成功下载了它.
如何启动该服务器并接受连接?
编辑:
完整解决方案:
sudo apt-get install postgres sudo su su postgres createuser ubuntu createuser fake_lunch_hub createdb fake_lunch_hub exit exit rails server
解决问题
gem只是Ruby/Rails与Postgres服务器通信的一种方式.宝石本身并没有包含它自己的服务器.
你还需要安装一个:
sudo apt-get install postgresql