开发者

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

开发者 https://www.devze.com 2023-04-11 05:52 出处:网络
Rails new app. Th开发者_如何学Goe current database.yml is like that: # SQLite version 3.x #gem install sqlite3

Rails new app.

Th开发者_如何学Goe current database.yml is like that:

# 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.

How can I do this?


Simply:

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

Source: Configuring Rails Applications


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)


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.yml entry looks like this

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


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

rails new myapp --database=postgresql

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


 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


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


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
  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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号