24

I'm doing this tutorial (http://dmix.ca/2008/09/how-to-scrape-websites-in-ruby-on-rails-using-scrubyt/) and step 4 before I begin is to set up the database.yml file. Not sure what that means. Could someone please explain?

j0k
  • 22,600
  • 28
  • 79
  • 90
Zack Shapiro
  • 6,648
  • 17
  • 83
  • 151

2 Answers2

72

The database.yml is the file where you set up all the information to connect to the database. It differs depending on the kind of DB you use. You can find more information about this in the Rails Guide or any tutorial explaining how to setup a rails project.

The information in the database.yml file is scoped by environment, allowing you to get a different setting for testing, development or production. It is important that you keep those distinct if you don't want the data you use for development deleted by mistake while running your test suite.

Regarding source control, you should not commit this file but instead create a template file for other developers (called database.yml.template). When deploying, the convention is to create this database.yml file in /shared/config directly on the server.

With SVN: svn propset svn:ignore config "database.yml"

With Git: Add config/database.yml to the .gitignore file or with git-extra git ignore config/database.yml


... and now, some examples:

SQLite

adapter: sqlite3
database: db/db_dev_db.sqlite3
pool: 5
timeout: 5000

MYSQL

adapter: mysql
database: my_db
hostname: 127.0.0.1
username: root
password: 
socket: /tmp/mysql.sock
pool: 5
timeout: 5000

MongoDB with MongoID (called mongoid.yml, but basically the same thing)

host: <%= ENV['MONGOID_HOST'] %>
port: <%= ENV['MONGOID_PORT'] %>
username: <%= ENV['MONGOID_USERNAME'] %>
password: <%= ENV['MONGOID_PASSWORD'] %>
database: <%= ENV['MONGOID_DATABASE'] %>
# slaves:
#   - host: slave1.local
#     port: 27018
#   - host: slave2.local
#     port: 27019
marcgg
  • 65,020
  • 52
  • 178
  • 231
  • why its a bad practice to checkin database.yml file in version control? I understand we should use ENV variables for database config like production. But I still don't see any reason to not have it tracked in git with basic config like development, test and ci available to all devs. What are your views on this? – Ajit Singh Apr 05 '18 at 16:42
  • Please include a source that explains why the file should not be committed. I don't see any reference to this in the Rails documentation https://edgeguides.rubyonrails.org/configuring.html. Regarding the .env and secrets.yml, I understand why it would be a bad idea to commit it, but not for this file, given that all variables that you need to hide can be put in `<%= ENV[...] %>`. It's not even in the .gitignore of Rails https://github.com/github/gitignore/blob/master/Rails.gitignore – Cyril Duchon-Doris Jun 26 '20 at 12:17
19

The database.yml is a file that is created with new rails applications in /config and defines the database configurations that your application will use in different environments. Read this for details.

Example database.yml:

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: mysql
  encoding: utf8
  database: your_db
  username: root
  password: your_pass
  socket: /tmp/mysql.sock
  host: your_db_ip     #defaults to 127.0.0.1
  port: 3306           
Makis Tsantekidis
  • 2,698
  • 23
  • 38
cmpolis
  • 3,051
  • 21
  • 19
  • If the `database.yml` file is checked into source control, wouldn't it be a Really Bad Thing™ if any database password were checked in - especially one from production? – Makoto Jan 12 '15 at 15:44
  • 1
    @Makoto yes it's a risk. One option is to use environment variables. For example, Heroku uses the `DATABASE_URL` environment variable that contains data such as the username, password, and location of the DB. It then parses that env var and uses it to create a `database.yml` file. – Dennis Feb 13 '15 at 19:03
  • What if the file is *never* created? We have a new application and the config/database.yml file is just nowhere to be found. – Diana Mar 12 '20 at 14:43