2

On Heroku Postgres there is written:

The value of your app’s DATABASE_URL config var might change at any time. You should not rely on this value either inside or outside your Heroku app.

I'm developing a Node.js server that uses node-postgres to connect and manage the connection pool with the database.

But what happens when Heroku changes the DATABASE_URL? How should this problem be managed?

1 Answer 1

3

You handle this by always connecting to Postgres using whatever value DATABASE_URL has. For example, you can use this value as a connection string when you create your pool:

const connectionString = process.env.DATABASE_URL

const pool = new Pool({
  connectionString: connectionString,
})

Heroku's dynos restart when their environment variables or addons are changed, which should cause your code to pick up the new database connection string when it starts back up.

Sign up to request clarification or add additional context in comments.

4 Comments

I already use the environment variable. But some things are not clear to me: pool = new Pool ({connectionString: connectionString}) it is executed only the first time, after which, when a new connection is needed, it is searched among those already active and available, otherwise a new one is established. So the questions are immediate: 1) How does connectionString capture the change? 2) What happens to the connections already active in the Pool?
@alessiovolpe, when the variable changes your dyno should restart and pick up the new setting. I'll add that to my answer.
Now I understand, otherwise I couldn't explain how the thing could be handled at run time. Thank you very much!
If you have preboot enabled, heroku will not change the env and will not restart your dyno. Just confirmed this with Heroku support.

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.