0

我正在使用 rails 3.2.9 和 postgres 数据库。

我已经通过以下方式编辑了 database.yml 文件,并且gem pg也安装了..

当我运行 bundle 命令时,它会在该列表中显示 pg gem。但是,如果我运行 rails server 命令,它会显示未建立活动连接。

    development:
      adapter: postgresql
      database: db_pchamara/development.postgresql
      pool: 5
      username: database username
      password: password
      host: database ip address
    
    test:
      adapter: postgresql
      database: database name/test.postgresql
      pool: 5
      username:database username
      password: password
    
    production:
      adapter: postgresql
      database: database name/production.postgresql
      pool: 5
      username: database username
      password: password

但是当我运行命令时rails server,它显示如下:

[root@vdimc04 my_app]# rails server
=> Booting WEBrick
=> Rails 3.2.9 application starting in development on http
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-12-06 12:49:35] INFO  WEBrick 1.3.1
[2012-12-06 12:49:35] INFO  ruby 1.9.3 (2012-11-10) [i686-linux]
[2012-12-06 12:49:35] INFO  WEBrick::HTTPServer#start: pid=7536 port=3000


Started GET "/assets/rails.png" for 127.0.0.1 at 2012-12-06 12:49:53 +0530

ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished):
  activerecord (3.2.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
  activerecord (3.2.9) lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
  activerecord (3.2.9) lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection'
  activerecord (3.2.9) lib/active_record/query_cache.rb:67:in `rescue in call'
  activerecord (3.2.9) lib/active_record/query_cache.rb:61:in `call'
  activerecord (3.2.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  activesupport (3.2.9) lib/active_support/callbacks.rb:405:in `_run__626989987__call__446659449__callbacks'
  activesupport (3.2.9) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.9) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  activesupport (3.2.9) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.9) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/reloader.rb:65:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.9) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.9) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.9) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.9) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.9) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.9) lib/rails/engine.rb:479:in `call'
  railties (3.2.9) lib/rails/application.rb:223:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.9) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /usr/local/rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /usr/local/rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /usr/local/rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'


  Rendered /usr/local/rvm/gems/ruby-1.9.3-p327/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.3ms)
  Rendered /usr/local/rvm/gems/ruby-1.9.3-p327/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (3.0ms)
  Rendered /usr/local/rvm/gems/ruby-1.9.3-p327/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (29.4ms)
    
Please help me to solve this problem.
4

1 回答 1

1

问题在于文件的配置:

development:
  adapter: postgresql
  database: db_pchamara/development.postgresql
  pool: 5
  username: database username
  password: password
  host: database ip address

将其更改为:

development:
  adapter: postgresql
  database: name_of_database
  pool: 5
  username: db_user
  password: db_pass
  host: db_host

大多数时候 db_host 只是 localhost

另外,在实际尝试连接 rails 之前,请确保您可以连接 psql。

如果数据库不存在,则必须使用

rake db:create

欢呼;)

于 2012-12-06T08:17:48.343 回答