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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7689097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:07:10  来源:igfitidea点击:

Ruby on Rails: How can i edit database.yml for postgresql?

ruby-on-railsrubydatabaseruby-on-rails-3postgresql

提问by shibly

rails new app=>

rails 新应用=>

The current database.yml is like that=>

当前的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

I need to edit this for postgresql database.

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

How can i do this ?

我怎样才能做到这一点 ?

回答by Zabba

Simply:

简单地:

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

Source: Configuring Rails Applications

来源:配置 Rails 应用程序

回答by LHH

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)

回答by tessi

As Zabba said it's

正如扎巴所说

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

As mentioned in the Configuring Rails Applications. But you might want an additional min_messages: WARNING, to get rid of the nasty NOTICE messages postgresql gives you during a migration. So my database.ymlentry looks like this

配置 Rails 应用程序中所述。但是您可能需要一个额外的min_messages: WARNING,以摆脱postgresql 在迁移过程中给您的讨厌的 NOTICE 消息。所以我的database.yml条目看起来像这样

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

回答by DonPaulie

You might be interested in generating new app with postgres default:

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

rails new myapp --database=postgresql

as mentioned here: https://devcenter.heroku.com/articles/getting-started-with-rails4

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

回答by Piyush R Gupta

 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

回答by Hardik Hardiya

Simply use

只需使用

rails new app_name --database=postgresql

or if existing application try

或者如果现有的应用程序尝试

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

回答by gagneet

Another way is to have the common values in &default and then have individual values for the other environments, which can be based on environment variables:

另一种方法是在 &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'] %>

Here all the values can come from environment variables (if you use Docker or Bitbucket Pipelines) or you can have them in your .env files.

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