Change PostgreSQL Authentication Method From ‘peer’ to ‘md5’

By Nicholas Brown.

If you want to log into your PostgreSQL database by entering a password, you can do so by changing the authentication method from ‘peer‘ to ‘md5‘. There is also the ‘password’ authentication method, but that sends the password in clear text, so I won’t recommend that. Both the md5 and password authentication methods prompt you for a password on login.

If you are just setting up a new PostgreSQL installation which is not yet exposed to any networks, you can temporarily change the authentication method for your respective username in the pg_hba.conf file to ‘trust’, then restart PostgreSQL with the following command if you’re on Ubuntu, and then change the authentication method back to md5 right after:

Find that PostgreSQL configuration file by typing: ‘sudo nano /etc/postgresql/11/main/pg_hba.conf‘. look for the user ‘postgres’ in white and look to the right for the ‘md5’. That’s where you set the authentication method for that PostgreSQL user.

PostgreSQL configuration file
PostgreSQL configuration file pg_hba.conf.
sudo service postgresql restart

Now you can login by typing:

psql -U postgres

It should let you in without a password prompt this time (not after you’re finished setting it up). Type ‘\password’ and then enter your desired password. Ensure that it is strong and contains a combination of numbers, letters, and symbols.

Now you must change the PostgreSQL authentication method back to md5, and it will prompt you for your password when you want to log into PostgreSQL from now on. Don’t leave it as ‘trust’, because anybody will be able to access your databases.