PostgreSQL | Role (User) ROLE Creation | Rename a Role (ALTER ROLE)

You can rename a created role by using the ALTER ROLE command. This page explains how to rename a role.

Rename a Role

To rename a role, use the ALTER ROLE command. The syntax is as follows.

ALTER ROLE name RENAME TO new_name

This changes the name of the target role (role_name) to the new name (new_name). You cannot change the name of the current session user, which is the role used when connecting. In other words, you cannot rename yourself.

A superuser can rename all roles. A role with the CREATEROLE privilege can rename roles that are not superusers.

If the client authentication method uses md5, renaming a role clears its connection password, so it must be set again.

Now try it in practice. Connect to PostgreSQL as the superuser postgres and rename the created kuma role.

First, check the list of currently created roles.

postgres=> \du
                                 롤 목록
  롤 이름  |                      속성                      | 소속 그룹:
-----------+------------------------------------------------+------------
 kuma      |                                                | {}
 postgres  | 슈퍼유저, 롤 만들기, DB 만들기, 복제, RLS 통과 | {}
 superkuma | DB 만들기                                     +| {}
           | 3개 연결                                       |


postgres=>

Currently, three roles have been created.

Now rename the kuma role to mykuma. Run the following.

devkuma=# alter role kuma rename to mykuma;
알림:  롤 이름이 변경 되어 MD5 암호를 지웠습니다
ALTER ROLE
devkuma=#

The role name has been changed to mykuma. Because this role used md5 as its authentication method, its password was cleared, as indicated by the message “알림: 롤 이름이 변경 되어 MD5 암호를 지웠습니다”.

Use the ALTER ROLE command to set the connection password again.

devkuma=# alter role mykuma with password 'mybear';
ALTER ROLE
devkuma=#

The connection password for the mykuma role has been set.

Now check the list of created roles again.

devkuma=# \du
                                 롤 목록
  롤 이름  |                      속성                      | 소속 그룹:
-----------+------------------------------------------------+------------
 mykuma    |                                                | {}
 postgres  | 슈퍼유저, 롤 만들기, DB 만들기, 복제, RLS 통과 | {}
 superkuma | DB 만들기                                     +| {}
           | 3개 연결                                       |


devkuma=#

You can confirm that the kuma role has been renamed to mykuma. The attributes set on the role remain unchanged.

This page explained how to rename a created role by using the ALTER ROLE command.