繁体   English   中英

将Postgresql数据库与Ruby Application集成

[英]Integrate Postgresql database with Ruby Application

我刚刚开始构建我的第一个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

当我尝试倾斜数据库时,这是我得到的错误。

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预先构建的,并且我已经设置了表,主键和外键。 我不确定这是否就是为什么它拒绝工作的原因。 任何帮助将非常感激。 谢谢

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

尝试使用PGAdmin3(随附的数据库管理员)打开它并创建一个新的BD

接下来在您的gem文件中添加此

gem 'pg'

在终端上运行捆绑安装。

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

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

您可能需要配置pg_hba.conf以在没有密码的情况下登录localhost。

cheerz

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

bundle install

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

大家好,我终于找到了解决我遇到的不同问题的方法。 事实证明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命令,瞧:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM