Ruby on Rails:我如何为postgresql编辑database.yml?

时间:2011-10-07 15:02:35

标签: ruby-on-rails ruby database ruby-on-rails-3 postgresql

rails new app =>

当前的database.yml就像是=>

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  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: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

我需要为postgresql数据库编辑它。

我该怎么做?

7 个答案:

答案 0 :(得分:94)

简单地:

development:
  adapter: postgresql
  encoding: unicode
  database: blog_development
  pool: 5
  username: blog
  password:
  host: localhost

来源:Configuring Rails Applications

答案 1 :(得分:14)

development:
  adapter: postgresql
  encoding: utf8
  database: name
  username: hading
  password: my_db_password
  pool: 5 # not mandatory
  timeout: 5000 # not mandatory
  host: localhost
  port: your postgresql port number (5432 or 5433)

答案 2 :(得分:6)

正如扎巴说的那样

development:
  adapter: postgresql
  encoding: unicode
  database: blog_development
  pool: 5
  username: blog
  password:

Configuring Rails Applications中所述。但您可能需要额外的min_messages: WARNING来摆脱the nasty NOTICE messages postgresql gives you during a migration。所以我的database.yml条目看起来像这样

development:
  adapter: postgresql
  encoding: unicode
  database: blog_development
  pool: 5
  username: blog
  password:
  min_messages: WARNING

答案 3 :(得分:4)

您可能有兴趣使用postgres默认生成新的应用程序:

rails new myapp --database=postgresql

如此处所述:https://devcenter.heroku.com/articles/getting-started-with-rails4

答案 4 :(得分:3)

 development:
  adapter: postgresql
  encoding: utf8
  database: name
  username: hading
  password: my_db_password
  host: localhost # not mandatory
  pool: 5 # not mandatory
  timeout: 5000 # not mandatory

答案 5 :(得分:2)

只需使用

rails new app_name --database=postgresql

或如果现有应用程序尝试

 development:
  adapter: postgresql
  encoding: unicode
  database: app_dev
  pool: 5
  username: username
  password: password
  host: localhost

答案 6 :(得分:0)

另一种方法是在&default中使用公用值,然后在其他环境中使用单独的值,这些值可以基于环境变量:

default: &default
  adapter:  postgresql
  encoding: unicode
  port:     <%= ENV.fetch("POSTGRESQL_PORT", "5432") %>
  pool:     <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV['POSTGRESQL_USER_NAME'] %>
  password: <%= ENV.fetch("POSTGRESQL_PASSWORD", "myS3cr3tP4ssw0rd") %>
  host:     <%= ENV['POSTGRESQL_HOST'] %>

development:
  <<: *default
  database: <%= ENV['POSTGRESQL_DB'] %>-development
  host: db

test:
  <<: *default
  database: <%= ENV['POSTGRESQL_DB'] %>-test
  host: db

production:
  <<: *default
  database: <%= ENV['POSTGRESQL_DB'] %>

这里所有值都可以来自环境变量(如果使用Docker或Bitbucket Pipelines),也可以将它们包含在.env文件中。