0

I am trying to set up a nodeJs project in my system. This project has different database environment for local and development, for which the username, password and other credentials are added in knex.js

I have installed Postgres, in my local (and used the same username and password as that of my system)

I need to create a new table, for that I ran this command to create the migration file

knex migrate:make 'test' --env local --knexfile db/knex.js

A file is created. Then in this file I added the code to create the table

exports.up = function (knex, Promise) {
    return knex.schema
        .createTable('oxygen', table => {
            table.increments('id').primary();
            table.string('name').notNull();
        });
};

exports.down = function (knex, Promise) {
    return knex.schema.dropTable('oxygen');
};

Now in order to create table in the database, I ran this migration command

knex migrate:latest --env local --knexfile db/knex.js

But I am getting this error

Working directory changed to ~/backend/db
Using environment: local
password authentication failed for user "postgres"
error: password authentication failed for user "postgres"
    at Connection.parseE (/home/bhavya/backend/node_modules/pg/lib/connection.js:614:13)
    at Connection.parseMessage (/home/bhavya/backend/node_modules/pg/lib/connection.js:413:19)
    at Socket.<anonymous> (/home/bhavya/backend/node_modules/pg/lib/connection.js:129:22)
    at Socket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:295:12)
    at readableAddChunk (_stream_readable.js:271:9)
    at Socket.Readable.push (_stream_readable.js:212:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:186:23)

The knex.js file includes following part of code for connecting with the local db

local: {
        client: 'pg',
        useNullAsDefault: true,
        migrations: {
            directory: './../db/migrations'
        },
        seeds: {
            directory: './../db/seeds'
        },
        connection: {
            host: '127.0.0.1',
            user: 'postgres',
            password: 'postgres',
            database: 'backend'
        }
    },
7
  • error: password authentication failed for user "postgres" is pretty plainly telling you that your credentials for connecting to the database are wrong... Commented Nov 29, 2021 at 7:59
  • @AKX yes, but I have rechecked the password credentials and they are correct. And then how knex migrate:make 'test' --env local --knexfile db/knex.js this command ran successfully ? Commented Nov 29, 2021 at 8:04
  • Maybe it didn't connect to the database to just create a new migration file? Commented Nov 29, 2021 at 8:05
  • @AKX I am using ubuntu and have set up a new user password. Is there any command which I can run to connect to Postgres locally in authenticated mode, so that I can verify if the user password I am using in knex.js is correct ? Commented Nov 29, 2021 at 8:09
  • It looks like Knex is trying to connect using the username postgres. What exactly do you have in your knexfile? Commented Nov 29, 2021 at 8:11

1 Answer 1

1

@escoder, Use the below command to login locally into PostgreSQL by using username and password via Ubuntu terminal

psql postgresql://<username>:<password>@localhost:5432

Then, If you face this below error in your terminal

FATAL: password authentication failed for user "postgres"

you have to reset your password of postgres user.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.