PostgreSQL - Change a user password
The PostgreSQL ALTER USER statement is used to change the password of a user in the PostgreSQL database.
Syntax
The syntax of changing password using ALTER USER statement in PostgreSQL is given below:
ALTER USER user_name WITH [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'new_password' | VALID UNTIL 'expiration';
Parameters
user_name |
Required. Specify the user for which the password need to be changed. |
new_password |
Optional. Assigns the given literal password to user_name. |
expiration |
Optional. Specify the date/time value when the password will expire. If this clause is omitted the password will be valid for all time. |
Example: Changing password
In the example below, the password of account named john is changed to 'password1'.
ALTER USER john WITH PASSWORD 'password1';
Example: Changing password expiry date
The VALID UNTIL clause can be used to set an absolute time after which the user's password will not be valid. If this clause is omitted the password will be valid for all time. In the example below, the user's password expiry date is set to 'Dec 31, 2025'.
ALTER USER john VALID UNTIL 'Dec 31, 2025';
Example: Changing password and expiry date
In the example below, password is changed with new expiry date for user named john.
ALTER USER john WITH PASSWORD 'password1' VALID UNTIL 'Dec 31, 2025';