Search code examples
ruby-on-railsrubypostgresql

error connecting external database with rails


Guys im trying to connect my application to an external database to consume it.

I ad the config in database.yml at the end of file.

external_db:
  adapter: postgresql
  encoding: utf8
  database: db_name
  host: https://hostdb.com.br/
  username: user
  password: pass
  port: 5734

I created a model for it.

class ExternalDb < ApplicationRecord
  self.abstract_class = true
  establish_connection :external_db
end

Well.. after this I tried to make a simple consult with

x = ExternalDb.connection.execute("select * from PRODUTOS;")

And I got the follow error:

/home/rails/deploy/shared/vendor/bundle/ruby/3.0.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/postgresql_adapter.rb:87:in `rescue in new_client': could not translate host name "https://hostdb.com.br/" to address: Name or service not known (ActiveRecord::ConnectionNotEstablished)
/home/rails/deploy/shared/vendor/bundle/ruby/3.0.0/gems/pg-1.5.4/lib/pg/connection.rb:819:in `connect_start': could not translate host name "https://hostdb.com.br/" to address: Name or service not known (PG::ConnectionBad)

Solution

  • https://hostdb.com.br/ is a URL specifically connecting to a web server on hostdb.com.br using the https protocol. You don't want to connect to a web server, you want to connect to a database.

    hostdb.com.br is the "host", the network name of the server where the database resides.