{"Entry":{"collection":"sql","key":"set-session-authorization","name":"SET SESSION AUTHORIZATION","aliases":["set-session-authorization"],"metadata":{"aliases":["set-session-authorization"],"changed_in":["7.3","9.0"],"changes":[{"from":"7.1","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"added","synopsis":null,"to":"7.2"},{"from":"7.2","purpose_changed":false,"renamed":null,"sections":{"added":["notes"],"changed":["description"],"removed":[]},"status":"changed","synopsis":{"added":["SET [ SESSION | LOCAL ] SESSION AUTHORIZATION username","SET [ SESSION | LOCAL ] SESSION AUTHORIZATION DEFAULT","RESET SESSION AUTHORIZATION"],"removed":["SET SESSION AUTHORIZATION 'username'"]},"to":"7.3"},{"from":"7.3","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","examples","compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"7.4"},{"from":"8.0","purpose_changed":false,"renamed":null,"sections":{"added":["see_also"],"changed":["description","compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"8.1"},{"from":"8.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description"],"removed":[]},"status":"changed","synopsis":null,"to":"8.3"},{"from":"8.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["SET [ SESSION | LOCAL ] SESSION AUTHORIZATION user_name"],"removed":["SET [ SESSION | LOCAL ] SESSION AUTHORIZATION username"]},"to":"9.0"},{"from":"16","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description"],"removed":[]},"status":"changed","synopsis":null,"to":"17"}],"content_hash":"b30ab8ff1fc61c2b5dd48bd2741c439449e23b1ac5a2bd7d9d6630793aa22202","editorial":{},"first_version":"7.2","group":"role","imported_at":"2026-09-30T17:43:38.279909+08:00","last_version":"20","name":"SET SESSION AUTHORIZATION","object":"SESSION AUTHORIZATION","position":5016,"present_in":["7.2","7.3","7.4","8.0","8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.3","9.4","9.5","9.6","10","11","12","13","14","15","16","17","18","19","20"],"purpose":"set the session user identifier and the current user identifier of the current session","purpose_zh":"","related":["set-role"],"slug":"set-session-authorization","source_rev":"a709ab85","synopsis":"SET [ SESSION | LOCAL ] SESSION AUTHORIZATION user_name\nSET [ SESSION | LOCAL ] SESSION AUTHORIZATION DEFAULT\nRESET SESSION AUTHORIZATION","verb":"SET"}},"Definition":{"Collection":"sql","Key":"set-session-authorization","SourceDatabase":"center","Version":"18","SourceTable":"sqlcmd","SourceKey":"set-session-authorization","SourceRevision":"a709ab85","Facts":{"anchor":"SQL-SET-SESSION-AUTHORIZATION","file":"sql-set-session-authorization.html","lang":"en","name":"SET SESSION AUTHORIZATION","purpose":"set the session user identifier and the current user identifier of the current session","purpose_zh":"","related":["set-role"],"sections":[{"html":"\u003cp\u003eThis command sets the session user identifier and the current user identifier of the current SQL session to be \u003cem class=\"replaceable\"\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e. The user name can be written as either an identifier or a string literal. Using this command, it is possible, for example, to temporarily become an unprivileged user and later switch back to being a superuser.\u003c/p\u003e\u003cp\u003eThe session user identifier is initially set to be the (possibly authenticated) user name provided by the client. The current user identifier is normally equal to the session user identifier, but might change temporarily in the context of \u003ccode class=\"literal\"\u003eSECURITY DEFINER\u003c/code\u003e functions and similar mechanisms; it can also be changed by \u003ca href=\"/docs/18/sql-set-role.html\" title=\"SET ROLE\"\u003e\u003ccode class=\"command\"\u003eSET ROLE\u003c/code\u003e\u003c/a\u003e. The current user identifier is relevant for permission checking.\u003c/p\u003e\u003cp\u003eThe session user identifier can be changed only if the initial session user (the \u003cem class=\"firstterm\"\u003eauthenticated user\u003c/em\u003e) has the superuser privilege. Otherwise, the command is accepted only if it specifies the authenticated user name.\u003c/p\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eSESSION\u003c/code\u003e and \u003ccode class=\"literal\"\u003eLOCAL\u003c/code\u003e modifiers act the same as for the regular \u003ca href=\"/docs/18/sql-set.html\" title=\"SET\"\u003e\u003ccode class=\"command\"\u003eSET\u003c/code\u003e\u003c/a\u003e command.\u003c/p\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e and \u003ccode class=\"literal\"\u003eRESET\u003c/code\u003e forms reset the session and current user identifiers to be the originally authenticated user name. These forms can be executed by any user.\u003c/p\u003e","key":"description","title":"Description"},{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eSET SESSION AUTHORIZATION\u003c/code\u003e cannot be used within a \u003ccode class=\"literal\"\u003eSECURITY DEFINER\u003c/code\u003e function.\u003c/p\u003e","key":"notes","title":"Notes"},{"html":"\u003cpre class=\"programlisting\"\u003eSELECT SESSION_USER, CURRENT_USER;\n\n session_user | current_user\n--------------+--------------\n peter        | peter\n\nSET SESSION AUTHORIZATION 'paul';\n\nSELECT SESSION_USER, CURRENT_USER;\n\n session_user | current_user\n--------------+--------------\n paul         | paul\n\u003c/pre\u003e","key":"examples","title":"Examples"},{"html":"\u003cp\u003eThe SQL standard allows some other expressions to appear in place of the literal \u003cem class=\"replaceable\"\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e, but these options are not important in practice. \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e allows identifier syntax (\u003ccode class=\"literal\"\u003e\"\u003cem class=\"replaceable\"\u003e\u003ccode\u003eusername\u003c/code\u003e\u003c/em\u003e\"\u003c/code\u003e), which SQL does not. SQL does not allow this command during a transaction; \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e does not make this restriction because there is no reason to. The \u003ccode class=\"literal\"\u003eSESSION\u003c/code\u003e and \u003ccode class=\"literal\"\u003eLOCAL\u003c/code\u003e modifiers are a \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e extension, as is the \u003ccode class=\"literal\"\u003eRESET\u003c/code\u003e syntax.\u003c/p\u003e\u003cp\u003eThe privileges necessary to execute this command are left implementation-defined by the standard.\u003c/p\u003e","key":"compatibility","title":"Compatibility"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/set-role/?v=18\" title=\"SET ROLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eSET ROLE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"See Also"}],"sections_same_as":"","slug":"18","synopsis_html":"SET [ SESSION | LOCAL ] SESSION AUTHORIZATION \u003cem class=\"replaceable\"\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e\nSET [ SESSION | LOCAL ] SESSION AUTHORIZATION DEFAULT\nRESET SESSION AUTHORIZATION","synopsis_text":"SET [ SESSION | LOCAL ] SESSION AUTHORIZATION user_name\nSET [ SESSION | LOCAL ] SESSION AUTHORIZATION DEFAULT\nRESET SESSION AUTHORIZATION"},"ManualEvidence":{},"MeasuredEvidence":{}},"Text":{"Collection":"sql","Key":"set-session-authorization","SourceDatabase":"pgweb","Version":"18","Locale":"zh-Hans","Title":"SET SESSION AUTHORIZATION","Summary":"设置当前会话的会话用户标识符和当前用户标识符","BodyHTML":"\u003cpre\u003eSET [ SESSION | LOCAL ] SESSION AUTHORIZATION user_name\nSET [ SESSION | LOCAL ] SESSION AUTHORIZATION DEFAULT\nRESET SESSION AUTHORIZATION\u003c/pre\u003e\u003csection\u003e\u003ch2\u003e描述\u003c/h2\u003e\u003cp\u003e这个命令将当前 SQL 会话的会话用户标识符和当前用户标识符设置为 \u003cem\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e。用户名可以写成一个标识符或者一个字符串字面量。例如，可以使用这个命令临时变为一个非特权用户，之后再切换回超级用户。\u003c/p\u003e\u003cp\u003e会话用户标识符初始时被设置为客户端提供的（可能已认证的）用户名。当前用户标识符通常与会话用户标识符相同，但可能在 \u003ccode\u003eSECURITY DEFINER\u003c/code\u003e函数和类似机制的上下文中临时更改；它也可以通过\u003ca href=\"/docs/18/sql-set-role.html\" title=\"SET ROLE\" rel=\"nofollow\"\u003e\u003ccode\u003eSET ROLE\u003c/code\u003e\u003c/a\u003e更改。当前用户标识符用于权限检查。\u003c/p\u003e\u003cp\u003e只有当初始会话用户（即\u003cem\u003e已认证用户\u003c/em\u003e）具有超级用户权限时，才能更改会话用户标识符。否则，只有当该命令指定的是已认证用户名时，才会被接受。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eSESSION\u003c/code\u003e和\u003ccode\u003eLOCAL\u003c/code\u003e修饰符的作用与常规的 \u003ca href=\"/docs/18/sql-set.html\" title=\"SET\" rel=\"nofollow\"\u003e\u003ccode\u003eSET\u003c/code\u003e\u003c/a\u003e命令相同。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eDEFAULT\u003c/code\u003e和\u003ccode\u003eRESET\u003c/code\u003e形式会将会话用户标识符和当前用户标识符重置为最初已认证的用户名。这些形式可以由任何用户执行。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e注解\u003c/h2\u003e\u003cp\u003e\u003ccode\u003eSET SESSION AUTHORIZATION\u003c/code\u003e不能在一个 \u003ccode\u003eSECURITY DEFINER\u003c/code\u003e函数中使用。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e示例\u003c/h2\u003e\u003cpre\u003eSELECT SESSION_USER, CURRENT_USER;\n\n session_user | current_user\n--------------+--------------\n peter        | peter\n\nSET SESSION AUTHORIZATION \u0026#39;paul\u0026#39;;\n\nSELECT SESSION_USER, CURRENT_USER;\n\n session_user | current_user\n--------------+--------------\n paul         | paul\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003eSQL 标准允许在字面值\u003cem\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e的位置上使用某些其他表达式，但这些选项在实践中并不重要。\u003cspan\u003ePostgreSQL\u003c/span\u003e允许使用标识符语法（\u003ccode\u003e\u0026#34;\u003cem\u003e\u003ccode\u003eusername\u003c/code\u003e\u003c/em\u003e\u0026#34;\u003c/code\u003e），而 SQL 标准不允许。SQL 不允许在事务中使用这个命令；\u003cspan\u003ePostgreSQL\u003c/span\u003e并不做此限制，因为没有理由这样做。\u003ccode\u003eSESSION\u003c/code\u003e和\u003ccode\u003eLOCAL\u003c/code\u003e修饰符以及\u003ccode\u003eRESET\u003c/code\u003e语法都是\u003cspan\u003ePostgreSQL\u003c/span\u003e扩展。\u003c/p\u003e\u003cp\u003e标准把执行这个命令所需的权限留给实现定义。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e另见\u003c/h2\u003e\u003cspan\u003e\u003ca href=\"/wiki/sql/set-role/?v=18\" title=\"SET ROLE\" rel=\"nofollow\"\u003e\u003cspan\u003eSET ROLE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e\u003c/section\u003e","SourceRevision":"1b5ca64c","ContentHash":"653aaefd4176ab978b77f0e5d3bfb563a6a672e73d6d369aa35bdb28dcd198b3","Payload":{"purpose_zh":"设置当前会话的会话用户标识符和当前用户标识符","sections":[{"html":"\u003cp\u003e这个命令将当前 SQL 会话的会话用户标识符和当前用户标识符设置为 \u003cem class=\"replaceable\"\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e。用户名可以写成一个标识符或者一个字符串字面量。例如，可以使用这个命令临时变为一个非特权用户，之后再切换回超级用户。\u003c/p\u003e\u003cp\u003e会话用户标识符初始时被设置为客户端提供的（可能已认证的）用户名。当前用户标识符通常与会话用户标识符相同，但可能在 \u003ccode class=\"literal\"\u003eSECURITY DEFINER\u003c/code\u003e函数和类似机制的上下文中临时更改；它也可以通过\u003ca href=\"/docs/18/sql-set-role.html\" title=\"SET ROLE\"\u003e\u003ccode class=\"command\"\u003eSET ROLE\u003c/code\u003e\u003c/a\u003e更改。当前用户标识符用于权限检查。\u003c/p\u003e\u003cp\u003e只有当初始会话用户（即\u003cem class=\"firstterm\"\u003e已认证用户\u003c/em\u003e）具有超级用户权限时，才能更改会话用户标识符。否则，只有当该命令指定的是已认证用户名时，才会被接受。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eSESSION\u003c/code\u003e和\u003ccode class=\"literal\"\u003eLOCAL\u003c/code\u003e修饰符的作用与常规的 \u003ca href=\"/docs/18/sql-set.html\" title=\"SET\"\u003e\u003ccode class=\"command\"\u003eSET\u003c/code\u003e\u003c/a\u003e命令相同。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eDEFAULT\u003c/code\u003e和\u003ccode class=\"literal\"\u003eRESET\u003c/code\u003e形式会将会话用户标识符和当前用户标识符重置为最初已认证的用户名。这些形式可以由任何用户执行。\u003c/p\u003e","key":"description","title":"描述"},{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eSET SESSION AUTHORIZATION\u003c/code\u003e不能在一个 \u003ccode class=\"literal\"\u003eSECURITY DEFINER\u003c/code\u003e函数中使用。\u003c/p\u003e","key":"notes","title":"注解"},{"html":"\u003cpre class=\"programlisting\"\u003eSELECT SESSION_USER, CURRENT_USER;\n\n session_user | current_user\n--------------+--------------\n peter        | peter\n\nSET SESSION AUTHORIZATION 'paul';\n\nSELECT SESSION_USER, CURRENT_USER;\n\n session_user | current_user\n--------------+--------------\n paul         | paul\n\u003c/pre\u003e","key":"examples","title":"示例"},{"html":"\u003cp\u003eSQL 标准允许在字面值\u003cem class=\"replaceable\"\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e的位置上使用某些其他表达式，但这些选项在实践中并不重要。\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e允许使用标识符语法（\u003ccode class=\"literal\"\u003e\"\u003cem class=\"replaceable\"\u003e\u003ccode\u003eusername\u003c/code\u003e\u003c/em\u003e\"\u003c/code\u003e），而 SQL 标准不允许。SQL 不允许在事务中使用这个命令；\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e并不做此限制，因为没有理由这样做。\u003ccode class=\"literal\"\u003eSESSION\u003c/code\u003e和\u003ccode class=\"literal\"\u003eLOCAL\u003c/code\u003e修饰符以及\u003ccode class=\"literal\"\u003eRESET\u003c/code\u003e语法都是\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e扩展。\u003c/p\u003e\u003cp\u003e标准把执行这个命令所需的权限留给实现定义。\u003c/p\u003e","key":"compatibility","title":"兼容性"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/set-role/?v=18\" title=\"SET ROLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eSET ROLE\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"另见"}],"sections_same_as":"","synopsis_html":"SET [ SESSION | LOCAL ] SESSION AUTHORIZATION \u003cem class=\"replaceable\"\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e\nSET [ SESSION | LOCAL ] SESSION AUTHORIZATION DEFAULT\nRESET SESSION AUTHORIZATION","synopsis_text":"SET [ SESSION | LOCAL ] SESSION AUTHORIZATION user_name\nSET [ SESSION | LOCAL ] SESSION AUTHORIZATION DEFAULT\nRESET SESSION AUTHORIZATION"}},"RequestedLocale":"zh-Hans","Fallback":false,"Versions":["10","11","12","13","14","15","16","17","18","19","20","7.2","7.3","7.4","8.0","8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.3","9.4","9.5","9.6"],"Locales":["en","zh-Hans"],"Signatures":null,"Spellings":null,"SQLState":null,"Evidence":null}
