'Unhandled rejection SequelizeConnectionError: password authentication failed for user "ankitj"

This happens even when the DB user specified in .env file is different. For the record "ankitj" is also the username of my system. I don't understand why this is happening.

Here's the error:

Unhandled rejection SequelizeConnectionError: password authentication failed for user "ankitj"
    at connection.connect.err (/home/ankitj/Desktop/skillbee/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:128:24)
    at Connection.connectingErrorHandler (/home/ankitj/Desktop/skillbee/node_modules/pg/lib/client.js:140:14)
    at Connection.emit (events.js:160:13)
    at Socket.<anonymous> (/home/ankitj/Desktop/skillbee/node_modules/pg/lib/connection.js:124:12)
    at Socket.emit (events.js:160:13)
    at addChunk (_stream_readable.js:269:12)
    at readableAddChunk (_stream_readable.js:256:11)
    at Socket.Readable.push (_stream_readable.js:213:10)
    at TCP.onread (net.js:599:20)


Solution 1:[1]

I'm assuming that you're getting this error using Sequelize with Node.js. I ran into the same error when I had the following:

const sequelize = new Sequelize(
  process.env.DATABASE,
  process.env.DATABASE_USER,
  process.env.DATABASE_PASSWORD,
  {
    dialect: 'postgres',
  }
)

I was able to solve the issue by replacing it with a connection ur:

const sequelize = new Sequelize("postgres://postgres:postgres@localhost/gql", {
  dialect: 'postgres'
  // anything else you want to pass
})

where gql is the name of DATABASE in my .env file.

The user "ankitj" executes the command to run the Node script, so the script is trying to connect as that user. I first tried to solve this on the Postgres end by adding a user and granting permissions, but was unable to get that to work--I'd be interested in seeing that solution--but specifying a connection url worked for me.

Solution 2:[2]

I had the same issue when I was using the default password for the PostgreSQL. Try to change it from the command line as follows.

  1. psql
  2. \password

Then Enter a new password and update your .env files and that should Work.

Solution 3:[3]

Instead of using the process.env variables I put the actual names but wrapped in quotes (" ") and it's worked.

Solution 4:[4]

I have faced the same issue when I connect with NodeJS and Postgres SQL. I found a solution. We need to check db.config.js file(database config file) and index.js(where u called the const sequelize = new Sequelize(.......) properties name is matching or not.

// db.config.js
module.exports = {
    user: '******',
    host: 'localhost',
    database: '***********',
    password: '***',
    port: 5432,
    dialect: "postgres", // we need to implement 
    pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000
    }
};

const Sequelize = require("sequelize");
const dbConfig = require("../config/db.config.js");

const sequelize = new Sequelize(dbConfig.database, dbConfig.user, dbConfig.password, {
    host: dbConfig.host,
    dialect: dbConfig.dialect,
    operatorsAliases: false,

    pool: {
        max: dbConfig.pool.max,
        min: dbConfig.pool.min,
        acquire: dbConfig.pool.acquire,
        idle: dbConfig.pool.idle
    },
})

Mentioned the above params line as (dbConfig.database, dbConfig.user, dbConfig.password) we need to check with db.config.js file

Note: please add dialect properties on the db.config.js file.

Now Problem is solved...Thanks

Console verbiage log

Solution 5:[5]

I also had the same error but I solved it by using template strings

const sequelize = new Sequelize(`${process.env.DB_NAME}`, `${process.env.DB_USER}`, `${process.env.DB_PASSWORD}`, {
  host: process.env.DB_HOST,
  dialect: 'postgres'
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 crash springfield
Solution 2 Yonatan Dawit
Solution 3 4b0
Solution 4 slideshowp2
Solution 5 Adrian Tompkins