ALTER_ROLE(7pg15)
πΈ ALTER ROLE π
(that's ALTER_ROLE, page 7pg15, from the PostgreSQL 15.19 docs bestie)
okay so this is the whole glow-up manual for ALTER ROLE β it's the page that tells you how to change a database role once it already exists. sit down, grab a strawberry drink π, we're gonna get through all of it together.
β¨ NAME β¨
ALTER_ROLE β change a database role. that's it, that's the whole vibe. π¦
π 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_ROLE
| CURRENT_USER
| SESSION_USER
π DESCRIPTION π
so ALTER ROLE changes the attributes of a PostgreSQL role. periodt.
the first variant in the synopsis (the big one with the option list) can change a whole bunch of the role attributes that you'd normally set in CREATE ROLE. honestly all the possible attributes are covered here, except β heads up β there are no options for adding or removing memberships, so use GRANT and REVOKE for that, okurrr? any attribute you don't mention in the command just keeps its previous settings, no drama. π
now who's allowed to do what, because this part is lowkey important:
- database superusers can change any of these settings for any role π
- roles that have the
CREATEROLEprivilege can change any of these settings exceptSUPERUSER,REPLICATION, andBYPASSRLSβ but only for non-superuser and non-replication roles, iykyk - ordinary roles can only change their own password π
the second variant changes the name of the role. the rules:
- database superusers can rename any role
- roles with
CREATEROLEprivilege can rename non-superuser roles - the current session user cannot be renamed β so connect as a different user if you need to do that, fr fr
- and this is the iconic little gotcha: because MD5-encrypted passwords use the role name as cryptographic salt, renaming a role clears its password if that password is MD5-encrypted. the way that just happens π
the remaining variants change a role's session default for a configuration variable β either for all databases, or, when the IN DATABASE clause is there, only for sessions in the named database. if ALL is specified instead of a role name, that changes the setting for all roles. and using ALL with IN DATABASE is effectively the same as using the command ALTER DATABASE ... SET .... π«Ά
so whenever the role subsequently starts a new session, that specified value becomes the session default, overriding whatever setting is present in postgresql.conf or has been received from the postgres command line. but bestie β this only happens at login time. executing SET ROLE or SET SESSION AUTHORIZATION does not cause new configuration values to be set. not me accidentally thinking it does π
precedence, because there's a whole hierarchy:
- settings set for all databases are overridden by database-specific settings attached to a role
- settings for specific databases or specific roles override settings for all roles
and the permission tea again β:
- superusers can change anyone's session defaults
- roles with
CREATEROLEprivilege can change defaults for non-superuser roles - ordinary roles can only set defaults for themselves
- 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 π§
π PARAMETERS π
nameβ the name of the role whose attributes are gonna be altered. the main character of the statement β¨CURRENT_ROLE,CURRENT_USERβ alter the current user instead of an explicitly identified role. so you don't have to say your own name twice πSESSION_USERβ alter the current session user instead of an explicitly identified role.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'β these clauses alter attributes that were originally set byCREATE ROLE. for more info, go read theCREATE ROLEreference page (it ate, honestly) π₯new_nameβ the new name of the role. rebrand era π¦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 given value. ifvalueisDEFAULTor, equivalently,RESETis used, the role-specific variable setting is removed, so the role will inherit the system-wide default setting in new sessions. useRESET ALLto clear all role-specific settings.SET FROM CURRENTsaves the session's current value of the parameter as the role-specific value. ifIN DATABASEis 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 and SET SESSION AUTHORIZATION do not process role-specific variable settings. literally just at login, ok?
see SET(7) and Chapter 20 for more information about allowed parameter names and values π
β NOTES β
- use
CREATE ROLEto add new roles, andDROP ROLEto remove a role. those are the spawn and despawn buttons π«Ά ALTER ROLEcannot change a role's memberships. useGRANTandREVOKEto do that. not negotiable π
β οΈ 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. like, the whole group chat could see it πpsql(1)contains a command\passwordthat can be used to change a role's password without exposing the cleartext password. use that one, please, i'm begging π
it is 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, the order is:
- database-role-specific settings override role-specific ones
- role-specific ones, in turn, override database-specific ones
that's the whole food chain bestie π
π 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;
π§ COMPATIBILITY π§
the ALTER ROLE statement is a PostgreSQL extension. not standard SQL, just our little thing π
β¨
π SEE ALSO π
CREATE ROLE(CREATE_ROLE(7))DROP ROLE(DROP_ROLE(7))ALTER DATABASE(ALTER_DATABASE(7))SET(7)
and that's the page!! π this was brought to you by PostgreSQL 15.19, dated 2026 β that's the whole sign-off bestie, go alter some roles responsibly, and remember: \password is your friend, cleartext is not π¦π slay.
π¦ where this page comes from
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-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