如何将Postgre数据库添加到现有的Rails项目中

4

我创建了一个不包含数据库的新Rails项目(rails new myApp -O)。 现在我该如何添加Postgresql数据库?

1个回答

4
pg宝石添加到您的gemfile中。
# Use postgresql as the database for Active Record
gem 'pg'

接下来,如果你没有这个文件,你需要在你的config目录下创建一个database.yml文件,因此进入该目录并创建名为database.yml的文件,其内容应该如下所示。

config/database.yml

default: &default
adapter: postgresql
encoding: unicode
username: your username for your Postgresql access
password: your password for your Postgresql access
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: The name for your **development** database that you created (probably in PGAdmin?)

test:
  <<: *default
  database: The name for your **test** database that you created (probably in PGAdmin?)

production:
  <<: *default
  database: The name for your **production** database that you created (probably in PGAdmin?)

2
可能需要在config/application.rb中添加一行代码以激活active_record。还有配置测试生成器等等。如果这是一个全新的应用程序,最好重新生成。 - Sergio Tulentsev
1
我完全同意,如果有这个选项,重新构建会更容易。 - Rockwell Rice
感谢Rockwell和Sergio,我会考虑这个选项(重新开始项目),但我也会尝试添加数据库并使事情正常运行,我可能会学到有趣的东西! - romss182
希望我的回答能够帮到你,但我可能会漏掉某些深藏在某处的东西。 - Rockwell Rice

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接