ALTER_ROLE(7pg13)
π ALTER_ROLE β change a database role π
okay bestie, grab your iced coffee, this is the ALTER ROLE(7) page from the PostgreSQL 13.23 Documentation, and we're gonna make it make sense. it's giving "glow-up for your roles" energy β¨
β¨ NAME β¨
ALTER_ROLE β change a database role. that's it, that's the vibe. your role gets a whole makeover π
πΈ SYNOPSIS πΈ
ALTER ROLE role_specification [ WITH ] option [ ... ]
where option can be:
SUPERUSER | NOSUPERUSER
| CREATEDB | NOCREATEDB
| CREATEROLE | NOCREATEROLE
| INHERIT | NOINHERIT
| LOGIN | NOLOGIN
| REPLICATION | NOREPLICATION
| BYPASSRLS | NOBYPASSRLS
| CONNECTION LIMIT connlimit
| [ ENCRYPTED ] PASSWORD 'password' | PASSWORD NULL
| VALID UNTIL 'timestamp'
ALTER ROLE name RENAME TO new_name
ALTER ROLE { role_specification | ALL } [ IN DATABASE database_name ] SET configuration_parameter { TO | = } { value | DEFAULT }
ALTER ROLE { role_specification | ALL } [ IN DATABASE database_name ] SET configuration_parameter FROM CURRENT
ALTER ROLE { role_specification | ALL } [ IN DATABASE database_name ] RESET configuration_parameter
ALTER ROLE { role_specification | ALL } [ IN DATABASE database_name ] RESET ALL
where role_specification can be:
role_name
| CURRENT_USER
| SESSION_USER
like, four whole flavors of this command, and every single one of them is iconic in its own way π«Ά
π¦ DESCRIPTION π¦
so ALTER ROLE changes the attributes of a PostgreSQL role. glow-up szn π
the first variant β attribute makeover π
the first variant in the synopsis can change many of the role attributes that you'd specify in CREATE ROLE (CREATE_ROLE(7)). and yes, that covers all the possible attributes β with one tiny exception: there's no option here for adding or removing memberships. for that you gotta use GRANT(7) and REVOKE(7). not me forgetting the memberships, i would never π
anything you don't mention in the command keeps its previous settings. so no surprise glow-downs, we love that for us.
now, who's allowed to do what:
- Database superusers can change any of these settings for any role. main character energy fr π
- Roles having
CREATEROLEprivilege can change any of these settings exceptSUPERUSER,REPLICATION, andBYPASSRLSβ and only for non-superuser and non-replication roles. read the fine print bestie, it matters. - Ordinary roles can only change their own password. that's the whole menu. tiny, but valid π₯Ί
the second variant β the name change β¨
the second variant changes the name of the role. like a rebrand.
- Database superusers can rename any role.
- Roles having
CREATEROLEprivilege can rename non-superuser roles. - the current session user cannot be renamed. (connect as a different user if you need to do that β no cap.)
β οΈ huge one: because MD5-encrypted passwords use the role name as cryptographic salt, renaming a role clears its password if the password is MD5-encrypted. literally wipes it. so like, know that before you go renaming people π
the remaining variants β config defaults, but for a role π
the rest of the variants change a role's session default for a configuration variable. either for all databases, or β when you throw in the IN DATABASE clause β only for sessions in the named database.
if you say ALL instead of a role name, it changes the setting for all roles. and using ALL with IN DATABASE is effectively the same as using the command ALTER DATABASE ... SET .... β same energy, different spelling π«
whenever the role subsequently starts a new session, that specified value becomes the session default, overriding whatever setting is sitting in postgresql.conf or has been received from the postgres command line.
and this is the key bit: it only happens at login time. executing SET ROLE (SET_ROLE(7)) or SET SESSION AUTHORIZATION (SET_SESSION_AUTHORIZATION(7)) does not cause new configuration values to be set. so don't come crying to me when your SET ROLE didn't pick up the new defaults, i warned you π€
also, the override hierarchy, which is lowkey a whole mood:
- settings set for all databases get overridden by database-specific settings attached to a role
- settings for specific databases or specific roles override settings for all roles
who can touch whose defaults π
- Superusers can change anyone's session defaults. periodt.
- Roles having
CREATEROLEprivilege can change defaults for non-superuser roles. - Ordinary roles can only set defaults for themselves. self-care π
- certain configuration variables cannot be set this way, or can only be set if a superuser issues the command.
- only superusers can change a setting for all roles in all databases. that's the final boss tier.
π§ PARAMETERS π§
nameβ the name of the role whose attributes are to be altered. the girlies getting the makeover.CURRENT_USERβ alter the current user instead of an explicitly identified role. you! it's you!SESSION_USERβ alter the current session user instead of an explicitly identified role. also kinda you, but the session flavor.
the attribute clauses β¨
SUPERUSERNOSUPERUSERCREATEDBNOCREATEDBCREATEROLENOCREATEROLEINHERITNOINHERITLOGINNOLOGINREPLICATIONNOREPLICATIONBYPASSRLSNOBYPASSRLSCONNECTION LIMIT connlimit[ ENCRYPTED ] PASSWORD 'password'PASSWORD NULLVALID UNTIL 'timestamp'
all of these clauses alter attributes originally set by CREATE ROLE (CREATE_ROLE(7)). for more info, go peek at the CREATE ROLE reference page β it's the origin story π¦
the rest of the parameters πΈ
new_nameβ the new name of the role. the rebrand. the fresh handle.database_nameβ the name of the database the configuration variable should be set in.configuration_parameter,valueβ set this role's session default for the specified configuration parameter to the givenvalue.
if value is DEFAULT or, equivalently, RESET is used, the role-specific variable setting is removed, so the role will inherit the system-wide default setting in new sessions. use RESET ALL to clear all role-specific settings. SET FROM CURRENT saves the session's current value of the parameter as the role-specific value. and if IN DATABASE is specified, the configuration parameter is set or removed for the given role and database only.
β οΈ role-specific variable settings take effect only at login;SET ROLE(SET_ROLE(7)) andSET SESSION AUTHORIZATION(SET_SESSION_AUTHORIZATION(7)) do not process role-specific variable settings. i'm saying it twice because it's the number one thing people get wrong, iykyk π
see SET(7) and Chapter 19 for more information about allowed parameter names and values π
π₯ NOTES π₯
- use
CREATE ROLE(CREATE_ROLE(7)) to add new roles, andDROP ROLE(DROP_ROLE(7)) to remove a role. birth and departure, both handled elsewhere π« ALTER ROLEcannot change a role's memberships. useGRANT(7)andREVOKE(7)to do that. i know, i know.- caution must be exercised when specifying an unencrypted password with this command. the password will be transmitted to the server in cleartext, and it might also be logged in the client's command history or the server log. that's giving "naked password in the group chat" energy and we do NOT want that π.
psql(1)contains a command\passwordthat can be used to change a role's password without exposing the cleartext password. use it. please. π«Ά - it's also possible to tie a session default to a specific database rather than to a role; see
ALTER DATABASE(ALTER_DATABASE(7)). and if there's a conflict: database-role-specific settings override role-specific ones, which in turn override database-specific ones. the hierarchy is real β
π EXAMPLES π
change a role's password:
ALTER ROLE davide WITH PASSWORD 'hu8jmn3';
remove a role's password:
ALTER ROLE davide WITH PASSWORD NULL;
change a password expiration date, specifying that the password should expire at midday on 4th May 2015 using the time zone which is one hour ahead of UTC:
ALTER ROLE chris VALID UNTIL 'May 4 12:00:00 2015 +1';
make a password valid forever:
ALTER ROLE fred VALID UNTIL 'infinity';
give a role the ability to manage other roles and create new databases:
ALTER ROLE miriam CREATEROLE CREATEDB;
give a role a non-default setting of the maintenance_work_mem parameter:
ALTER ROLE worker_bee SET maintenance_work_mem = 100000;
give a role a non-default, database-specific setting of the client_min_messages parameter:
ALTER ROLE fred IN DATABASE devel SET client_min_messages = DEBUG;
slay, honestly. every single one of these ate π β¨
π COMPATIBILITY π
the ALTER ROLE statement is a PostgreSQL extension. it's ours, we invented it, it's exclusive π
π SEE ALSO π
CREATE ROLE(CREATE_ROLE(7))DROP ROLE(DROP_ROLE(7))ALTER DATABASE(ALTER_DATABASE(7))SET(7)
and that's the whole page bestie!! go forth and alter your roles responsibly π this was brought to you by PostgreSQL 13.23, dated 2025 β the ALTER ROLE(7) manual page, now with 100% more sparkle π¦πΈβ ngl you're gonna crush it π«Ά
π¦ where this page comes from
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms