{"Entry":{"collection":"sql","key":"create-schema","name":"CREATE SCHEMA","aliases":["createschema"],"metadata":{"aliases":["createschema"],"changed_in":["9.0","9.3","9.5","14"],"changes":[{"from":"7.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"added","synopsis":null,"to":"7.3"},{"from":"7.3","purpose_changed":false,"renamed":null,"sections":{"added":["parameters","notes","see_also"],"changed":["description","examples","compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"7.4"},{"from":"7.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","notes","compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"8.0"},{"from":"8.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"8.3"},{"from":"8.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":{"added":["CREATE SCHEMA schema_name [ AUTHORIZATION user_name ] [ schema_element [ ... ] ]","CREATE SCHEMA AUTHORIZATION user_name [ schema_element [ ... ] ]"],"removed":["CREATE SCHEMA schemaname [ AUTHORIZATION username ] [ schema_element [ ... ] ]","CREATE SCHEMA AUTHORIZATION username [ schema_element [ ... ] ]"]},"to":"9.0"},{"from":"9.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","examples","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["CREATE SCHEMA IF NOT EXISTS schema_name [ AUTHORIZATION user_name ]","CREATE SCHEMA IF NOT EXISTS AUTHORIZATION user_name"],"removed":[]},"to":"9.3"},{"from":"9.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"changed","synopsis":{"added":["CREATE SCHEMA schema_name [ AUTHORIZATION role_specification ] [ schema_element [ ... ] ]","CREATE SCHEMA AUTHORIZATION role_specification [ schema_element [ ... ] ]","CREATE SCHEMA IF NOT EXISTS schema_name [ AUTHORIZATION role_specification ]","CREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_specification","| CURRENT_USER","| SESSION_USER"],"removed":["CREATE SCHEMA schema_name [ AUTHORIZATION user_name ] [ schema_element [ ... ] ]","CREATE SCHEMA AUTHORIZATION user_name [ schema_element [ ... ] ]","CREATE SCHEMA IF NOT EXISTS schema_name [ AUTHORIZATION user_name ]"]},"to":"9.5"},{"from":"9.5","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"9.6"},{"from":"9.6","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description"],"removed":[]},"status":"changed","synopsis":null,"to":"10"},{"from":"13","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"changed","synopsis":{"added":["| CURRENT_ROLE","| CURRENT_USER"],"removed":[]},"to":"14"},{"from":"15","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":null,"to":"16"},{"from":"18","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"19"}],"content_hash":"7bf27d71ee6620a3cb00afa410d0c0bfc1f414e317dfaaa757dbe0e161fed0d8","editorial":{},"first_version":"7.3","group":"schema","imported_at":"2026-09-30T17:43:37.805554+08:00","last_version":"20","name":"CREATE SCHEMA","object":"SCHEMA","position":4003,"present_in":["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":"define a new schema","purpose_zh":"","related":["alter-schema","drop-schema"],"slug":"create-schema","source_rev":"a709ab85","synopsis":"CREATE SCHEMA schema_name [ AUTHORIZATION role_specification ] [ schema_element [ ... ] ]\nCREATE SCHEMA AUTHORIZATION role_specification [ schema_element [ ... ] ]\nCREATE SCHEMA IF NOT EXISTS schema_name [ AUTHORIZATION role_specification ]\nCREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_specification\nwhere role_specification can be:\nuser_name\n| CURRENT_ROLE\n| CURRENT_USER\n| SESSION_USER","verb":"CREATE"}},"Definition":{"Collection":"sql","Key":"create-schema","SourceDatabase":"center","Version":"18","SourceTable":"sqlcmd","SourceKey":"create-schema","SourceRevision":"a709ab85","Facts":{"anchor":"SQL-CREATESCHEMA","file":"sql-createschema.html","lang":"en","name":"CREATE SCHEMA","purpose":"define a new schema","purpose_zh":"","related":["alter-schema","drop-schema"],"sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE SCHEMA\u003c/code\u003e enters a new schema into the current database. The schema name must be distinct from the name of any existing schema in the current database.\u003c/p\u003e\u003cp\u003eA schema is essentially a namespace: it contains named objects (tables, data types, functions, and operators) whose names can duplicate those of other objects existing in other schemas. Named objects are accessed either by \u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003equalifying\u003c/span\u003e”\u003c/span\u003e their names with the schema name as a prefix, or by setting a search path that includes the desired schema(s). A \u003ccode class=\"literal\"\u003eCREATE\u003c/code\u003e command specifying an unqualified object name creates the object in the current schema (the one at the front of the search path, which can be determined with the function \u003ccode class=\"function\"\u003ecurrent_schema\u003c/code\u003e).\u003c/p\u003e\u003cp\u003eOptionally, \u003ccode class=\"command\"\u003eCREATE SCHEMA\u003c/code\u003e can include subcommands to create objects within the new schema. The subcommands are treated essentially the same as separate commands issued after creating the schema, except that if the \u003ccode class=\"literal\"\u003eAUTHORIZATION\u003c/code\u003e clause is used, all the created objects will be owned by that user.\u003c/p\u003e","key":"description","title":"Description"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of a schema to be created. If this is omitted, the \u003cem class=\"replaceable\"\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e is used as the schema name. The name cannot begin with \u003ccode class=\"literal\"\u003epg_\u003c/code\u003e, as such names are reserved for system schemas.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe role name of the user who will own the new schema. If omitted, defaults to the user executing the command. To create a schema owned by another role, you must be able to \u003ccode class=\"literal\"\u003eSET ROLE\u003c/code\u003e to that role.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_element\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eAn SQL statement defining an object to be created within the schema. Currently, only \u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e, \u003ccode class=\"command\"\u003eCREATE VIEW\u003c/code\u003e, \u003ccode class=\"command\"\u003eCREATE INDEX\u003c/code\u003e, \u003ccode class=\"command\"\u003eCREATE SEQUENCE\u003c/code\u003e, \u003ccode class=\"command\"\u003eCREATE TRIGGER\u003c/code\u003e and \u003ccode class=\"command\"\u003eGRANT\u003c/code\u003e are accepted as clauses within \u003ccode class=\"command\"\u003eCREATE SCHEMA\u003c/code\u003e. Other kinds of objects may be created in separate commands after the schema is created.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eIF NOT EXISTS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eDo nothing (except issuing a notice) if a schema with the same name already exists. \u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_element\u003c/code\u003e\u003c/em\u003e subcommands cannot be included when this option is used.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"Parameters"},{"html":"\u003cp\u003eTo create a schema, the invoking user must have the \u003ccode class=\"literal\"\u003eCREATE\u003c/code\u003e privilege for the current database. (Of course, superusers bypass this check.)\u003c/p\u003e","key":"notes","title":"Notes"},{"html":"\u003cp\u003eCreate a schema:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE SCHEMA myschema;\n\u003c/pre\u003e\u003cp\u003eCreate a schema for user \u003ccode class=\"literal\"\u003ejoe\u003c/code\u003e; the schema will also be named \u003ccode class=\"literal\"\u003ejoe\u003c/code\u003e:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE SCHEMA AUTHORIZATION joe;\n\u003c/pre\u003e\u003cp\u003eCreate a schema named \u003ccode class=\"literal\"\u003etest\u003c/code\u003e that will be owned by user \u003ccode class=\"literal\"\u003ejoe\u003c/code\u003e, unless there already is a schema named \u003ccode class=\"literal\"\u003etest\u003c/code\u003e. (It does not matter whether \u003ccode class=\"literal\"\u003ejoe\u003c/code\u003e owns the pre-existing schema.)\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe;\n\u003c/pre\u003e\u003cp\u003eCreate a schema and create a table and view within it:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE SCHEMA hollywood\n    CREATE TABLE films (title text, release date, awards text[])\n    CREATE VIEW winners AS\n        SELECT title, release FROM films WHERE awards IS NOT NULL;\n\u003c/pre\u003e\u003cp\u003eNotice that the individual subcommands do not end with semicolons.\u003c/p\u003e\u003cp\u003eThe following is an equivalent way of accomplishing the same result:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE SCHEMA hollywood;\nCREATE TABLE hollywood.films (title text, release date, awards text[]);\nCREATE VIEW hollywood.winners AS\n    SELECT title, release FROM hollywood.films WHERE awards IS NOT NULL;\n\u003c/pre\u003e","key":"examples","title":"Examples"},{"html":"\u003cp\u003eThe SQL standard allows a \u003ccode class=\"literal\"\u003eDEFAULT CHARACTER SET\u003c/code\u003e clause in \u003ccode class=\"command\"\u003eCREATE SCHEMA\u003c/code\u003e, as well as more subcommand types than are presently accepted by \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e.\u003c/p\u003e\u003cp\u003eThe SQL standard specifies that the subcommands in \u003ccode class=\"command\"\u003eCREATE SCHEMA\u003c/code\u003e can appear in any order. The present \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e implementation does not handle all cases of forward references in subcommands; it might sometimes be necessary to reorder the subcommands in order to avoid forward references.\u003c/p\u003e\u003cp\u003eAccording to the SQL standard, the owner of a schema always owns all objects within it. \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e allows schemas to contain objects owned by users other than the schema owner. This can happen only if the schema owner grants the \u003ccode class=\"literal\"\u003eCREATE\u003c/code\u003e privilege on their schema to someone else, or a superuser chooses to create objects in it.\u003c/p\u003e\u003cp\u003eThe \u003ccode class=\"literal\"\u003eIF NOT EXISTS\u003c/code\u003e option is a \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e extension.\u003c/p\u003e","key":"compatibility","title":"Compatibility"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/alter-schema/?v=18\" title=\"ALTER SCHEMA\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER SCHEMA\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-schema/?v=18\" title=\"DROP SCHEMA\"\u003e\u003cspan class=\"refentrytitle\"\u003eDROP SCHEMA\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"See Also"}],"sections_same_as":"","slug":"18","synopsis_html":"CREATE SCHEMA \u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_name\u003c/code\u003e\u003c/em\u003e [ AUTHORIZATION \u003cem class=\"replaceable\"\u003e\u003ccode\u003erole_specification\u003c/code\u003e\u003c/em\u003e ] [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_element\u003c/code\u003e\u003c/em\u003e [ ... ] ]\nCREATE SCHEMA AUTHORIZATION \u003cem class=\"replaceable\"\u003e\u003ccode\u003erole_specification\u003c/code\u003e\u003c/em\u003e [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_element\u003c/code\u003e\u003c/em\u003e [ ... ] ]\nCREATE SCHEMA IF NOT EXISTS \u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_name\u003c/code\u003e\u003c/em\u003e [ AUTHORIZATION \u003cem class=\"replaceable\"\u003e\u003ccode\u003erole_specification\u003c/code\u003e\u003c/em\u003e ]\nCREATE SCHEMA IF NOT EXISTS AUTHORIZATION \u003cem class=\"replaceable\"\u003e\u003ccode\u003erole_specification\u003c/code\u003e\u003c/em\u003e\n\n\u003cspan class=\"phrase\"\u003ewhere \u003cem class=\"replaceable\"\u003e\u003ccode\u003erole_specification\u003c/code\u003e\u003c/em\u003e can be:\u003c/span\u003e\n\n    \u003cem class=\"replaceable\"\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e\n  | CURRENT_ROLE\n  | CURRENT_USER\n  | SESSION_USER","synopsis_text":"CREATE SCHEMA schema_name [ AUTHORIZATION role_specification ] [ schema_element [ ... ] ]\nCREATE SCHEMA AUTHORIZATION role_specification [ schema_element [ ... ] ]\nCREATE SCHEMA IF NOT EXISTS schema_name [ AUTHORIZATION role_specification ]\nCREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_specification\nwhere role_specification can be:\nuser_name\n| CURRENT_ROLE\n| CURRENT_USER\n| SESSION_USER"},"ManualEvidence":{},"MeasuredEvidence":{}},"Text":{"Collection":"sql","Key":"create-schema","SourceDatabase":"pgweb","Version":"18","Locale":"zh-Hans","Title":"CREATE SCHEMA","Summary":"定义一个新模式","BodyHTML":"\u003cpre\u003eCREATE SCHEMA schema_name [ AUTHORIZATION role_specification ] [ schema_element [ ... ] ]\nCREATE SCHEMA AUTHORIZATION role_specification [ schema_element [ ... ] ]\nCREATE SCHEMA IF NOT EXISTS schema_name [ AUTHORIZATION role_specification ]\nCREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_specification\n其中 role_specification 可以是：\nuser_name\n| CURRENT_ROLE\n| CURRENT_USER\n| SESSION_USER\u003c/pre\u003e\u003csection\u003e\u003ch2\u003e描述\u003c/h2\u003e\u003cp\u003e\u003ccode\u003eCREATE SCHEMA\u003c/code\u003e在当前数据库中创建一个新模式。该模式名必须与当前数据库中任何现有模式的名称不同。\u003c/p\u003e\u003cp\u003e模式本质上是一个命名空间：它包含具名对象（表、数据类型、函数和操作符），这些对象的名称可以与其他模式中的对象重名。访问具名对象时，可以在其名称前加上模式名作为前缀来\u003cspan\u003e“\u003cspan\u003e限定\u003c/span\u003e”\u003c/span\u003e名称，或者设置一个包含所需模式的搜索路径。指定非限定对象名的\u003ccode\u003eCREATE\u003c/code\u003e命令会在当前模式中创建该对象（即搜索路径最前面的模式，可通过函数\u003ccode\u003ecurrent_schema\u003c/code\u003e确定）。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eCREATE SCHEMA\u003c/code\u003e还可以选择包含子命令，以便在新模式中创建对象。这些子命令基本上会被当作在创建模式之后单独发出的命令来处理。不过，如果使用了\u003ccode\u003eAUTHORIZATION\u003c/code\u003e子句，则所有创建的对象都将归该用户所有。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e参数\u003c/h2\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003eschema_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要创建的模式名称。如果省略，\u003cem\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e将被用作模式名。该名称不能以\u003ccode\u003epg_\u003c/code\u003e开头，因为这类名称保留给系统模式。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e将拥有新模式的用户的角色名。如果省略，则默认为执行该命令的用户。要创建由另一个角色拥有的模式，你必须能够对该角色执行 \u003ccode\u003eSET ROLE\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003eschema_element\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e定义要在该模式中创建的对象的 SQL 语句。当前，只有\u003ccode\u003eCREATE TABLE\u003c/code\u003e、\u003ccode\u003eCREATE VIEW\u003c/code\u003e、\u003ccode\u003eCREATE INDEX\u003c/code\u003e、\u003ccode\u003eCREATE SEQUENCE\u003c/code\u003e、\u003ccode\u003eCREATE TRIGGER\u003c/code\u003e和\u003ccode\u003eGRANT\u003c/code\u003e可作为 \u003ccode\u003eCREATE SCHEMA\u003c/code\u003e中的子句使用。其他类型的对象可以在模式创建之后通过单独的命令创建。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eIF NOT EXISTS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果同名模式已经存在，则不执行任何操作（但会发出一条提示）。使用该选项时不能包含 \u003cem\u003e\u003ccode\u003eschema_element\u003c/code\u003e\u003c/em\u003e子命令。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e注解\u003c/h2\u003e\u003cp\u003e要创建一个模式，执行该命令的用户必须拥有当前数据库的\u003ccode\u003eCREATE\u003c/code\u003e 权限（当然，超级用户可以绕过这项检查）。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e示例\u003c/h2\u003e\u003cp\u003e创建一个模式：\u003c/p\u003e\u003cpre\u003eCREATE SCHEMA myschema;\n\u003c/pre\u003e\u003cp\u003e为用户\u003ccode\u003ejoe\u003c/code\u003e创建一个模式，该模式也将被命名为 \u003ccode\u003ejoe\u003c/code\u003e：\u003c/p\u003e\u003cpre\u003eCREATE SCHEMA AUTHORIZATION joe;\n\u003c/pre\u003e\u003cp\u003e创建一个被用户\u003ccode\u003ejoe\u003c/code\u003e拥有的名为\u003ccode\u003etest\u003c/code\u003e的模式，除非已经有一个名为\u003ccode\u003etest\u003c/code\u003e的模式（不管\u003ccode\u003ejoe\u003c/code\u003e 是否拥有那个预先存在的模式）。\u003c/p\u003e\u003cpre\u003eCREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe;\n\u003c/pre\u003e\u003cp\u003e创建一个模式并且在其中创建一个表和视图：\u003c/p\u003e\u003cpre\u003eCREATE SCHEMA hollywood\n    CREATE TABLE films (title text, release date, awards text[])\n    CREATE VIEW winners AS\n        SELECT title, release FROM films WHERE awards IS NOT NULL;\n\u003c/pre\u003e\u003cp\u003e请注意，各个子命令末尾都不带分号。\u003c/p\u003e\u003cp\u003e下面是实现相同结果的一种等效写法：\u003c/p\u003e\u003cpre\u003eCREATE SCHEMA hollywood;\nCREATE TABLE hollywood.films (title text, release date, awards text[]);\nCREATE VIEW hollywood.winners AS\n    SELECT title, release FROM hollywood.films WHERE awards IS NOT NULL;\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003eSQL 标准允许在\u003ccode\u003eCREATE SCHEMA\u003c/code\u003e中使用 \u003ccode\u003eDEFAULT CHARACTER SET\u003c/code\u003e子句，并允许使用比 \u003cspan\u003ePostgreSQL\u003c/span\u003e当前所接受的更多子命令类型。\u003c/p\u003e\u003cp\u003eSQL 标准规定，\u003ccode\u003eCREATE SCHEMA\u003c/code\u003e中的子命令可以按任意顺序出现。当前的\u003cspan\u003ePostgreSQL\u003c/span\u003e实现并不能处理子命令中所有前向引用的情况；有时可能需要重新排列子命令的顺序，以避免前向引用。\u003c/p\u003e\u003cp\u003e根据 SQL 标准，模式的拥有者总是拥有其中的所有对象。\u003cspan\u003ePostgreSQL\u003c/span\u003e允许模式包含由模式拥有者之外的用户所拥有的对象。只有当模式拥有者把其模式上的\u003ccode\u003eCREATE\u003c/code\u003e权限授予其他人，或者超级用户选择在该模式中创建对象时，才会发生这种情况。\u003c/p\u003e\u003cp\u003e\u003ccode\u003eIF NOT EXISTS\u003c/code\u003e选项是一种 \u003cspan\u003ePostgreSQL\u003c/span\u003e扩展。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e另见\u003c/h2\u003e\u003cspan\u003e\u003ca href=\"/wiki/sql/alter-schema/?v=18\" title=\"ALTER SCHEMA\" rel=\"nofollow\"\u003e\u003cspan\u003eALTER SCHEMA\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-schema/?v=18\" title=\"DROP SCHEMA\" rel=\"nofollow\"\u003e\u003cspan\u003eDROP SCHEMA\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e\u003c/section\u003e","SourceRevision":"1b5ca64c","ContentHash":"afb137fa58112e01b8c46b849c00a35ff476a3d8ac1dc95c0bd84b912e678f7b","Payload":{"purpose_zh":"定义一个新模式","sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE SCHEMA\u003c/code\u003e在当前数据库中创建一个新模式。该模式名必须与当前数据库中任何现有模式的名称不同。\u003c/p\u003e\u003cp\u003e模式本质上是一个命名空间：它包含具名对象（表、数据类型、函数和操作符），这些对象的名称可以与其他模式中的对象重名。访问具名对象时，可以在其名称前加上模式名作为前缀来\u003cspan class=\"quote\"\u003e“\u003cspan class=\"quote\"\u003e限定\u003c/span\u003e”\u003c/span\u003e名称，或者设置一个包含所需模式的搜索路径。指定非限定对象名的\u003ccode class=\"literal\"\u003eCREATE\u003c/code\u003e命令会在当前模式中创建该对象（即搜索路径最前面的模式，可通过函数\u003ccode class=\"function\"\u003ecurrent_schema\u003c/code\u003e确定）。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE SCHEMA\u003c/code\u003e还可以选择包含子命令，以便在新模式中创建对象。这些子命令基本上会被当作在创建模式之后单独发出的命令来处理。不过，如果使用了\u003ccode class=\"literal\"\u003eAUTHORIZATION\u003c/code\u003e子句，则所有创建的对象都将归该用户所有。\u003c/p\u003e","key":"description","title":"描述"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要创建的模式名称。如果省略，\u003cem class=\"replaceable\"\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e将被用作模式名。该名称不能以\u003ccode class=\"literal\"\u003epg_\u003c/code\u003e开头，因为这类名称保留给系统模式。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e将拥有新模式的用户的角色名。如果省略，则默认为执行该命令的用户。要创建由另一个角色拥有的模式，你必须能够对该角色执行 \u003ccode class=\"literal\"\u003eSET ROLE\u003c/code\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_element\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e定义要在该模式中创建的对象的 SQL 语句。当前，只有\u003ccode class=\"command\"\u003eCREATE TABLE\u003c/code\u003e、\u003ccode class=\"command\"\u003eCREATE VIEW\u003c/code\u003e、\u003ccode class=\"command\"\u003eCREATE INDEX\u003c/code\u003e、\u003ccode class=\"command\"\u003eCREATE SEQUENCE\u003c/code\u003e、\u003ccode class=\"command\"\u003eCREATE TRIGGER\u003c/code\u003e和\u003ccode class=\"command\"\u003eGRANT\u003c/code\u003e可作为 \u003ccode class=\"command\"\u003eCREATE SCHEMA\u003c/code\u003e中的子句使用。其他类型的对象可以在模式创建之后通过单独的命令创建。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eIF NOT EXISTS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果同名模式已经存在，则不执行任何操作（但会发出一条提示）。使用该选项时不能包含 \u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_element\u003c/code\u003e\u003c/em\u003e子命令。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"参数"},{"html":"\u003cp\u003e要创建一个模式，执行该命令的用户必须拥有当前数据库的\u003ccode class=\"literal\"\u003eCREATE\u003c/code\u003e 权限（当然，超级用户可以绕过这项检查）。\u003c/p\u003e","key":"notes","title":"注解"},{"html":"\u003cp\u003e创建一个模式：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE SCHEMA myschema;\n\u003c/pre\u003e\u003cp\u003e为用户\u003ccode class=\"literal\"\u003ejoe\u003c/code\u003e创建一个模式，该模式也将被命名为 \u003ccode class=\"literal\"\u003ejoe\u003c/code\u003e：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE SCHEMA AUTHORIZATION joe;\n\u003c/pre\u003e\u003cp\u003e创建一个被用户\u003ccode class=\"literal\"\u003ejoe\u003c/code\u003e拥有的名为\u003ccode class=\"literal\"\u003etest\u003c/code\u003e的模式，除非已经有一个名为\u003ccode class=\"literal\"\u003etest\u003c/code\u003e的模式（不管\u003ccode class=\"literal\"\u003ejoe\u003c/code\u003e 是否拥有那个预先存在的模式）。\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE SCHEMA IF NOT EXISTS test AUTHORIZATION joe;\n\u003c/pre\u003e\u003cp\u003e创建一个模式并且在其中创建一个表和视图：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE SCHEMA hollywood\n    CREATE TABLE films (title text, release date, awards text[])\n    CREATE VIEW winners AS\n        SELECT title, release FROM films WHERE awards IS NOT NULL;\n\u003c/pre\u003e\u003cp\u003e请注意，各个子命令末尾都不带分号。\u003c/p\u003e\u003cp\u003e下面是实现相同结果的一种等效写法：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE SCHEMA hollywood;\nCREATE TABLE hollywood.films (title text, release date, awards text[]);\nCREATE VIEW hollywood.winners AS\n    SELECT title, release FROM hollywood.films WHERE awards IS NOT NULL;\n\u003c/pre\u003e","key":"examples","title":"示例"},{"html":"\u003cp\u003eSQL 标准允许在\u003ccode class=\"command\"\u003eCREATE SCHEMA\u003c/code\u003e中使用 \u003ccode class=\"literal\"\u003eDEFAULT CHARACTER SET\u003c/code\u003e子句，并允许使用比 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e当前所接受的更多子命令类型。\u003c/p\u003e\u003cp\u003eSQL 标准规定，\u003ccode class=\"command\"\u003eCREATE SCHEMA\u003c/code\u003e中的子命令可以按任意顺序出现。当前的\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e实现并不能处理子命令中所有前向引用的情况；有时可能需要重新排列子命令的顺序，以避免前向引用。\u003c/p\u003e\u003cp\u003e根据 SQL 标准，模式的拥有者总是拥有其中的所有对象。\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e允许模式包含由模式拥有者之外的用户所拥有的对象。只有当模式拥有者把其模式上的\u003ccode class=\"literal\"\u003eCREATE\u003c/code\u003e权限授予其他人，或者超级用户选择在该模式中创建对象时，才会发生这种情况。\u003c/p\u003e\u003cp\u003e\u003ccode class=\"literal\"\u003eIF NOT EXISTS\u003c/code\u003e选项是一种 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e扩展。\u003c/p\u003e","key":"compatibility","title":"兼容性"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/alter-schema/?v=18\" title=\"ALTER SCHEMA\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER SCHEMA\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-schema/?v=18\" title=\"DROP SCHEMA\"\u003e\u003cspan class=\"refentrytitle\"\u003eDROP SCHEMA\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"另见"}],"sections_same_as":"","synopsis_html":"CREATE SCHEMA \u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_name\u003c/code\u003e\u003c/em\u003e [ AUTHORIZATION \u003cem class=\"replaceable\"\u003e\u003ccode\u003erole_specification\u003c/code\u003e\u003c/em\u003e ] [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_element\u003c/code\u003e\u003c/em\u003e [ ... ] ]\nCREATE SCHEMA AUTHORIZATION \u003cem class=\"replaceable\"\u003e\u003ccode\u003erole_specification\u003c/code\u003e\u003c/em\u003e [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_element\u003c/code\u003e\u003c/em\u003e [ ... ] ]\nCREATE SCHEMA IF NOT EXISTS \u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_name\u003c/code\u003e\u003c/em\u003e [ AUTHORIZATION \u003cem class=\"replaceable\"\u003e\u003ccode\u003erole_specification\u003c/code\u003e\u003c/em\u003e ]\nCREATE SCHEMA IF NOT EXISTS AUTHORIZATION \u003cem class=\"replaceable\"\u003e\u003ccode\u003erole_specification\u003c/code\u003e\u003c/em\u003e\n\n\u003cspan class=\"phrase\"\u003e其中 \u003cem class=\"replaceable\"\u003e\u003ccode\u003erole_specification\u003c/code\u003e\u003c/em\u003e 可以是：\u003c/span\u003e\n\n    \u003cem class=\"replaceable\"\u003e\u003ccode\u003euser_name\u003c/code\u003e\u003c/em\u003e\n  | CURRENT_ROLE\n  | CURRENT_USER\n  | SESSION_USER","synopsis_text":"CREATE SCHEMA schema_name [ AUTHORIZATION role_specification ] [ schema_element [ ... ] ]\nCREATE SCHEMA AUTHORIZATION role_specification [ schema_element [ ... ] ]\nCREATE SCHEMA IF NOT EXISTS schema_name [ AUTHORIZATION role_specification ]\nCREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_specification\n其中 role_specification 可以是：\nuser_name\n| CURRENT_ROLE\n| CURRENT_USER\n| SESSION_USER"}},"RequestedLocale":"zh-Hans","Fallback":false,"Versions":["10","11","12","13","14","15","16","17","18","19","20","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}
