docker 集成测试期间的 Postgres 身份验证问题

Issues with Postgres authentication during integration tests with docker

问题是在 运行 集成测试时,我收到此错误:

 error: password authentication failed for user "postgres"
      at Connection.parseE (node_modules/pg/lib/connection.js:567:11)
      at Connection.parseMessage (node_modules/pg/lib/connection.js:391:17)

无论 docker-compose.yml 文件中的内容如何,​​这种情况 100% 都会发生。

我正在使用 knex 建立数据库连接,如下所示:

    function getConnectionForDatabase(database) {
     return {
       host: config.get("database.connection.host"),
       user: config.get("database.connection.user"),
       password: config.get("database.connection.password"),
       database: database
     };
   }

   function getDatabaseConnection(database) {
     return knex({
       client: config.get("database.client"),
       connection: getConnectionForDatabase(database),
       debug: config.get("database.debugMode"),
       pool: {
         min: config.get("database.pool.min"),
         max: config.get("database.pool.max")
       }
     });
   }

getDatabaseConnection("postgres");

主机是 127.0.0.1 假设用户名是 postgres 密码是 postgres

docker-compose.yml 包含:

   postgres:
    image: postgres:9.5.4
    command: postgres -D /var/lib/postgresql/data
    volumes:
      - db-data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    environment:
      POSTGRES_PASSWORD: 'postgres'
      POSTGRES_USER: 'postgres'

我可以使用以下方法手动连接到 postgres 数据库:

docker exec -it <hash> bash
root@<hash>:/# psql -U postgres

我还更新了密码以创建 docker-compose.yml 文件,但无法连接:

psql -d postgres -c "ALTER USER postgres WITH PASSWORD '<new password>';"

然而,我 运行 单元测试...我收到与上述相同的错误消息!!

我缺少什么才能得到这个运行?任何帮助将不胜感激!

谢谢!

尝试从您的主机连接您的数据库与 psql postgres://postgres:postgres@localhost:5432/database。如果这不起作用,则说明您的 docker psql 服务器有问题。您当前测试连接的方式可能使用 unix 套接字连接服务器,因此它不会验证您的服务器是否正在通过 TCP 套接字接受连接。

如果与 psql 的 TCP 连接工作正常,请尝试打印出您使用的 knex 配置,以确保没有 undefined 值传递给任何连接属性。在尝试做任何更复杂的事情之前,您应该首先尝试使用硬编码参数进行连接。