Search code examples
ruby-on-railspostgresqldockerheroku

Heroku - Ruby on Rails - PG::ConnectionBad: could not translate host name to address: Name or service not known


I am trying to push my Docker container into Heroku and I keep having this error when I try to do heroku run rake db:migrate:

PG::ConnectionBad: could not translate host name "db" to address: Name or service not known

I have been searching around and added: POSTGRES_HOST_AUTH_METHOD: trust to my docker-compose.yml and also kept the same names for the environment in both docker-compose.yml and database.yml, but it keeps giving the same error and I am quite lost.

This is the docker-compose.yml:

version: "3.8"

services:
  db:
    image: postgres
    environment:
      POSTGRES_HOST_AUTH_METHOD: trust
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_HOST: db
      RAILS_ENV: production

This is the database.yml:

default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: user
  password: password
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: _rails_ecommerce_site_development

test:
  <<: *default
  database: _rails_ecommerce_site_test

production:
  <<: *default
  database: _rails_ecommerce_site_production

And these are the logs from heroku logs --tail:

2022-04-19T11:07:38.574966+00:00 heroku[web.1]: Starting process with command `rails server -b 0.0.0.0`
2022-04-19T11:07:42.805650+00:00 app[web.1]: => Booting Puma
2022-04-19T11:07:42.805662+00:00 app[web.1]: => Rails 6.0.4.4 application starting in production
2022-04-19T11:07:42.805662+00:00 app[web.1]: => Run `rails server --help` for more startup options
2022-04-19T11:07:44.787345+00:00 app[web.1]: Puma starting in single mode...
2022-04-19T11:07:44.787369+00:00 app[web.1]: * Version 4.3.10 (ruby 2.6.6-p146), codename: Mysterious Traveller
2022-04-19T11:07:44.787370+00:00 app[web.1]: * Min threads: 5, max threads: 5
2022-04-19T11:07:44.787370+00:00 app[web.1]: * Environment: production
2022-04-19T11:07:44.787625+00:00 app[web.1]: * Listening on tcp://0.0.0.0:24849
2022-04-19T11:07:44.787902+00:00 app[web.1]: Use Ctrl-C to stop
2022-04-19T11:07:45.114352+00:00 heroku[web.1]: State changed from starting to up
2022-04-19T11:07:47.793790+00:00 heroku[router]: at=info method=GET path="/" host=xxxx.herokuapp.com request_id=0368ded8-ba57-474b-b540-4f23292881d1 fwd="119.105.11.94" dyno=web.1 connect=0ms service=109ms status=500 bytes=1827 protocol=https
2022-04-19T11:08:36.612997+00:00 app[api]: Starting process with command `rake db:migrate` by user xxxxx
2022-04-19T11:09:06.965267+00:00 heroku[run.7786]: State changed from starting to up
2022-04-19T11:09:07.057271+00:00 heroku[run.7786]: Awaiting client
2022-04-19T11:09:07.084913+00:00 heroku[run.7786]: Starting process with command `rake db:migrate`
2022-04-19T11:09:12.164888+00:00 heroku[run.7786]: State changed from up to complete
2022-04-19T11:09:11.892289+00:00 heroku[run.7786]: Process exited with status 1

Is there anything I am missing here? How could I fix it? Thanks!


Solution

  • Solved!

    So, basically we need to add the Postgres add-on in Heroku. The steps are here (https://betterprogramming.pub/how-to-containerize-and-deploy-apps-with-docker-and-heroku-b1c49e5bc070) but basically:

    1. Navigate to Heroku and click on the app you created.
    2. In Resources, search “postgres” and click on the Heroku Postgres.
    3. Add it to your app. Click Provision.

    And REALLY important, in the database.yml of the project the production should have this: url: <%= ENV['DATABASE_URL'] %>. In my case:

    production:
      <<: *default
      database: _rails_ecommerce_site_production
      url: <%= ENV['DATABASE_URL'] %>
    

    The <%= ENV['DATABASE_URL'] %> will be given by Heroku once we add the Postgresql add-on