简体   繁体   English

Ruby on Rails连接到正确的Postgres DB

[英]Ruby on rails connect to right Postgres DB

I have developed a website using RoR + Postgresql, migrated it to Heroku using Git. 我已经使用RoR + Postgresql开发了一个网站,并使用Gi​​t将其迁移到了Heroku。 All works well. 一切正常。

Then I imported the website and the databases from Heroku back to local (on another computer). 然后,我将网站和数据库从Heroku导入回本地(在另一台计算机上)。 All went well too. 一切也顺利。

However, I now have 4 databases locally: 但是,我现在在本地有4个数据库:

myapp myapp_test myapp_development myapp_production myapp myapp_test myapp_development myapp_production

When testing my app locally, it turns out it is communicating with myapp_development. 在本地测试我的应用程序时,事实证明它正在与myapp_development通信。

But all my data is in the DB myapp . 但是我所有的数据都在数据库myapp

How do I instruct RoR to use the DB myapp ? 如何指示RoR使用数据库myapp

If I understand you correct - I can provide next solution. 如果我理解您的正确-我可以提供下一个解决方案。 Here is example of config/database.yml 这是config / database.yml的示例

login: &login
  adapter: postgresql
  encoding: utf8
  host: localhost
  username: user_name
  password: password

development:
  <<: *login
  database: db_name_development

test:
  <<: *login
  database: db_name_test

production:
  <<: *login
  database: db_name_production

Try to replace: 尝试更换:

development:
  <<: *login
  database: db_name_development

With: 带有:

development:
  <<: *login
  database: myapp_development

I hope it will help you. 希望对您有帮助。

Rails uses database configuration from two sources - ENV['DATABASE_URL'] and config/database.yml . Rails使用来自两个来源的数据库配置ENV['DATABASE_URL']config/database.yml

ENV['DATABASE_URL'] is an env var that is set in the shell, operating system or when involking the process. ENV['DATABASE_URL']是在外壳程序,操作系统或调用进程时设置的环境变量。

For an example setting ENV['DATABASE_URL'] to: 例如,将ENV['DATABASE_URL']为:

postgresql://localhost/myapp

Would connect to the correct database via postgres. 将通过postgres连接到正确的数据库。 This is how Heroku sets which database your production app connects to. 这是Heroku设置生产应用程序连接到哪个数据库的方式。

config/database.yml is just a YAML file that is read by ActiveRecord when setting up the database. config/database.yml只是一个YAML文件,它在设置数据库时由ActiveRecord读取。 It contains a hash of hashes - which key is used is determined by Rails.env (which is set via ENV['RAILS_ENV'] ). 它包含哈希值的哈希值-使用哪个键由Rails.env (通过ENV['RAILS_ENV'] )确定。

common: &common
  adapter: postgresql
  encoding: utf8
  host: localhost

development:
  <<: *common # this is YAML syntax to merge this hash with &common
  database: myapp

The values in ENV['DATABASE_URL'] are merged with those from config/database.yml but ENV['DATABASE_URL'] has preceedence. ENV['DATABASE_URL']中的值与config/database.yml的值合并,但是ENV['DATABASE_URL']具有优先级。

A friendly tip: 友善提示:

  • Just set the bare minimum in config/database.yml - everything such as usernames, passwords etc should be set via ENV['DATABASE_URL'] for security reasons and to avoid developer wars. 只需在config/database.yml设置最低要求-出于安全原因和避免开发人员之争,应通过ENV['DATABASE_URL']设置用户名,密码等所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM