0

I have a PostgreSQL DB hosted in heroku and I want to get access from other applications which aren't hosted in heroku, but I saw in the DB settings that the credentials are not permanent.

How can I get access from other application always having updated credentials?

1 Answer 1

1

Heroku recommends using the Heroku CLI to fetch fresh credentials every time you run your external application:

Always fetch the database URL config var from the corresponding Heroku app when your application starts. For example, you may follow 12Factor application configuration principles by using the Heroku CLI and invoke your process like so:

DATABASE_URL=$(heroku config:get DATABASE_URL -a your-app) your_process

This way, you ensure your process or application always has correct database credentials.

In this example, your_process will see an environment variable called DATABASE_URL that is set to the same value as the DATABASE_URL config far on the Heroku app called your-app.

Since you are using Python, here is one way to access that value:

import os


database_url = os.getenv("DATABASE_URL", default="some_default_for_local_development")
Sign up to request clarification or add additional context in comments.

2 Comments

Or this helps me a lot, but my application will be running at all times. Can I update the DATABASE_URL while the application is running? Anyway thank you very much :)
You can, but I wouldn't. The connection string does change at arbitrary times, but it doesn't change frequently. I suggest you catch database authorization errors (which should happen infrequently), shut down gracefully, then start back up as above with fresh credentials. There are lots of ways to handle this depending on your infrastructure: with a Docker restart policy, a service script, Windows task scheduler, pm2, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.