# Using Chef to set the database name, username, and password

**URL:** <https://www.ruby-forum.com/t/using-chef-to-set-the-database-name-username-and-password/237263>\
**Category:** Rails\
**Created:** [November 1, 2013, 6:59pm UTC](https://www.ruby-forum.com/t/using-chef-to-set-the-database-name-username-and-password/237263 "2013-11-01T18:59:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [November 1, 2013, 6:59pm UTC](https://www.ruby-forum.com/t/using-chef-to-set-the-database-name-username-and-password/237263/1 "2013-11-01T18:59:53Z")

</div>

One of my Ruby on Rails apps is Bargain Stock Funds  
([https://github.com/jhsu802701/bsf](https://github.com/jhsu802701/bsf)), and it relies on a Ruby gem called  
bsf\_scrape ([https://github.com/jhsu802701/bsf\_scrape](https://github.com/jhsu802701/bsf_scrape)) to obtain and  
process  
data on stock funds and then save the results in a PostgreSQL database.

As you well know, the config/database.yml file needs the database name,  
the  
username, and the password. My procedures for handling this work but are  
clunky and probably not considered to be best practices. From what I’ve  
heard, using Chef is the best practice. However, Chef is complicated,  
and  
I’m having difficulty making sense of it.

In the source code for bsf\_scrape (Ruby gem), I have functions in the  
lib/bsf\_scrape.rb file that do the following:

1. Set the database name (stores it in a file)
2. Get the database name (read it from a file)
3. Set the path to the directory containing the username and password  
(necessary for the production environment, as the database username is  
different from the server username): Please note that while the database  
name is the same in the development and production environments, the  
username and password are different.
4. Get the path to the directory containing the username and password  
(production environment)
5. Reads the username from a file (production environment)
6. Reads the password from a file (production environment)

In the source code for bsf (Rails app), I have procedures that obtain  
the  
database parameters during the deployment process. Running the  
deployment  
script (config/deploy.rb) involves running the update script  
(config/update.rb), that provides the database name, database username,  
and  
database password and retains these parameters in files for use by the  
bsf\_scrape Ruby gem.

I know this process is convoluted, but I can’t replace it until I figure  
out how to do it correctly in Chef. Are there any good Chef  
tutorials/examples that will help me with this? My needs aren’t that  
complex at the moment, but most of the tutorials/examples I’ve looked at  
are.
