PostgreSQL – Password authentication failed for user “postgres”

When logging into PostgreSQL, an error message may appear like this:

Password authentication failed for user "postgres".

When PostgreSQL installs on Linux, by default no password is assigned to the postgres superuser.
The postgres user password must be explicitly set using a command such as like the following.

[email protected]:~$ sudo passwd postgres
Enter new UNIX password: ****
Retype new UNIX password: ****
passwd: password updated successfully

After login, you can ALTER postgres user:

ALTER USER postgres PASSWORD 'NewPassword';

Not: If you forgot your postgres password, you can login without password by changing “md5” to “trust” in pg_hba.conf file before restart the PostgreSQL server.
After login, you should set a password to postgres user with ALTER command, and change again pg_hba.conf entry from “trust” to “md5”. Than restart the PostgreSQL server.