0

I'm trying to use PostgreSQL 15 on my desktop at work (Windows 10). When installing, everything goes fine and I set a password to the default postgres user.

Using psql -U postgres on PowerShell, it throws this error message BEFORE prompting me to enter a password:

psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL:  password authentication failed for user "postgres"

In the pg_hba.conf file, all METHOD values are set to scram-sha-256. Changing to md5 and password doesn't help. It only works when I change it to trust, but I wanted to have a password. How can I fix this?

  • 1
    Do you have a [password file](https://www.postgresql.org/docs/current/libpq-pgpass.html)? –  Nov 18 '22 at 13:03
  • @a_horse_with_no_name As far as I'm aware, I don't. I even tried to search within `%appdata%` for the file, but there's no `postgresql` folder there, only a `pgAdmin` folder. –  Nov 18 '22 at 13:34
  • 1
    What do get if you run `echo %PGPASSWORD%`? –  Nov 18 '22 at 13:39
  • @a_horse_with_no_name I get only `%PGPASSWORD%`. It was supposed to be set on PATH? –  Nov 18 '22 at 13:45
  • But anyways, thanks for the help. Forcing to prompt for password using -W fixed it. But I can't wrap my mind around why it doesn't ask for a password if I've set everything by default. –  Nov 18 '22 at 13:46
  • Apparently, the `runpsql.bat` script was missing the -W option to prompt for a password. Is it a bug or it has always been like this? –  Nov 18 '22 at 13:59
  • Sorry, I have no idea what your `runpsql.bat` is doing. And the -W option should never be necessary. –  Nov 18 '22 at 14:02
  • runpsql.bat doesn't prompt for a password because that is the job of the "real" psql. On my system, it prompts fine once I rename the pgpass.conf file so it can't be found anymore under the expected name. – jjanes Nov 18 '22 at 16:26

1 Answers1

1

add -W flag to force prompt for password https://www.postgresql.org/docs/devel/app-psql.html

esmin
  • 491
  • 7
  • 14
  • 1
    That worked! Thank you so much. Why is it that by default it isn't asking for the password? Weird. –  Nov 18 '22 at 13:41
  • psql is usually used in scripts and passwords managed via .pgpass. One does not want server scripts to wait for someone to enter password :D – esmin Nov 18 '22 at 14:37