如何在heroku更改DATABASE_URL时处理postgres连接

问题描述 投票:0回答:1

Heroku Postgres 上写道:

您应用的DATABASE_URL配置var的值可能随时发生变化。您不应该在Heroku应用程序内部或外部依赖此值。

我正在开发一个Node.js服务器,它使用node-postgres连接和管理与数据库的连接池。

但是当Heroku改变DATABASE_URL时会发生什么?该如何管理这个问题?

node.js heroku heroku-postgres node-postgres
1个回答
1
投票

你通过使用DATABASE_URL所具有的任何值连接到Postgres来处理这个问题。例如,您可以在创建池时将此值用作connection string

const connectionString = process.env.DATABASE_URL

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

Heroku的dynos restart when their environment variables or addons are changed,它应该导致你的代码在它重新启动时获取新的数据库连接字符串。

© www.soinside.com 2019 - 2024. All rights reserved.