ALTER_ROLE(7)

okay bestie, pull up a chair because we're reading ALTER ROLE(7) from the PostgreSQL 18.6 Documentation and honestly? this page is lowkey a whole personality. let's get into it ๐Ÿ’–


๐ŸŒธ NAME ๐ŸŒธ

ALTER_ROLE โ€” change a database role. yes bestie, it's exactly what it sounds like. glow-up time ๐Ÿ’…


๐ŸŽ€ 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

like, look at all those variants. the way there are four whole flavours of this command?? we love a versatile queen โœจ


๐Ÿ’– DESCRIPTION ๐Ÿ’–

so ALTER ROLE changes the attributes of a PostgreSQL role. that's the whole vibe.

the first variant in the synopsis can change a whole bunch of the role attributes you'd set in CREATE ROLE. (yes, all the possible attributes are covered โ€” with one little exception: there are no options here for adding or removing memberships. use GRANT and REVOKE for that, bestie.) any attribute you don't mention just keeps its previous setting, no drama.

now for the permission tea, because this part matters:

  • Database superusers can change any of these settings for any role โ€” except changing the SUPERUSER property for the bootstrap superuser. that one's off limits, periodt.
  • Non-superuser roles that have the CREATEROLE privilege can change most of these properties, but only for non-superuser and non-replication roles for which they've been granted ADMIN OPTION.
  • Non-superusers cannot change the SUPERUSER property at all, and they can change CREATEDB, REPLICATION, and BYPASSRLS only if they already possess the corresponding property themselves. iykyk.
  • Ordinary roles can only change their own password. that's it. that's the whole list. ๐Ÿฅฒ

the second variant changes the role's name. database superusers can rename any role. roles with CREATEROLE privilege can rename non-superuser roles for which they've been granted ADMIN OPTION. the current session user cannot be renamed โ€” connect as a different user if you need to do that, no workaround here. also heads up: because MD5-encrypted passwords use the role name as cryptographic salt, renaming a role clears its password if that password is MD5-encrypted. so, yeah. note that. ๐Ÿ’€

the remaining variants change a role's session default for a configuration variable โ€” either for all databases, or, when the IN DATABASE clause is specified, only for sessions in the named database. if ALL is specified instead of a role name, it changes the setting for all roles. using ALL with IN DATABASE is effectively the same as using the command ALTER DATABASE ... SET ....

whenever the role later 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. and this only happens at login time โ€” executing SET ROLE or SET SESSION AUTHORIZATION does not cause new configuration values to be set. so don't expect it there. settings set for all databases get overridden by database-specific settings attached to a role. and settings for specific databases or specific roles override settings for all roles.

who can do what with these defaults:

  • Superusers can change anyone's session defaults.
  • Roles having CREATEROLE privilege can change defaults for non-superuser roles for which they've been granted ADMIN OPTION.
  • Ordinary roles can only set defaults for themselves.
  • Certain configuration variables can't be set this way at all, or can only be set if a superuser issues the command.
  • Only superusers can change a setting for all roles in all databases. main character energy only ๐Ÿ”ฅ

๐Ÿฆ‹ PARAMETERS ๐Ÿฆ‹

  • name โ€” the name of the role whose attributes are about to get altered.
  • CURRENT_ROLE, CURRENT_USER โ€” alter the current user instead of an explicitly identified role.
  • 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 originally set by CREATE ROLE. for more info, go check the CREATE ROLE reference page, bestie.
  • new_name โ€” the new name of the role. fresh start, new you โœจ
  • 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. 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. 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 and SET SESSION AUTHORIZATION do not process role-specific variable settings. (yes, saying it again, it's that important.)

see SET(7) and Chapter 19 for more info about allowed parameter names and values ๐Ÿ“š


๐ŸŒˆ NOTES ๐ŸŒˆ

use CREATE ROLE to add new roles, and DROP ROLE to remove a role. that's the lifecycle, babe.

ALTER ROLE cannot change a role's memberships. use GRANT and REVOKE for that. stop trying to make ALTER ROLE do memberships, it's not going to happen ๐Ÿ’…

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. psql(1) contains a command \password that can be used to change a role's password without exposing the cleartext password. please use that one, i am begging ๐Ÿ™ˆ

it's also possible to tie a session default to a specific database rather than to a role; see ALTER DATABASE (ALTER_DATABASE(7)). if there's a conflict, database-role-specific settings override role-specific ones, which in turn override database-specific ones. hierarchy!! ๐Ÿ‘‘


โญ 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. it's ours, we made it, no cap ๐Ÿซถ


๐Ÿ”ฅ SEE ALSO ๐Ÿ”ฅ


and that's the whole page, bestie!! you just read ALTER ROLE(7) from the PostgreSQL 18.6 Documentation, dated 2026 โ€” go forth and alter some roles responsibly, and remember: never ship an unencrypted password, that's not the vibe ๐Ÿ’–โœจ๐ŸŽ€๐Ÿฆ‹