3

Is it ok to store DB password for the production environment in the "config/database.yml" file? Or is there any more correct way to do it (maybe environment variables)?

Thanks in advance.

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242

1 Answers1

3

It's not a good idea! One main reason is that the config/database.yml file will probably be included in some kind of source control, like a git repository. Even if the repo is private currently, you can't know for sure it won't be made public in the future and then you would have a problem on your hands!

In addition, if anyone ever gains read-access to your application's files or just a copy of your application's source, they now have your database password.

A typical solution is to set an environment variable like you suggested and then read it in the .yml file:

password: <%= ENV['DATABASE_PASSWORD'] %>

If you're using a PaaS like Heroku, this is the standard way to do things. But even this isn't a perfect solution, so evaluate your options carefully.

Community
  • 1
  • 1
JKillian
  • 18,061
  • 8
  • 41
  • 74