{
    "mode": "man",
    "parameter": "SET_ROLE",
    "section": "7",
    "url": "https://www.chedong.com/phpMan.php/man/SET_ROLE/7/json",
    "generated": "2026-10-08T20:29:18Z",
    "synopsis": "SET [ SESSION | LOCAL ] ROLE rolename\nSET [ SESSION | LOCAL ] ROLE NONE\nRESET ROLE",
    "sections": {
        "NAME": {
            "content": "SETROLE - set the current user identifier of the current session\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "SET [ SESSION | LOCAL ] ROLE rolename\nSET [ SESSION | LOCAL ] ROLE NONE\nRESET ROLE\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This command sets the current user identifier of the current SQL session to be rolename. The\nrole name can be written as either an identifier or a string literal. After SET ROLE,\npermissions checking for SQL commands is carried out as though the named role were the one\nthat had logged in originally.\n\nThe current session user must have the SET option for the specified rolename, either\ndirectly or indirectly via a chain of memberships with the SET option. (If the session user\nis a superuser, any role can be selected.)\n\nThe SESSION and LOCAL modifiers act the same as for the regular SET command.\n\nSET ROLE NONE sets the current user identifier to the current session user identifier, as\nreturned by sessionuser.  RESET ROLE sets the current user identifier to the connection-time\nsetting specified by the command-line options, ALTER ROLE, or ALTER DATABASE, if any such\nsettings exist. Otherwise, RESET ROLE sets the current user identifier to the current session\nuser identifier. These forms can be executed by any user.\n",
            "subsections": []
        },
        "NOTES": {
            "content": "Using this command, it is possible to either add privileges or restrict one's privileges. If\nthe session user role has been granted memberships WITH INHERIT TRUE, it automatically has\nall the privileges of every such role. In this case, SET ROLE effectively drops all the\nprivileges except for those which the target role directly possesses or inherits. On the\nother hand, if the session user role has been granted memberships WITH INHERIT FALSE, the\nprivileges of the granted roles can't be accessed by default. However, if the role was\ngranted WITH SET TRUE, the session user can use SET ROLE to drop the privileges assigned\ndirectly to the session user and instead acquire the privileges available to the named role.\nIf the role was granted WITH INHERIT FALSE, SET FALSE then the privileges of that role cannot\nbe exercised either with or without SET ROLE.\n\nNote that when a superuser chooses to SET ROLE to a non-superuser role, they lose their\nsuperuser privileges.\n\nSET ROLE has effects comparable to SET SESSION AUTHORIZATION, but the privilege checks\ninvolved are quite different. Also, SET SESSION AUTHORIZATION determines which roles are\nallowable for later SET ROLE commands, whereas changing roles with SET ROLE does not change\nthe set of roles allowed to a later SET ROLE.\n\nSET ROLE does not process session variables as specified by the role's ALTER ROLE settings;\nthis only happens during login.\n\nSET ROLE cannot be used within a SECURITY DEFINER function.\n",
            "subsections": []
        },
        "EXAMPLES": {
            "content": "SELECT SESSIONUSER, CURRENTUSER;\n\nsessionuser | currentuser\n--------------+--------------\npeter        | peter\n\nSET ROLE 'paul';\n\nSELECT SESSIONUSER, CURRENTUSER;\n\nsessionuser | currentuser\n--------------+--------------\npeter        | paul\n",
            "subsections": []
        },
        "COMPATIBILITY": {
            "content": "PostgreSQL allows identifier syntax (\"rolename\"), while the SQL standard requires the role\nname to be written as a string literal. SQL does not allow this command during a transaction;\nPostgreSQL does not make this restriction because there is no reason to. The SESSION and\nLOCAL modifiers are a PostgreSQL extension, as is the RESET syntax.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "SET SESSION AUTHORIZATION (SETSESSIONAUTHORIZATION(7))\n\nPostgreSQL 16.14                                2026                                     SET ROLE(7)",
            "subsections": []
        }
    },
    "summary": "SETROLE - set the current user identifier of the current session",
    "flags": [],
    "examples": [
        "SELECT SESSIONUSER, CURRENTUSER;",
        "sessionuser | currentuser",
        "--------------+--------------",
        "peter        | peter",
        "SET ROLE 'paul';",
        "SELECT SESSIONUSER, CURRENTUSER;",
        "sessionuser | currentuser",
        "--------------+--------------",
        "peter        | paul"
    ],
    "see_also": [
        {
            "name": "SETSESSIONAUTHORIZATION",
            "section": "7",
            "url": "https://www.chedong.com/phpMan.php/man/SETSESSIONAUTHORIZATION/7/json"
        },
        {
            "name": "ROLE",
            "section": "7",
            "url": "https://www.chedong.com/phpMan.php/man/ROLE/7/json"
        }
    ]
}