Sometimes you may need to change user password in PostgreSQL, for database management and website administration. In this article, we will look at how to change user password in PostgreSQL. You can use these steps to change postgres password in Ubuntu, Windows and other Linux systems. You can also use it to change default postgres password.
How To Change User Password in PostgreSQL
Here are the steps to change user password in PostgreSQL.
1. Log into PostgreSQL
Open terminal and run the following command to log into PostgreSQL. Replace username and dbname with your username and database name respectively.
sudo -u username psql dbname
Also read : How to change user to superuser in PostgreSQL
2. Change User Password
We will use the ALTER USER command to change user password. Here is its syntax.
ALTER ROLE username WITH PASSWORD 'password';
In the above command, you need to specify the username whose password you want to change, and also the new password for that user.
Run the following ALTER USER command to change user password. Replace testuser and newpassword with
ALTER USER testuser WITH PASSWORD 'newpassword';
Similarly, if you want to change password for postgres user, modify the above command as shown.
ALTER USER postgres WITH PASSWORD 'newpassword';
Sometimes you may need to expire a password after a specific date. In such cases, use VALID UNTIL clause with ALTER USER statement. Here’s the above SQL query modified to set password expiration date as December 31, 2020.
ALTER USER testuser WITH PASSWORD 'newpassword'VALID UNTIL 'December 31, 2020'
As you can see, it is very easy to change user password in PostgreSQL in Ubuntu, CentOS and other systems.