6

After installing and opening the psql shell, it throws this error BEFORE even asking the password I had set during install:

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

I enter the default user ("postgres") and it immediately throws the error before prompting me to enter the password as everyone else's shell.

Tried to look at the pg_hba.conf file and changing the METHOD from scram-sha-256 (the one that was originally set) to md5 and password, but no good.

I know I can just set it to trust but that doesn't seem right. Why is this happening? How can it be an installing mistake if it doesn't even wait for me to the enter the password?

3
  • 1
    What is "the psql shell"? People who say that are usually on Windows and are talking about some kind of bat file which is wrapper, not the real psql binary. Commented Nov 17, 2022 at 22:51
  • 1
    If a password is supplied by .pgpass file or by PGPASSWORD environment, then psql (without -W option) will not prompt for a password, but will just use the one supplied. Commented Nov 17, 2022 at 22:53
  • Yes, I'm trying to use PostgreSQL on Windows (my desktop at work). It comes with an app called SQL Shell (psql) and the pgAdmin 4 app. When I open the SQL Shell and try to connect, it throws the error. Also, I can't find the .pgpass file on %appdata% or anywhere else, even though i'm checking for hidden items on the Explorer. Commented Nov 18, 2022 at 12:18

5 Answers 5

4

During installation you are asked for the password of the superuser (typically postgres). If you don't provide a username, psql assumes your current operating system user. So unless you have already created another user, you need to log in using the superuser:

psql -U postgres

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

Comments

3

First of all, check did you set up a password for the 'postgres' user. Then try to change in the file /var/lib/pgsql/data/pg_hba.conf all values 'ident' to 'scram-sha-256' And then restart server: sudo systemctl restart postgresql.service

Comments

1

Open terminal.

Log in to your postgres sql: sudo -u postgres psql postgres

You have to set up a new password for postgre so now After you see the postgres prompt type in: \password postgres

Enter your new password, confirm it and reopen postgres again. This should work.

1 Comment

Your solution was helpful, this worked for me sudo -u postgres psql postgres
0

I had the same issue with psql shell on Windows. I solved it by running the pg_env.bat file in C:\Program Files\PostgreSQL\16rc1\ before running psql.This file sets the environment variables for PostgreSQL, such as PGUSER and PGDATA. After running pg_env.bat, psql accepted my password and connected to the server successfully.

1 Comment

This file doesn't exist in my case
0

I put "admin" as a password from a fresh install for user postgres and it works

Comments

Your Answer

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