Knex:Error Pool2 - error: password authentication failed for user

旧时模样 提交于 2020-05-27 07:23:30

问题


I am having trouble with my migration to Ubuntu Linux. I can use Postgres in the terminal. So I don't have a problem with the Postgres password.

When I type: knex migrate:latest --env development
I get:
Using environment: development Knex:warning - Pool2 - Error: Pool was destroyed Knex:Error Pool2 - error: password authentication failed for user "user"
I've read from other answers in related questions to go into the pg_hba.conf and set the method to trust. I've done this, but no change. my knex.js file looks like this:
module.exports = { devolopment: {client: 'pg', connection: 'postres://localhost/bikesdb' }, production: { client: 'pg', connection: process.env.DATABASE_URL } };
I'm not sure what I'm doing wrong.


回答1:


You are most likely running on node 6.x.x so you'd need to upgrade your pg package version npm install --save pg@4.5.5

Reference: https://github.com/tgriesser/knex/issues/1371




回答2:


If Andrei Stalbe's solution doesn't help... here are some more thoughts.

How are you connecting to postgres from commandline?

If you are writing just psql bikesdbit actually uses unix socket for connecting and it has different security settings in postgres by default than through TCP. You can try to change your connection string to: postgres:///bikesdb which makes also knex to use unix socket.

You can change/check security policies in pg_hba.conf.

Also you have typo in postres://localhost/bikesdb.

EDIT:

I'm pretty sure you are still having some general postgresql configuration problem. If you like to create user to access your database you can do this:

CREATE ROLE bikesuser WITH LOGIN PASSWORD 'letmepass';
GRANT ALL PRIVILEGES ON DATABASE "bikesdb" TO bikesuser;

And change connection string to:

'postgres://bikesuser:letmepass@localhost/bikesdb'

If you like to allow access from localhost to postgresdb this should do it in pg_hba.conf

host all all 127.0.0.1/32 trust

Hope that something of this is useful, even that it sounds that you have already tried this all.




回答3:


I had the same problem and it was all about the fact that knex was trying to use the username taken from the environment variables (LOGNAME) of my command line interface. If Postgres is set to use a different user (by default postgres if you haven't changed it) there will be an authentication error. You can solve this problem by specifying the authentication credentials inside the URL that you use to connect to the Postgres server.

module.exports = { 
 devolopment: {client: 'pg',
 connection: 'postres://user:password@localhost/bikesdb'
 },
 production: {
 client: 'pg',
 connection: process.env.DATABASE_URL
 }

};

I highlight the key point in the URL: postres://user:password@localhost/bikesdb

You can check the name of the user to access the Postgresql server from the UI interface provided by Postgresql and the password is the one that you have defined during the installation phase (if you haven't changed it)




回答4:


I ran into this problem recently. My issue was I could run migrations on local macOS but not on a remote Linux Ubuntu machine. I found out that the reason was I didn’t have a password set for the user, though everything works fine on local for some reason it required a password on remote (Linux). So, setting password for the user and adding the password to the connection solved my problem.

Example:

On postgres

\password username

Then enter and confirm the password.

In your config file (e.g knexfile.js)

module.export  = { client: ‘postgresql`, user: username, password: secret }



回答5:


I had set my psql to go to my user instead of postgres. However, I reset the postgres password. I just reset the postgres password, and the in the development connection I included the password. (I tried including it before the postgres password reset and it still didn't work). It's odd because the user I specified in the connection was me and not postgres.



来源:https://stackoverflow.com/questions/38111578/knexerror-pool2-error-password-authentication-failed-for-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!