1

I am following a tutorial on how to create a to do app with PERN stack. (working on ubuntu 18.4, postgres version 12.3) I did install the PostrgreSQL, server is running on 5000 and I am able to enter to the database from the command line with "psql -U postgres" but when I try to connect database with the server I am getting this error: password authentication failed for user "postgres". I was not asked to give any password to the user "postgres" while instalation so I left the password in the db file empty.

My db file looks like this:

const Pool = require("pg").Pool;

const pool = new Pool({
  user: "postgres",
  password: "",
  host: "localhost",
  port: 5432,
  database: "perntodo",
});

module.exports = pool;

what can I co? I did set the postrgres authentication in pg_hba.conf file from peer to trust as I found in another issue on stockoverflow, but the error keeps appearing.

PaulinaL
  • 11
  • 1
  • 2

3 Answers3

1

The immediate fix would be to eliminate:

host: "localhost"

from your connection settings. This would force the connection to be made on local which would be equivalent to what you are doing with psql -U postgres.

The longer term fix would be to use psql -U postgres to connect and then ALTER ROLE postgres WITH PASSWORD '<some_pwd>'. This would give this ROLE a password. You can do this with other roles that already exist by using ALTER or when you CREATE a new role in the create statement.

The connection authentication methods are controlled by the pg_hba.conf file. A full explanation of what it does is available here:

https://www.postgresql.org/docs/current/auth-pg-hba-conf.html

If the above does not answer all your questions then come back with specific concerns.

Adrian Klaver
  • 15,886
  • 2
  • 17
  • 28
  • thanks! unfortunatelly removing the host: "localhost" did not helped, I have created a new user and put this user to the db.js file and now it works. – PaulinaL Aug 24 '20 at 10:09
  • 1
    Did you connect using ```psql -U postgres```? Did you do the ```ALTER ROLE``` for the postgres user? – Adrian Klaver Aug 24 '20 at 13:40
0

Don't use user postgres to connect your application to PostgreSQL.

  1. Create separate user for application.
  2. Grant to application user strict permissions only for schemes and tables what it need access.
  3. Add pg_hba.conf record for application user
  4. Enjoy
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
  • Unfortunately the above is not an answer to the issue @PaulinaL is having and will only lead to more of the same errors. The problem is authentication type and mode of connection. – Adrian Klaver Aug 20 '20 at 14:27
  • You could do both. However, providing an answer that only leads to the same problem is not an answer. – Adrian Klaver Aug 20 '20 at 14:39
  • I did create a new user, gave him rights almost like the postgres user and changed in the db.js file the user from postgres to my new user and to the new user's password and it works now. – PaulinaL Aug 24 '20 at 10:08
  • @PaulinaL Good Luck! – Slava Rozhnev Aug 24 '20 at 11:05
0

If I go as per your error message, then you need to reset postgres password. And I believe the default isnt working or you might have forgotten after resetting it.

Please follow below tutorial for resetting password:-

https://docs.bitnami.com/aws/infrastructure/postgresql/administration/change-reset-password/

or refer the answer for below:-

What is the default password for Postgres

Nik
  • 204
  • 1
  • 7
  • 18