Cannot Connect to PostgreSQL Database on Heroku Using Ruby - Failed to Translate Host Name

I use Ruby (not Rails) and mount a PostgreSQL database. I'm trying to configure on Heroku, but I am having problems running the application. Running the application locally works fine.

My local .env looks like this:

postgres://DATABASE_URL=localhost 

And connecting to Ruby to connect to the database is as follows:

 @@db = PGconn.open(:hostaddr => ENV['DATABASE_URL'], :dbname => '(dbname)', :password => '(password)') 

When I click on Heroku, the application crashes to this line and writes this error to the logs:

 could not translate host name "postgres://(my heroku db address)" to address: Name or service not known (PG::Error) 

The database address matches DATABASE_URL in my heroku:config . I use a shared database.

I tried using :host => ENV['DATABASE_URL'] (as opposed to :hostaddr ), but had the same result. I assume that there is something simple, but I did not have good ideas.

+7
source share
2 answers

You need to parse specific parts of DATABASE_URL . See https://devcenter.heroku.com/articles/rack#database_access

+8
source

Heroku Devcenter doesn't seem to include this anymore, so here's how to do the splitting manually:

  db_parts = ENV['DATABASE_URL'].split(/\/|:|@/) username = db_parts[3] password = db_parts[4] host = db_parts[5] db = db_parts[7] conn = PGconn.open(:host => host, :dbname => db, :user=> username, :password=> password) 

Courtesy of Grio .

+4
source

All Articles