![]() |
VOOZH | about |
In PostgreSQL, the ALTER ROLE statement is a versatile tool used to manage and modify roles. It allows administrators to change a role's name, attributes, and session defaults for configuration variables.
Let us take a look at the ALTER ROLE statement, its syntax, and various functions available with examples to help you manage PostgreSQL roles effectively.
The basic syntax for the ALTER ROLE statement is:
ALTER ROLE role_name [WITH] option;
The ALTER ROLE statement includes several options to manage role attributes:
When using the ALTER ROLE statement, certain rules must be followed:
CREATE ROLE attribute.Let us take a look at some of the examples of ALTER ROLE in PostgreSQL to better understand the concept.
First, log in to PostgreSQL using the postgres role. Then, create a new role called 'Ravi' with the following statement:
CREATE ROLE ravi LOGIN PASSWORD 'geeks12345';Now, modify the role 'Ravi' to be a superuser:
ALTER ROLE ravi SUPERUSER;To view the role attributes, use the following command:
\du raviOutput:
👁 PostgreSQL ALTER ROLE ExampleTo set an expiry date for the role 'Ravi':
ALTER ROLE ravi VALID UNTIL '2024-12-31';To grant the role 'Ravi' permission to create databases:
ALTER ROLE ravi CREATEDB;PostgreSQL ALTER ROLE Statement
- You can combine multiple options in a single
ALTER ROLEstatement, which helps in consolidating role modifications into one command for efficiency.- You can use
ALTER ROLEto set default values for configuration variables that apply every time the role logs in.- The
INHERITattribute allows a role to inherit privileges of roles it is a member of. WithoutINHERIT, the role must explicitly setSET ROLEto use the privileges.- The
VALID UNTILclause with the ALTER ROLE allows you to set an expiration date for the role's password.