Search code examples
ruby-on-railspostgresqlherokuheroku-postgres

Switched my Rails database to PostgreSQL for Heroku, and now the app can't access the DB at all


I'm really stumped on this one. I've tried so many things.

I had to switch from SQLite3 to PostgreSQL so I can deploy on Heroku. Everything worked fine when I ran it locally. There's one error that happens after I git push heroku master. It starts to deploy and I get this:

Rails couldn't infer whether you are using multiple databases from your database.yml and can't generate the tasks for the non-primary databases. If you'd like to use this feature, please simplify your ERB.

Which also happens when I run heroku rake except it's followed by this:

rake aborted! Psych::BadAlias: Cannot load database configuration: Unknown alias: default

followed by stack trace.

I assume I screwed something up along the way. I just don't know what it's expecting, or where I should be looking. My database.yml looks like this:

production: adapter: postgresql encoding: utf8 pool: 15 <<: *default url: *db url from heroku* timeout: 5000

Any help is greatly appreciated. I'm new to all this. i've tried dropping and reseting the db. The last bit of evidence I can provide is the log from Heroku:

heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=ohmydog.herokuapp.com request_id=6217a180-f9ef-43da-8bb0-fc5d064e11fb fwd="185.232.22.206" dyno= connect= service= status=503 bytes= protocol=https


Solution

  • You actually have to do close to no configuration to get Postgres to work on Heroku as it provides the almost the entire configuration via ENV["DATABASE_URL"] which is automatically merged with (and takes precedence over) the settings from database.yml.

    default: &default
      adapter: postgresql
      encoding: unicode
      pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 15 } %>
    
    development:
      <<: *default
      database: my_app_development
    
    test:
      <<: *default
      database: my_app_test
    
    production:
      <<: *default
    

    This is actually a fully functioning config for Postgres.app locally and Heroku. I would really leave almost everything at the defaults and only tune the DB if needed. Prefer using ENV["DATABASE_URL"] locally as well for setting stuff like passwords and usernames as it avoids developer wars.