与Docker进行集成测试期间的Postgres身份验证问题 [英] Issues with Postgres authentication during integration tests with docker

查看:83
本文介绍了与Docker进行集成测试期间的Postgres身份验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Problem is while running integration tests, i receive this error:

 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%发生.

This happens 100% of the time regardless of what is in the docker-compose.yml file.

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

I'm using knex to make the DB connection like so:

    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

host is 127.0.0.1 and lets say the username is postgres and the password is 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数据库:

and i can connect to the postgres db manually using:

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

ive还更新了密码以制作docker-compose.yml文件,但该文件无法连接:

ive also updated the password to make the docker-compose.yml file and it wont connect:

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

但是,我运行了单元测试...我收到与上面提到的相同的错误消息!

Yet, I run the unit test... i get the same error message as mentioned above!!

要运行此程序,我缺少什么?任何帮助将不胜感激!

What am i missing to get this run? Any help would be incredibly appreciated!

谢谢!

推荐答案

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

Try to connect your database with psql postgres://postgres:postgres@localhost:5432/database from your host machine. If that does not work you have problem with your docker psql server. The way you are currently testing your connection might use unix sockets to connect the server, so it doesn't verify that your server is accepting connections through TCP socket.

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

If TCP connection with psql works fine, try to print out your used knex configurations to make sure there are no undefined values passed to any of connection attributes. First you should try to connect with hardcoded parameters, before trying to do anything more complicated.

这篇关于与Docker进行集成测试期间的Postgres身份验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆