1

I am working with heroku postgres for the first time. Did everything according to the tutorial, especially this:

set DATABASE_URL=postgres://$(whoami)

Python:

DATABASE_URL = os.environ['DATABASE_URL']

connection = psycopg2.connect(DATABASE_URL, sslmode='require')

cursor = connection.cursor()

Here is the error message I recieve when I try to connect to the database:

(myenv) C:\deployment\myenv\remastered>heroku local

20:46:58 worker.1   |  Traceback (most recent call last):
20:46:58 worker.1   |    File "db.py", line 6, in <module>
20:46:58 worker.1   |      connection = psycopg2.connect(DATABASE_URL, sslmode='require')
20:46:58 worker.1   |    File "C:\deployment\myenv\lib\site-packages\psycopg2\__init__.py", line 122, in connect
20:46:58 worker.1   |      conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
20:46:58 worker.1   |  psycopg2.OperationalError: could not translate host name "$(whoami)" to address: Unknown server error
[DONE] Killing all processes with signal  SIGINT
20:46:58 worker.1   Exited with exit code null

It seems that for some reason it can't translate the "$(whoami)" expression to the database url.

How do I fix this?

Is that supposed to work only when running app on heroku? And when running locally I should specify the url manually?

I get the same error no matter what I inject there instead of $(whoami) unless I copy the url from heroku postgres database credentials for manual connections, but those change periodically so it is not very convinient solution. It connects fine when launching on heroku though.

kxrxkt
  • 11
  • 2
  • Possible duplicate of this question https://stackoverflow.com/questions/39004608/pgconnectionbad-could-not-translate-host-name-error-after-running-export-data. See if the answers suddenly help you, in case you don't leave a comment. – ByteBat Jul 17 '21 at 13:57
  • I think a good way to debug would be to print out DATABASE_URL in your python code so you can tell if its the value you're expecting or not. From the stacktrace it doesn't look like it's been set properly – mayankmehtani Jul 17 '21 at 13:59

1 Answers1

2

Can you try this?

set DATABASE_URL=postgres://%USERNAME%

Unfortunately, there is no built-in way to do command substitution in Windows Command Prompt. $(...) is only available in UNIX shells.

You can also see the following posts:

  • Thanks for answering. Tried this and it did substitute the expression with my username, howerver I still get the same error no matter what I inject there unless I copy the url from heroku postgres database credentials for manual connections, but those change periodically so it is not very convinient solution. It connects fine when launching on heroku though. – kxrxkt Jul 18 '21 at 05:31