How to change the PostgreSQL user’s password

Steps to change the PostgreSQL user password

1 Login with the existing user

C:\Program Files\PostgreSQL\15\bin>psql -U postgres
Password for user postgres:
psql (15.2)
WARNING: Console code page (437) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.

postgres=#

2 Change the password with two ways: alter command or \password command:

ALTER USER postgres PASSWORD '<new-password>';
Example:
postgres=# alter user postgres password 'hello';
ALTER ROLE

OR
\password
Example:
postgres-# \password postgres
Enter new password for user "postgres":
Enter it again:
postgres-#

Run in one command:

 psql -c "ALTER USER postgres PASSWORD '<new-password>';"

Example:
C:\Program Files\PostgreSQL\15\bin>psql -U postgres -c "alter user postgres password 'password'"
Password for user postgres:
ALTER ROLE

C:\Program Files\PostgreSQL\15\bin>

Leave a Reply