突然,PostgreSQL 服务器的 Heroku 凭据为用户错误提供了致命密码

Suddenly, Heroku credentials to a PostgreSQL server gives FATAL password for user error

如果不更改设置中的任何内容,我无法连接到 Heroku 上托管的 PostgreSQL 数据库。我无法在我的应用程序中访问它,并出现错误

OperationalError: (psycopg2.OperationalError) FATAL:  password authentication failed for user "<heroku user>" FATAL:  no pg_hba.conf entry for host "<address>", user "<user>", database "<database>", SSL off

它说 SSL 已关闭,但正如我在 PgAdmin 中确认的那样,它已启用。当尝试通过 PgAdmin 4 访问数据库时,我遇到了同样的问题,说存在用户 '' 错误的致命密码验证。

我已经检查了 Heroku 上数据库的凭据,但没有任何变化。难道我做错了什么?我必须在 pg_hba.conf 中更改某些内容吗?

编辑:我可以在 Heroku 的通知中看到数据库在数据库停止为我工作时更新了。但是,我不确定是否触发了更新。

这里是通知中心:

一般来说,it isn't a good idea to hard-code credentials when connecting to Heroku Postgres:

Do not copy and paste database credentials to a separate environment or into your application’s code. The database URL is managed by Heroku and will change under some circumstances such as:

  • User-initiated database credential rotations using heroku pg:credentials:rotate.
  • Catastrophic hardware failures that require Heroku Postgres staff to recover your database on new hardware.
  • Security issues or threats that require Heroku Postgres staff to rotate database credentials.
  • Automated failover events on HA-enabled plans.

It is best practice to 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.

根据你截图中的消息,我怀疑你受到了第二颗子弹的影响。不管是什么原因,其中一条消息明确表示

Once it has completed, your database URL will have changed

我遇到了同样的问题。感谢@Chris 我用这种方式解决了它。 此文件位于 config/database.js (Strapi 3.1.3)

var parseDbUrl = require("parse-database-url");

if (process.env.NODE_ENV === 'production') {
  module.exports = ({ env }) => {
    var dbConfig = parseDbUrl(env('DATABASE_URL', ''));
    return {
      defaultConnection: 'default',
      connections: {
        default: {
          connector: 'bookshelf',
          settings: {
            client: dbConfig.driver,
            host: dbConfig.host,
            port: dbConfig.port,
            database: dbConfig.database,
            username: dbConfig.user,
            password: dbConfig.password,
          },
          options: {
            ssl: false,
          },
        },
      },
    }
  };
} else {
  // to use the default local provider you can return an empty configuration
  module.exports = ({ env }) => ({
    defaultConnection: 'default',
    connections: {
      default: {
        connector: 'bookshelf',
        settings: {
          client: 'sqlite',
          filename: env('DATABASE_FILENAME', '.tmp/data.db'),
        },
        options: {
          useNullAsDefault: true,
        },
      },
    },
  });
}