Question

I'm curious how to correctly set up my database.yml file in a Rails 4 application.

It's not something I have really looked into in great detail as it all seems to just work when deploying to Heroku, but I want to understand it now and have noticed that the format has changed a little from Rails 4.0 to 4.1. For example

4.0.2

development:
 adapter: mysql2
 encoding: utf8
 database: my_app_development
 pool: 5
 username: root
 password:

test:
 adapter: mysql2
 encoding: utf8
 database: my_app_test
 pool: 5
 username: root
 password:


production:
 adapter: mysql2
 encoding: utf8
 database: ymca_gym_production
 pool: 5
 username: root
 password:

4.1.0

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock

development:
  <<: *default
    database: my_app_development


test:
  <<: *default
   database: my_app_test


 # On Heroku and other platform providers, you may have a full connection URL
 # available as an environment variable. For example:
 #
 #   DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
 #
 # You can use this database configuration with:
 #
 #   production:
 #      url: <%= ENV['DATABASE_URL'] %>
 #

 production:
   <<: *default
     database: my_app_production
     username: my_app
     password: <%= ENV['MY_APP_DATABASE_PASSWORD'] %>

My questions are

  1. Should I be setting usernames and passwords in ALL environments?
  2. I'm using ClearDB on Heroku as my database. Should I be using url: <%= ENV['DATABASE_URL'] %> for production as the comments suggest?
Was it helpful?

Solution

The second database.yml you posted is actually equivalent to the first, it just copies values from the development block.

To answer your other questions:

1) Should I be setting usernames and passwords in ALL environments

you can if you wish, or you can leave it as you have above where it takes the credentials all from one block.

2)If I'm using clear DB with Heroku as my database then should I be uncommenting

heroku actually completely disregards this file (which shouldn't be checked into source control in the first place anyway). Heroku has its own mechanism to handle databases which you can read more about here: https://devcenter.heroku.com/articles/heroku-postgresql

Essentially, treat "heroku's databases" and local databases that you define in this file completely different.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top