Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ghost opened this issue May 30, 2020 · 5 comments

Comments

@ghost
Copy link

ghost commented May 30, 2020

When at the installation step of docker-compose run --rm web rails db:migrate is run I get the error message:

Starting mastodon_redis_1 ... done
Starting mastodon_db_1    ... done
rails aborted!
PG::ConnectionBad: could not translate host name "db" to address: Name or service not known
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/pg-1.2.3/lib/pg.rb:58:in `initialize'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/pg-1.2.3/lib/pg.rb:58:in `new'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/pg-1.2.3/lib/pg.rb:58:in `connect'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/postgresql_adapter.rb:692:in `connect'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/postgresql_adapter.rb:223:in `initialize'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/postgresql_adapter.rb:48:in `new'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/postgresql_adapter.rb:48:in `postgresql_connection'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `try_to_checkout_new_connection'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:814:in `acquire_connection'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:538:in `checkout'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1033:in `retrieve_connection'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_handling.rb:90:in `connection'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:172:in `migrate'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/strong_migrations-0.6.8/lib/strong_migrations/database_tasks.rb:4:in `migrate'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/railties/databases.rake:60:in `block (2 levels) in <top (required)>'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/command.rb:48:in `invoke'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.3/lib/rails/commands.rb:18:in `<top (required)>'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `block in require'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:257:in `load_dependency'
/opt/mastodon/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `require'
/opt/mastodon/bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Running docker logs on the mastodon_db_1 container shows this error message:

Error: Database is uninitialized and superuser password is not specified.
       You must specify POSTGRES_PASSWORD to a non-empty value for the
       superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".

       You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
       connections without a password. This is *not* recommended.

       See PostgreSQL documentation about "trust":

I have tried docker-compose run -e POSTGRES_PASSWORD=password --rm web rails db:migrate to the same error message.

This is my docker compose file. I have all the containers on an internal network.
https://gist.github.com/y2klol/f785835402ae366c167ef79e712be692

@wjtnk
Copy link

wjtnk commented May 30, 2020

in docker-compose.yml
add "POSTGRES_HOST_AUTH_METHOD=trust" like this

  db:
    restart: always
    image: postgres:9.6-alpine
    shm_size: 256mb
    networks:
      - internal_network
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres"]
    volumes:
      - ./postgres:/var/lib/postgresql/data
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust

@ghost
Copy link
Author

ghost commented May 31, 2020

This solved the issue. I was able to run docker-compose run --rm web rails db:migrate successfully, then docker-compose run --rm web rails assets:precompile

@ghost ghost closed this as completed May 31, 2020
@ghost
Copy link
Author

ghost commented May 31, 2020

in docker-compose.yml
add "POSTGRES_HOST_AUTH_METHOD=trust" like this

  db:
    restart: always
    image: postgres:9.6-alpine
    shm_size: 256mb
    networks:
      - internal_network
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres"]
    volumes:
      - ./postgres:/var/lib/postgresql/data
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust

May I as why it is safe to add "POSTGRES_HOST_AUTH_METHOD=trust" when the error message says "This is not recommended."
I trust your judgement but I am curious.

@SuperSandro2000
Copy link
Contributor

You have no password set for your postgres container and everything can connect to it in the default Docker configuration. If you publish the port and have no firewall you basically have an open postgres on the internet. Not recommend. Set a password.

@blitzblade
Copy link

set the env variables correctly:

db:
    image: postgres
    restart: always
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    networks:
      - internal_network

Make sure the your app is on the same network as the db (internal_network) and provide the correct env variables.

web:
   ...
    depends_on:
      - db
    networks:
      - internal_network
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_HOST: db

Finally, make sure you're using the same values in database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  host: <%= ENV["POSTGRES_HOST"] %>
  user: <%= ENV["POSTGRES_USER"] %>
  password: <%= ENV["POSTGRES_PASSWORD"] %>

NB: If you have a volume attached, make sure it doesn't introduce existing details. Remove it or deal with that however you want.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants