简体   繁体   中英

Cannot connect to the postgresql database locally using python and heroku postgres

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.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM