Advertisement
Advertisement


PostgreSQL: How to change PostgreSQL user password?


Question

How do I change the password for PostgreSQL user?

2020/05/31
1
1045
5/31/2020 3:47:43 PM

Accepted Answer

For password less login:

sudo -u user_name psql db_name

To reset the password if you have forgotten:

ALTER USER user_name WITH PASSWORD 'new_password';
2017/12/24
1476
12/24/2017 4:10:12 PM


You can and should have the users's password encrypted:

ALTER USER username WITH ENCRYPTED PASSWORD 'password';
2017/07/05

I believe the best way to change the password is simply to use:

\password

in the Postgres console.

Source:

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.

from https://www.postgresql.org/docs/9.0/static/sql-alterrole.html.

2017/08/30

To change password using Linux command line, use:

sudo -u <user_name> psql -c "ALTER USER <user_name> PASSWORD '<new_password>';"
2018/10/29

To Change Password

 sudo -u postgres psql

then

\password postgres

now enter New Password and Confirm

then \q to exit

2019/06/29

Go to your Postgresql Config and Edit pg_hba.conf

sudo vim /etc/postgresql/9.3/main/pg_hba.conf

Then Change this Line :

Database administrative login by Unix domain socket
local      all              postgres                                md5

to :

Database administrative login by Unix domain socket
local   all             postgres                                peer

then Restart the PostgreSQL service via SUDO command then

psql -U postgres

You will be now entered and will See the Postgresql terminal

then enter

\password

and enter the NEW Password for Postgres default user, After Successfully changing the Password again go to the pg_hba.conf and revert the change to "md5"

now you will be logged in as

psql -U postgres

with your new Password.

Let me know if you all find any issue in it.

2014/10/09