This short post assumes your username is the default one used by PostgreSQL – postgres. If you’re using a different one then just use yours when needed.
So let’s begin.
Connect to the Postgres promptĀ
sudo -u postgres psql
Change / set the password for user postgres
\password postgres
And that’s it! Your postgres user is now protected with the password you chose. However, if you now try to log in with that user chances are you won’t be asked for your password and that bugged me for an hour. In order to fix that you need to make a simple change inside the pg_hba.conf file.
Open pg_hba.conf file (note that your path might be different)
sudo nano /etc/postgresql/12/main/pg_hba.conf
Change from peer to md5 the line below
The final result shoud look like this
Restart the PostgreSQL service
sudo service postgresql restart
After that you should be asked for the password you set for the postgres account.
Hope that helps you in your journey, dear friend.