In PostgreSQL, the ALTER ROLE statement is used to manage and modify roles. It can be used to modify the name of a role, the attributes of a role, and alter a role’s session default for a configuration variable.
Syntax: ALTER ROLE role_name [WITH] option;
The following functions are available with ALTER ROLE statement:
- SUPERUSER | NOSUPERUSER – It is used to determine if the role is a superuser.
- VALID UNTIL ‘timestamp’ – It is used to specify the expiry date and time of a role’s password.
The following are the set of rules that must be followed while using the ALTER ROLE statement:
- Superusers can modify the attributes for any role.
- Only non-superusers and no-replication roles can be modified is a role has CREATE ROLE attribute to it.
- Ordinal roles can only change their passwords.
First, log in to the PostgreSQL using the Postgres role.
Now, create a new role called ‘Ravi’ using the CREATE ROLE statement:
create role ravi login password 'geeks12345';
Now modify the role of ravi to be a superuser using the below statement:
alter role ravi superuser;
To view the role use the following command: