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

时间:2017-12-29 16:55:01

标签: ruby-on-rails database postgresql

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

1 个答案:

答案 0 :(得分:1)

pg gem添加到您的gemfile中。

# Use postgresql as the database for Active Record
gem 'pg'

然后,如果你没有它,你需要database.yml目录中的config文件,所以去那里并在config目录中创建一个名为database.yml的fole,它应该看起来像像这样。

配置/ 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?)