When trying to access my app locally, I am given: "could not translate host name ... to address" and "Temporary failure in name resolution"

Viewed 5008

I am going through Heroku's getting started tutorial. As far as I know, Postgres is fully updated and everything is installed appropriately. Interestingly, the app works online when I deploy it to heroku, but when I try to run the app locally, not so much! I am getting a:

PG::ConnectionBad

could not translate host name "myname" to address: Temporary failure in name resolution
    ### Convenience alias for PG::Connection.new.
def self::connect( *args )
    return PG::Connection.new( *args )
end

I have not touched any information in the files created by Heroku for this tutorial - so what could be wrong?

The following commands were run:

heroku login
git clone https://github.com/heroku/ruby-getting-started.git
cd ruby-getting-started

heroku create

git push heroku main

heroku open (this gets to the app okay)
heroku logs --tail
which psql

bundle install

export DATABASE_URL=postgres://$(whoami)
bundle exec rake db:create db:migrate (Here is where the error is)

heroku local web
(Error here) http://localhost:5000/

3 Answers

Debug Postgres connection

Confirm that Postgres exists in yout local machine, is running and it have a user that maches with you machine username.

Try to run this command in your local machine:

psql -U $(whoami) -d postgres

The output should be something like this:

psql (9.2.24, server 10.14)
WARNING: psql version 9.2, server version 10.0.
        Some psql features might not work.
Type "help" for help.

postgres=#

Then, confirm that a database matching with your username exists with "\l".

postgres=#\l
                                   List of databases
      Name      |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
----------------+----------+----------+-------------+-------------+-----------------------
 myuser       | odoo     | UTF8     | en_US.UTF-8 | en_US.UTF-8 |

tip: you can close with "\q"

postgres=#\q

if no error, try replacing the value of DATABASE_URL with one of the following...

postgresql://
postgresql://localhost
postgresql://localhost:5433
postgresql://localhost/mydb 
postgresql://user@localhost

If this is not successful, then you must review the way that postgres is installed.

...Or manually create a superadmin User with a password inside Postgres.

The conection string pattern is postgres://user:password@host:port

example...

export DATABASE_URL=postgres://db_user:123456@localhost:5432
DATABASE_URL=postgres://$(whoami)

This is trying to connect to a computer which happens to have the same name as your local user account. Unsurprisingly, it doesn't exist, hence the error.

Here's the command to use if you are trying to make a backup or create a dump file from your local Postgres DB.

Mainly you need to change --host to --port, and that's it:

pg_dump --format t --no-owner --no-privileges --verbose --username postgres --password --dbname <DATABASE_NAME> --port <PORT, usually 5432> --file <THE FILE NAME YOU WANT>.dump

*if you are on Windows, use CMD instead of git bash. idk why.

Related