PostgreSQL is a widely-used open-source RDBMS that offers several advanced security features, like secure authentication, encryption, etc. Implementing strong passwords is one of the most important aspects of securing a PostgreSQL database. However, sometimes users may need to remove a password for various purposes, such as passwordless access, if access to the database is no longer needed, etc.
This write-up illustrates a detailed guide on removing passwords for users in Postgres.
How to Remove/Delete Passwords for a User in PostgreSQL?
By setting a strong password, PostgreSQL users can ensure that only authorized users can access the database and its sensitive information. However, occasionally, we may need to remove a password for a user in Postgres. For this purpose, the ALTER USER command can be executed with the PASSWORD clause. In addition to this, the password value must be specified as “NULL”. Here is the syntax to remove a password for a user in Postgres:
To remove/delete a password for a user in Postgres, you can execute the following command:
ALTER USER user_name PASSWORD NULL;
In the above command, specify the actual name of a user(whose password needs to be deleted) in place of the user_name. Moreover, only the superusers can execute the above-stated command.
Example 1: Remove User Password in Postgres
Execute the following “\du” command to see the list of available users:
Suppose we want to remove the password of a user named “joseph”. For this purpose, we will execute the below-provided command:
ALTER USER joseph PASSWORD NULL;
The above command will set the password for the “joseph” user as “NULL”, which is equivalent to removing the password:
The output snippet signifies that the user has been altered, indicating that the password has been successfully removed.
To verify the password removal, launch a new “psql” terminal and log in as “joseph”:
It can be seen in the above snippet that the user, "joseph," has successfully logged in without specifying a password.
To remove/delete a password for a user in Postgres, execute the “ALTER USER user_name PASSWORD NULL;” command. In the stated command, replace user_name with the username of your choice. To execute this command you must be a superuser, else you will encounter a “permission denied” error. This post has illustrated a detailed method for removing passwords for a user in Postgres.