How to set up database.yml for deployment to Heroku
I recently upgraded to the newest version of Rails and I don't understand how to deploy applications to Heroku.
Here is my database.yml
file
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
I've never seen this syntax before in database.yml. Does anyone know how to set this up?
It's so different from what I'm used to
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:
thank
source to share
For Heroku, you will need to use postgresql as it does not support mysql2. Heroku has its own database engine, which you can read about here: https://devcenter.heroku.com/articles/heroku-postgresql
As such, referring to the "heroku" databases and the local databases you define in this file are completely different. You will find it easier to use sqlite for your local and test environments, and for production you should change your yaml code to this:
development:
adapter: mysql2
encoding: utf8
database: my_app_development
pool: 5
username: root
password:
test:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
database: my_database_production
pool: 5
timeout: 5000
The above code is not enough to make it work for hero yet, you will also need to edit the content of the gemfile as shown below:
gem 'pg', :group => :production
gem 'mysql2' , :group => :development
gem 'sqlite3', :group => :test
I made the gemfile code according to the database.yaml code I wrote. You can use mysql2 for development and testing environment. If you do, you can change the content of the gemfile as shown below:
gem 'pg', :group => :production
gem 'mysql2' , :group => [:development, :test]
Hope this helps .. :)
source to share
in yaml
<<: *default
this means you are adding by default to the group, so this
default: &default
adapter: postgresql
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
equal to this
development:
adapter: postgresql
pool: 5
timeout: 5000
database: db/development.sqlite3
source to share
I don't know if this is really exactly what you want, but I just took a while to figure it out, so I thought I'd post it here.
ActiveRecord supports retrieving database connection information from both the environment variable DATABASE_URL
and the database.yml file. Something that can be used in any situation can be a little tricky. Fortunately, this has mostly been taken care of - see this pull request for most of the details.
The short version is that if your database.yml file does not have a url key, the information in DATABASE_URL
will automatically override database.yml for anything it can set that is present in the url, including username, password, server, port, adapter and database name. If there is a url key in database.yml, then this overrides the DATABASE_URL
env var and everything else it conflicts with in the database.yml entry.
Since Heroku automatically installs the full one DATABASE_URL
, everything except the pool and encoding is taken from it, and entries for other things in the production section in the database.yml file are ignored. Therefore, you can leave them blank or install something more convenient for you on your dev machine.
Since you can put ERB in yml files, some sources recommend that you explicitly put
production:
url: <%= ENV['DATABASE_URL'] %>
in your .yml database to explicitly set url as env var. This makes it clearer that everything in the env var is meant to override everything in the yml.
source to share