Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

Issues with Postgres authentication during integration tests with docker

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)

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

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");

host is 127.0.0.1 and lets say the username is postgres and the password is postgres

the docker-compose.yml contains:

   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'

and i can connect to the postgres db manually using:

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

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!

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...