FATAL: password authentication failed for user "postgres"
You may encounter this error when attempting to access a database for the first time on PostgreSQL. Instead of configuring security and users settings, you can edit the hba_file to allow any user on the local system to authenticate as any PosgreSQL user. This should not be done in production, however this can make your life easier while working in a test environment.
Step 1: Within the bash terminal, log in as the default postgres user
Step 2: Open your posgres database (e.g., “todoapp”)
[postgres@devserver ~]$ psql todoapp
My results:
psql (10.6)
Type “help” for help.todoapp=#
Step 3: Locate the hba_file
todoapp=# show hba_file;
My results:
hba_file
---------------------------------
/var/lib/pgsql/data/pg_hba.conf
(1 row)
Step 4: Within the bash terminal, log in with your local username that has root access to postgres files
Step 5: Use vim and the file path result from step 2 to edit the hba_file
sudo vim /var/lib/pgsql/data/pg_hba.conf
My results:
Enter the “i” key to use vim’s insert mode. Use your arrow keys to navigate the text file. Under the “Method” column you may see the following entries: password, md5, indent, reject. Go ahead and update them all to trust. This allows any user on the local system to authenticate as any PosgreSQL user. Only set all to trust in a test environment, not in production.
Esc key will exit insert mode and return you to command mode. The following commands can save or cancel your edits:
#save and quit
:wq#quit without saving
:q!
Step 6: Within the bash terminal restart PostgreSQL
[localUserName@devserver ~]$ sudo systemctl restart postgresql
Expected results
Postgres user can access the database and you no longer receive the error.