将Postgresql数据库与Ruby Application集成

时间:2012-07-26 00:01:57

标签: ruby-on-rails database postgresql

我刚开始构建我的第一个Ruby on Rails应用程序,为了让我在heroku上托管它,我在应用程序中更改了我的数据库设置。我通过更改应用程序中的database.yml文件来做到这一点。这就是我现在所拥有的。

# SQLite version 3.x
#   gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
  adapter:postgresql
  encoding:unicode
  database:dezirus_dev
  pool:5
  host:localhost
  username:postgres
  password:
  port:5432
 # timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter:postgresql
  encoding:unicode
  database:dezirus_test
  pool:5
  username:postgres
  password:
  host:localhost
  port:5432

production:
  adapter:postgresql
  encoding:unicode
  database:dezirus
  pool:5
  username:postgres
  password:
  host:localhost
  port:5432

当我尝试耙DB时,这是我得到的错误。

RAHMAN@IMLDEV1-LT ~/rails-projects/dezirus
$ rake db:migrate --trace
rake aborted!
no such file to load -- pg
/usr/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler/runtime.rb:68:in `require'
/usr/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler/runtime.rb:68:in `require'
/usr/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler/runtime.rb:66:in `each'
/usr/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler/runtime.rb:66:in `require'
/usr/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler/runtime.rb:55:in `each'
/usr/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler/runtime.rb:55:in `require'
/usr/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler.rb:122:in `require'
/home/RAHMAN/rails-projects/dezirus/config/application.rb:7
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
/home/RAHMAN/rails-projects/dezirus/Rakefile:4
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load_rakefile'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:501:in `raw_load_rakefile'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:82:in `load_rakefile'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:81:in `load_rakefile'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:65:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/bin/rake:33
/usr/bin/rake:23:in `load'
/usr/bin/rake:23

请注意,数据库是使用postgreSQL预建的,我已经设置了我的表,主键和外键。我不确定这可能是它拒绝工作的原因。任何帮助将非常感激。谢谢

3 个答案:

答案 0 :(得分:3)

首先在计算机中安装PG,http://www.postgresql.org/download/

尝试使用PGAdmin3(它自带的DB管理员)打开它并创建一个新的BD

接下来在你的gem文件中添加这个

gem 'pg'

在终端运行bundle install。

在这里,我向您发送我的DB.yml

示例
development:
  adapter: postgresql
  encoding: unicode
  database: billy
  pool: 5
  username: postgres
  password:

您可能需要将pg_hba.conf配置为在本地主机中没有密码的情况下登录。

cheerz

答案 1 :(得分:1)

将gem'pg'添加到您的gem文件中并运行

bundle install

然后转到此处http://www.postgresql.org/download/并将数据库安装到您的计算机上,以便在本地运行您的应用。

答案 2 :(得分:0)

嘿伙计们,我终于找到了解决我遇到的不同问题的解决方案。事实证明,cygwin pg gem不支持我的PostgreSQL版本,我使用了ruby安装程序并安装了它而没有退缩。然后我不得不将这行代码添加到boot.rb文件

require 'yaml'
YAML::ENGINE.yamler = 'syck'

YML文件也需要看起来像这样。

development:
  adapter: postgresql
  encoding: unicode
  database: dezirus_dev
  pool: 5
  username: postgres
  password:
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: postgresql
  encoding: unicode
  database: dezirus_test
  username: postgres
  password:
production:
  adapter: postgresql
  encoding: unicode
  database: dezirus
  username: sudo
  password: *******

然后我运行了rake db:migrate命令并瞧瞧:)