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
url: <%= ENV['DATABASE_URL'] %>
for production as the comments suggest?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.
Rails 6.0 ships with all the rails tasks you need to use multiple databases in Rails. Running a command like bin/rails db:create will create both the primary and animals databases.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With