0

I'm coding a simple "connect to db" program in Node.js using the pg module.

Until yesterday, everything was working fine, but now for some reason this error appears:

error: password authentication failed for user "ezert"

I don't understand what is going on, because I know the password used in the script is correct.

Full code:

const { Client } = require('pg'); // quando definido entre parêntesis, declara-se que se trata de um contrutor

let connectionURL = 'postgresql://ezert:<MY PASSWORD>@localhost:5432/mydb';

const client = new Client({
    connectionURL: connectionURL
});

client.connect();

client.query("SELECT * FROM my_table", function (err, result) {

    if (err) {

        console.log('[ERROR]:\n' + err);
        return;

    }

    console.log('[RESULT]:');
    console.log(result.rows);
    return;

});
mhutter
  • 2,369
  • 17
  • 26

1 Answers1

0

This is a problem with PostgreSQL, not with Node.js.

To solve it:

  1. Edit the following file: /etc/postgresql/YOUR_PG_VERSION/main/pg_hba.conf
  2. Change all authentication methods to trust.
  3. Change the Unix password for postgres user.
  4. Restart your server.
  5. Login with psql -h localhost -U postgres, using the password you chose at (3).
  6. Now you can reset the pg_hba.conf file to the default values, if you want.
Daniele Molinari
  • 453
  • 5
  • 28