postgres Change user password ubuntu / Mac / Linux / Unix

Step 1: Start Postgres using psql command

$ psqlpsql (11.1, server 11.0)Type "help" for help.postgres=# 

Step 2: change password using password command

postgres=# passwordEnter new password:Enter it again:postgres=#

Step 3: Then to quit psql:

q

Step 4: If that does not work, reconfigure authentication file pg_hba.conf

/etc/postgresql/9.1/main/pg_hba.conf (path may differ)

and change:

    local   all             all                                     peer

to:

    local   all             all                                     md5

Then restart the server:

$ sudo service postgresql restart

Important Point:

I believe the best way to change the password is simply to use: password  in the Postgres console.

Caution must be exercised when specifying an unencrypted password with this command. The password will be transmitted to the server in cleartext, and it might also be logged in the client’s command history or the server log. psql contains a command password that can be used to change a role’s password without exposing the cleartext password. 

This is the correct answer from a security point of view.

This can also be used to change passwords for other users: password username

Method 2:

Step 1: Start Postgres using psql command

$ psqlpsql (11.1, server 11.0)Type "help" for help.postgres=# 

Step 2: To reset the password if you have forgotten:

ALTER USER user_name WITH PASSWORD 'new_password';

For passwordless login no need to change the password. simply hit below command

sudo -u user_name psql db_name