Ruby on Rails:我该如何编辑postgresql的database.yml文件?

46

创建一个 Rails 应用程序。

当前的 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数据库。

我该如何做?

8个回答

98

简而言之:

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

来源: 配置Rails应用程序


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)

6
如Zabba所说,这是关于IT技术的内容。
development:
  adapter: postgresql
  encoding: unicode
  database: blog_development
  pool: 5
  username: blog
  password:

如在配置Rails应用程序中所提到的。但是,您可能需要额外添加min_messages: WARNING,以消除postgresql在迁移期间给出的讨厌的NOTICE消息。因此,我的database.yml条目看起来像这样。

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

5

3

只需简单使用

rails new app_name --database=postgresql

如果存在应用程序,则尝试

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

3

另一种方法是将公共值放在 &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文件中。


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

0
development:
 adapter: postgresql
 encoding: utf8
 database: best_solution
 username: username
 password: password
 host: localhost
 pool: 2
 timeout: 5000

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