'How to connect to Heroku postgres db from WSL2?

I have a flask app running on Heroku with postgres database.

When trying to run the app localy on WSL2, the app runs, but when there is interaction with the db it curshes with the following error:

connection to server at "x-x-x-x-169.compute-1.amazonaws.com" (x.xxx.xxx.169), port 5432 failed: FATAL:  database "xxxxxx828vkthi
" does not exist

(I have replaced the numbers with x)

I have opened port 5432 on windows, but still the same error appear.

Any suggestions on how to do it?

My setup on the flask app is

app = Flask(__name__)
DATABASE_URI = os.environ['DATABASE_URL']
DATABASE_URI= DATABASE_URI[:8]+'ql' + DATABASE_URI[8:]
engine = create_engine(DATABASE_URI)
db=scoped_session(sessionmaker(bind=engine))


Solution 1:[1]

Nothing should prevent this from working on the Heroku side. Just make sure to always use the connection information provided by DATABASE_URL (it can change at any time) and to use sslmode=require. You can get the current value of the DATABASE_URL by running

heroku config:get DATABASE_URL

Since this is an outbound connection you shouldn't have to open any ports on WSL or Windows. Out of the box, this outbound connection should work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris