Search code examples
ruby-on-railsdatabaseheroku

How to configure 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 have never seen this syntax before in database.yml. Does anyone know how to configure this?

It looks a lot different than 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:

Thanks


Solution

  • For Heroku you will have to use postgresql as it doesn't support mysql2. 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. It will be easier for you to use sqlite for 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
    

    Above code is not enough to get it working on heroku yet, you will also need to edit the gemfile content like below :

    gem 'pg', :group => :production
    gem 'mysql2' , :group => :development
    gem 'sqlite3', :group => :test
    

    I have made the gemfile code according to the database.yaml code that i wrote. You can use mysql2 for both development and test environment. If you are doing so you can change the gemfile contents like below:

    gem 'pg', :group => :production
    gem 'mysql2' , :group => [:development, :test]
    

    Hope this helps.. :)