This article will explain how to change a user’s password in PostgreSQL. PostgreSQL is one of the most popular database systems due to its flexibility and support for granular permissions and user roles that allow you to assign permissions to a group of users.
Connect to the PostgreSQL Server as an Administrative User
To change a password for a user in PostgreSQL, you must first connect with the psql client:
psql -h localhost -p 5432 -U postgres
Above, the psql command is used to connect to the PostgreSQL server on localhost, on port 5432, as the default postgres user.
The default postgres user has administrative rights, has permission to alter the passwords of other users. If you are logging in as a different user, they will need these permissions granted to them.
Updating a Users Password in PostgreSQL
Once connected to the PostgreSQL server, you can update a users password using the ALTER USER statement:
ALTER USER user_name WITH PASSWORD 'new_password';
Note that you’ll need to replace user_name and new_password with the username of the user you want to update the password for, and their new password, respectively.
Checking that the Password has Updated Successfully
To check that the password change was successful, you can attempt to log in as the user:
psql -h localhost -p 5432 -U user_namename
After entering this command you will be prompted for a password for the login. If the password change was successful, the login attempt will be successful.
If not, re-check the command you used to change the password, making sure that you have typed the password correctly and that no error occured.