{"Entry":{"collection":"sql","key":"create-extension","name":"CREATE EXTENSION","aliases":["createextension"],"metadata":{"aliases":["createextension"],"changed_in":["9.6","13"],"changes":[{"from":"9.0","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"added","synopsis":null,"to":"9.1"},{"from":"9.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes","examples"],"removed":[]},"status":"changed","synopsis":null,"to":"9.5"},{"from":"9.5","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes"],"removed":[]},"status":"changed","synopsis":{"added":["[ CASCADE ]"],"removed":[]},"to":"9.6"},{"from":"9.6","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters","notes"],"removed":[]},"status":"changed","synopsis":null,"to":"10"},{"from":"10","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"11"},{"from":"11","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"12"},{"from":"12","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","parameters","examples"],"removed":[]},"status":"changed","synopsis":{"added":[],"removed":["[ FROM old_version ]","[ CASCADE ]"]},"to":"13"},{"from":"13","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"14"},{"from":"16","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"17"},{"from":"17","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":null,"to":"18"}],"content_hash":"c16511a9a539d23ab7bcb72837aad33ea18b989e7c022110a38e6521cd2009e1","editorial":{},"first_version":"9.1","group":"extension","imported_at":"2026-09-30T17:43:38.510397+08:00","last_version":"20","name":"CREATE EXTENSION","object":"EXTENSION","position":7003,"present_in":["9.1","9.2","9.3","9.4","9.5","9.6","10","11","12","13","14","15","16","17","18","19","20"],"purpose":"install an extension","purpose_zh":"","related":["alter-extension","drop-extension"],"slug":"create-extension","source_rev":"a709ab85","synopsis":"CREATE EXTENSION [ IF NOT EXISTS ] extension_name\n[ WITH ] [ SCHEMA schema_name ]\n[ VERSION version ]\n[ CASCADE ]","verb":"CREATE"}},"Definition":{"Collection":"sql","Key":"create-extension","SourceDatabase":"center","Version":"18","SourceTable":"sqlcmd","SourceKey":"create-extension","SourceRevision":"a709ab85","Facts":{"anchor":"SQL-CREATEEXTENSION","file":"sql-createextension.html","lang":"en","name":"CREATE EXTENSION","purpose":"install an extension","purpose_zh":"","related":["alter-extension","drop-extension"],"sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE EXTENSION\u003c/code\u003e loads a new extension into the current database. There must not be an extension of the same name already loaded.\u003c/p\u003e\u003cp\u003eLoading an extension essentially amounts to running the extension's script file. The script will typically create new \u003cacronym\u003eSQL\u003c/acronym\u003e objects such as functions, data types, operators and index support methods. \u003ccode class=\"command\"\u003eCREATE EXTENSION\u003c/code\u003e additionally records the identities of all the created objects, so that they can be dropped again if \u003ccode class=\"command\"\u003eDROP EXTENSION\u003c/code\u003e is issued.\u003c/p\u003e\u003cp\u003eThe user who runs \u003ccode class=\"command\"\u003eCREATE EXTENSION\u003c/code\u003e becomes the owner of the extension for purposes of later privilege checks, and normally also becomes the owner of any objects created by the extension's script.\u003c/p\u003e\u003cp\u003eLoading an extension ordinarily requires the same privileges that would be required to create its component objects. For many extensions this means superuser privileges are needed. However, if the extension is marked \u003cem class=\"firstterm\"\u003etrusted\u003c/em\u003e in its control file, then it can be installed by any user who has \u003ccode class=\"literal\"\u003eCREATE\u003c/code\u003e privilege on the current database. In this case the extension object itself will be owned by the calling user, but the contained objects will be owned by the bootstrap superuser (unless the extension's script explicitly assigns them to the calling user). This configuration gives the calling user the right to drop the extension, but not to modify individual objects within it.\u003c/p\u003e","key":"description","title":"Description"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\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 not throw an error if an extension with the same name already exists. A notice is issued in this case. Note that there is no guarantee that the existing extension is anything like the one that would have been created from the currently-available script file.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eextension_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name of the extension to be installed. \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e will create the extension using details from the file \u003ccode class=\"filename\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eextension_name\u003c/code\u003e\u003c/em\u003e.control\u003c/code\u003e, found via the server's extension control path (set by \u003ca href=\"/docs/18/runtime-config-client.html#GUC-EXTENSION-CONTROL-PATH\"\u003eextension_control_path\u003c/a\u003e.)\u003c/p\u003e\u003c/dd\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 the schema in which to install the extension's objects, given that the extension allows its contents to be relocated. The named schema must already exist. If not specified, and the extension's control file does not specify a schema either, the current default object creation schema is used.\u003c/p\u003e\u003cp\u003eIf the extension specifies a \u003ccode class=\"literal\"\u003eschema\u003c/code\u003e parameter in its control file, then that schema cannot be overridden with a \u003ccode class=\"literal\"\u003eSCHEMA\u003c/code\u003e clause. Normally, an error will be raised if a \u003ccode class=\"literal\"\u003eSCHEMA\u003c/code\u003e clause is given and it conflicts with the extension's \u003ccode class=\"literal\"\u003eschema\u003c/code\u003e parameter. However, if the \u003ccode class=\"literal\"\u003eCASCADE\u003c/code\u003e clause is also given, then \u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_name\u003c/code\u003e\u003c/em\u003e is ignored when it conflicts. The given \u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_name\u003c/code\u003e\u003c/em\u003e will be used for installation of any needed extensions that do not specify \u003ccode class=\"literal\"\u003eschema\u003c/code\u003e in their control files.\u003c/p\u003e\u003cp\u003eRemember that the extension itself is not considered to be within any schema: extensions have unqualified names that must be unique database-wide. But objects belonging to the extension can be within schemas.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eversion\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe version of the extension to install. This can be written as either an identifier or a string literal. The default version is whatever is specified in the extension's control file.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCASCADE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eAutomatically install any extensions that this extension depends on that are not already installed. Their dependencies are likewise automatically installed, recursively. The \u003ccode class=\"literal\"\u003eSCHEMA\u003c/code\u003e clause, if given, applies to all extensions that get installed this way. Other options of the statement are not applied to automatically-installed extensions; in particular, their default versions are always selected.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"Parameters"},{"html":"\u003cp\u003eBefore you can use \u003ccode class=\"command\"\u003eCREATE EXTENSION\u003c/code\u003e to load an extension into a database, the extension's supporting files must be installed. Information about installing the extensions supplied with \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e can be found in \u003ca href=\"/docs/18/contrib.html\" title=\"Appendix F. Additional Supplied Modules and Extensions\"\u003eAdditional Supplied Modules\u003c/a\u003e.\u003c/p\u003e\u003cp\u003eThe extensions currently available for loading can be identified from the \u003ca href=\"/docs/18/view-pg-available-extensions.html\" title=\"53.3. pg_available_extensions\"\u003e\u003ccode class=\"structname\"\u003epg_available_extensions\u003c/code\u003e\u003c/a\u003e or \u003ca href=\"/docs/18/view-pg-available-extension-versions.html\" title=\"53.4. pg_available_extension_versions\"\u003e\u003ccode class=\"structname\"\u003epg_available_extension_versions\u003c/code\u003e\u003c/a\u003e system views.\u003c/p\u003e\u003cdiv class=\"caution\"\u003e\u003ch3\u003eCaution\u003c/h3\u003e\u003cp\u003eInstalling an extension as superuser requires trusting that the extension's author wrote the extension installation script in a secure fashion. It is not terribly difficult for a malicious user to create trojan-horse objects that will compromise later execution of a carelessly-written extension script, allowing that user to acquire superuser privileges. However, trojan-horse objects are only hazardous if they are in the \u003ccode class=\"varname\"\u003esearch_path\u003c/code\u003e during script execution, meaning that they are in the extension's installation target schema or in the schema of some extension it depends on. Therefore, a good rule of thumb when dealing with extensions whose scripts have not been carefully vetted is to install them only into schemas for which CREATE privilege has not been and will not be granted to any untrusted users. Likewise for any extensions they depend on.\u003c/p\u003e\u003cp\u003eThe extensions supplied with \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e are believed to be secure against installation-time attacks of this sort, except for a few that depend on other extensions. As stated in the documentation for those extensions, they should be installed into secure schemas, or installed into the same schemas as the extensions they depend on, or both.\u003c/p\u003e\u003c/div\u003e\u003cp\u003eFor information about writing new extensions, see \u003ca href=\"/docs/18/extend-extensions.html\" title=\"36.17. Packaging Related Objects into an Extension\"\u003eSection 36.17\u003c/a\u003e.\u003c/p\u003e","key":"notes","title":"Notes"},{"html":"\u003cp\u003eInstall the \u003ca href=\"/docs/18/hstore.html\" title=\"F.17. hstore — hstore key/value datatype\"\u003ehstore\u003c/a\u003e extension into the current database, placing its objects in schema \u003ccode class=\"literal\"\u003eaddons\u003c/code\u003e:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE EXTENSION hstore SCHEMA addons;\n\u003c/pre\u003e\u003cp\u003eAnother way to accomplish the same thing:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eSET search_path = addons;\nCREATE EXTENSION hstore;\n\u003c/pre\u003e","key":"examples","title":"Examples"},{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE EXTENSION\u003c/code\u003e 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-extension/?v=18\" title=\"ALTER EXTENSION\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER EXTENSION\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-extension/?v=18\" title=\"DROP EXTENSION\"\u003e\u003cspan class=\"refentrytitle\"\u003eDROP EXTENSION\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"See Also"}],"sections_same_as":"","slug":"18","synopsis_html":"CREATE EXTENSION [ IF NOT EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eextension_name\u003c/code\u003e\u003c/em\u003e\n    [ WITH ] [ SCHEMA \u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_name\u003c/code\u003e\u003c/em\u003e ]\n             [ VERSION \u003cem class=\"replaceable\"\u003e\u003ccode\u003eversion\u003c/code\u003e\u003c/em\u003e ]\n             [ CASCADE ]","synopsis_text":"CREATE EXTENSION [ IF NOT EXISTS ] extension_name\n[ WITH ] [ SCHEMA schema_name ]\n[ VERSION version ]\n[ CASCADE ]"},"ManualEvidence":{},"MeasuredEvidence":{}},"Text":{"Collection":"sql","Key":"create-extension","SourceDatabase":"pgweb","Version":"18","Locale":"zh-Hans","Title":"CREATE EXTENSION","Summary":"安装一个扩展","BodyHTML":"\u003cpre\u003eCREATE EXTENSION [ IF NOT EXISTS ] extension_name\n[ WITH ] [ SCHEMA schema_name ]\n[ VERSION version ]\n[ CASCADE ]\u003c/pre\u003e\u003csection\u003e\u003ch2\u003e描述\u003c/h2\u003e\u003cp\u003e\u003ccode\u003eCREATE EXTENSION\u003c/code\u003e将一个新扩展装载到当前数据库中。同名扩展不能已经被装载。\u003c/p\u003e\u003cp\u003e装载一个扩展本质上就是运行该扩展的脚本文件。该脚本通常会创建新的 \u003cacronym\u003eSQL\u003c/acronym\u003e 对象，例如函数、数据类型、操作符以及索引支持方法。\u003ccode\u003eCREATE EXTENSION\u003c/code\u003e还会记录所有已创建对象的标识，这样在发出\u003ccode\u003eDROP EXTENSION\u003c/code\u003e时就可以将它们一并删除。\u003c/p\u003e\u003cp\u003e运行\u003ccode\u003eCREATE EXTENSION\u003c/code\u003e的用户会成为该扩展的所有者，以供后续权限检查之用，并且通常也会成为扩展脚本所创建的任何对象的所有者。\u003c/p\u003e\u003cp\u003e装载扩展通常需要具备创建其组成对象所需的同等权限。对许多扩展来说，这意味着需要超级用户权限。不过，如果扩展在其控制文件中被标记为\u003cem\u003e受信任的\u003c/em\u003e，那么任何在当前数据库上拥有 \u003ccode\u003eCREATE\u003c/code\u003e 权限的用户都可以安装它。在这种情况下，扩展对象本身将由调用用户拥有，但其中包含的对象将由引导超级用户拥有（除非扩展脚本明确把它们赋给调用用户）。这种配置赋予调用用户删除该扩展的权利，但不允许其修改其中的单个对象。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e参数\u003c/h2\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eIF NOT EXISTS\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果已存在同名扩展，则不要抛出错误。这种情况下会发出一条提示。注意，这并不保证现有扩展与根据当前可用脚本文件本应创建出的扩展有任何相似之处。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003eextension_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要安装的扩展名称。\u003cspan\u003ePostgreSQL\u003c/span\u003e将根据文件 \u003ccode\u003e\u003cem\u003e\u003ccode\u003eextension_name\u003c/code\u003e\u003c/em\u003e.control\u003c/code\u003e 中的详细信息创建该扩展；该文件通过服务器的扩展控制路径找到（由\u003ca href=\"/docs/18/runtime-config-client.html#GUC-EXTENSION-CONTROL-PATH\" rel=\"nofollow\"\u003eextension_control_path\u003c/a\u003e设置）。\u003c/p\u003e\u003c/dd\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如果该扩展允许其内容被重定位，则这是安装该扩展所含对象的模式名称。指定的模式必须已经存在。如果未指定，并且扩展控制文件中也未指定模式，则使用当前默认的对象创建模式。\u003c/p\u003e\u003cp\u003e如果扩展在其控制文件中指定了 \u003ccode\u003eschema\u003c/code\u003e 参数，就不能用 \u003ccode\u003eSCHEMA\u003c/code\u003e 子句覆盖该模式。通常，如果给出了 \u003ccode\u003eSCHEMA\u003c/code\u003e 子句，且它与扩展的 \u003ccode\u003eschema\u003c/code\u003e 参数冲突，就会报错。不过，如果同时给出 \u003ccode\u003eCASCADE\u003c/code\u003e 子句，则发生冲突时会忽略 \u003cem\u003e\u003ccode\u003eschema_name\u003c/code\u003e\u003c/em\u003e。给定的 \u003cem\u003e\u003ccode\u003eschema_name\u003c/code\u003e\u003c/em\u003e 将用于安装任何必需且其控制文件中未指定 \u003ccode\u003eschema\u003c/code\u003e 的扩展。\u003c/p\u003e\u003cp\u003e请记住，扩展本身不被视为属于任何模式：扩展的名称是非限定名，并且必须在整个数据库范围内唯一。但是，属于该扩展的对象可以位于模式中。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003eversion\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要安装的扩展版本。它可以写成标识符，也可以写成字符串常量。默认版本由扩展控制文件指定。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eCASCADE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e自动安装该扩展所依赖、但尚未安装的任何扩展。它们的依赖也会以同样的方式递归自动安装。如果给出了 \u003ccode\u003eSCHEMA\u003c/code\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 EXTENSION\u003c/code\u003e 将扩展装载到数据库之前，必须先安装好该扩展的支持文件。关于安装 \u003cspan\u003ePostgreSQL\u003c/span\u003e 随附扩展的信息，可见\u003ca href=\"/docs/18/contrib.html\" rel=\"nofollow\"\u003e额外提供的模块\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e当前可供装载的扩展可从系统视图 \u003ca href=\"/docs/18/view-pg-available-extensions.html\" rel=\"nofollow\"\u003e\u003ccode\u003epg_available_extensions\u003c/code\u003e\u003c/a\u003e 或 \u003ca href=\"/docs/18/view-pg-available-extension-versions.html\" rel=\"nofollow\"\u003e\u003ccode\u003epg_available_extension_versions\u003c/code\u003e\u003c/a\u003e 中查出。\u003c/p\u003e\u003cdiv\u003e\u003ch3\u003e小心\u003c/h3\u003e\u003cp\u003e以超级用户身份安装扩展，意味着必须相信扩展作者以安全的方式编写了扩展安装脚本。恶意用户要创建特洛伊木马对象并不算太难；这类对象可能会在之后执行编写不严谨的扩展脚本时实施攻击，使该用户获得超级用户权限。不过，只有当特洛伊木马对象在脚本执行期间位于 \u003ccode\u003esearch_path\u003c/code\u003e 中时，它们才会构成危险；这意味着它们位于扩展的安装目标模式中，或位于它所依赖的某个扩展的目标模式中。因此，处理那些脚本尚未经过仔细审查的扩展时，一个经验法则是：只把它们安装到从未向任何不受信任用户授予、而且今后也不会授予 CREATE 权限的模式中。它们所依赖的任何扩展也应如此。\u003c/p\u003e\u003cp\u003e一般认为，\u003cspan\u003ePostgreSQL\u003c/span\u003e 随附的扩展能够抵御这类安装时攻击，但少数依赖其他扩展的扩展除外。正如这些扩展的文档所述，它们应当安装到安全模式中，或者安装到与其所依赖扩展相同的模式中，或者同时满足这两点。\u003c/p\u003e\u003c/div\u003e\u003cp\u003e关于编写新扩展的信息，见\u003ca href=\"/docs/18/extend-extensions.html\" rel=\"nofollow\"\u003e第 36.17 节\u003c/a\u003e。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e示例\u003c/h2\u003e\u003cp\u003e将 \u003ca href=\"/docs/18/hstore.html\" rel=\"nofollow\"\u003ehstore\u003c/a\u003e 扩展安装到当前数据库中，并把其对象放在 \u003ccode\u003eaddons\u003c/code\u003e 模式中：\u003c/p\u003e\u003cpre\u003eCREATE EXTENSION hstore SCHEMA addons;\n\u003c/pre\u003e\u003cp\u003e实现同样效果的另一种方式是：\u003c/p\u003e\u003cpre\u003eSET search_path = addons;\nCREATE EXTENSION hstore;\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003e\u003ccode\u003eCREATE EXTENSION\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-extension/?v=18\" title=\"ALTER EXTENSION\" rel=\"nofollow\"\u003e\u003cspan\u003eALTER EXTENSION\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-extension/?v=18\" title=\"DROP EXTENSION\" rel=\"nofollow\"\u003e\u003cspan\u003eDROP EXTENSION\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e\u003c/section\u003e","SourceRevision":"1b5ca64c","ContentHash":"9023d6e2f71a8621a292e44d1623d35f11bdd6535e4889ca92f385540252a2f9","Payload":{"purpose_zh":"安装一个扩展","sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE EXTENSION\u003c/code\u003e将一个新扩展装载到当前数据库中。同名扩展不能已经被装载。\u003c/p\u003e\u003cp\u003e装载一个扩展本质上就是运行该扩展的脚本文件。该脚本通常会创建新的 \u003cacronym\u003eSQL\u003c/acronym\u003e 对象，例如函数、数据类型、操作符以及索引支持方法。\u003ccode class=\"command\"\u003eCREATE EXTENSION\u003c/code\u003e还会记录所有已创建对象的标识，这样在发出\u003ccode class=\"command\"\u003eDROP EXTENSION\u003c/code\u003e时就可以将它们一并删除。\u003c/p\u003e\u003cp\u003e运行\u003ccode class=\"command\"\u003eCREATE EXTENSION\u003c/code\u003e的用户会成为该扩展的所有者，以供后续权限检查之用，并且通常也会成为扩展脚本所创建的任何对象的所有者。\u003c/p\u003e\u003cp\u003e装载扩展通常需要具备创建其组成对象所需的同等权限。对许多扩展来说，这意味着需要超级用户权限。不过，如果扩展在其控制文件中被标记为\u003cem class=\"firstterm\"\u003e受信任的\u003c/em\u003e，那么任何在当前数据库上拥有 \u003ccode class=\"literal\"\u003eCREATE\u003c/code\u003e 权限的用户都可以安装它。在这种情况下，扩展对象本身将由调用用户拥有，但其中包含的对象将由引导超级用户拥有（除非扩展脚本明确把它们赋给调用用户）。这种配置赋予调用用户删除该扩展的权利，但不允许其修改其中的单个对象。\u003c/p\u003e","key":"description","title":"描述"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\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如果已存在同名扩展，则不要抛出错误。这种情况下会发出一条提示。注意，这并不保证现有扩展与根据当前可用脚本文件本应创建出的扩展有任何相似之处。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eextension_name\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要安装的扩展名称。\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e将根据文件 \u003ccode class=\"filename\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eextension_name\u003c/code\u003e\u003c/em\u003e.control\u003c/code\u003e 中的详细信息创建该扩展；该文件通过服务器的扩展控制路径找到（由\u003ca href=\"/docs/18/runtime-config-client.html#GUC-EXTENSION-CONTROL-PATH\"\u003eextension_control_path\u003c/a\u003e设置）。\u003c/p\u003e\u003c/dd\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如果该扩展允许其内容被重定位，则这是安装该扩展所含对象的模式名称。指定的模式必须已经存在。如果未指定，并且扩展控制文件中也未指定模式，则使用当前默认的对象创建模式。\u003c/p\u003e\u003cp\u003e如果扩展在其控制文件中指定了 \u003ccode class=\"literal\"\u003eschema\u003c/code\u003e 参数，就不能用 \u003ccode class=\"literal\"\u003eSCHEMA\u003c/code\u003e 子句覆盖该模式。通常，如果给出了 \u003ccode class=\"literal\"\u003eSCHEMA\u003c/code\u003e 子句，且它与扩展的 \u003ccode class=\"literal\"\u003eschema\u003c/code\u003e 参数冲突，就会报错。不过，如果同时给出 \u003ccode class=\"literal\"\u003eCASCADE\u003c/code\u003e 子句，则发生冲突时会忽略 \u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_name\u003c/code\u003e\u003c/em\u003e。给定的 \u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_name\u003c/code\u003e\u003c/em\u003e 将用于安装任何必需且其控制文件中未指定 \u003ccode class=\"literal\"\u003eschema\u003c/code\u003e 的扩展。\u003c/p\u003e\u003cp\u003e请记住，扩展本身不被视为属于任何模式：扩展的名称是非限定名，并且必须在整个数据库范围内唯一。但是，属于该扩展的对象可以位于模式中。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003eversion\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要安装的扩展版本。它可以写成标识符，也可以写成字符串常量。默认版本由扩展控制文件指定。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eCASCADE\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e自动安装该扩展所依赖、但尚未安装的任何扩展。它们的依赖也会以同样的方式递归自动安装。如果给出了 \u003ccode class=\"literal\"\u003eSCHEMA\u003c/code\u003e 子句，它将适用于所有以这种方式安装的扩展。该语句的其他选项不会应用于自动安装的扩展；特别是，这些扩展总是选择其默认版本。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"参数"},{"html":"\u003cp\u003e在使用 \u003ccode class=\"command\"\u003eCREATE EXTENSION\u003c/code\u003e 将扩展装载到数据库之前，必须先安装好该扩展的支持文件。关于安装 \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 随附扩展的信息，可见\u003ca href=\"/docs/18/contrib.html\" title=\"附录 F. 额外提供的模块与扩展\"\u003e额外提供的模块\u003c/a\u003e。\u003c/p\u003e\u003cp\u003e当前可供装载的扩展可从系统视图 \u003ca href=\"/docs/18/view-pg-available-extensions.html\" title=\"53.3. pg_available_extensions\"\u003e\u003ccode class=\"structname\"\u003epg_available_extensions\u003c/code\u003e\u003c/a\u003e 或 \u003ca href=\"/docs/18/view-pg-available-extension-versions.html\" title=\"53.4. pg_available_extension_versions\"\u003e\u003ccode class=\"structname\"\u003epg_available_extension_versions\u003c/code\u003e\u003c/a\u003e 中查出。\u003c/p\u003e\u003cdiv class=\"caution\"\u003e\u003ch3\u003e小心\u003c/h3\u003e\u003cp\u003e以超级用户身份安装扩展，意味着必须相信扩展作者以安全的方式编写了扩展安装脚本。恶意用户要创建特洛伊木马对象并不算太难；这类对象可能会在之后执行编写不严谨的扩展脚本时实施攻击，使该用户获得超级用户权限。不过，只有当特洛伊木马对象在脚本执行期间位于 \u003ccode class=\"varname\"\u003esearch_path\u003c/code\u003e 中时，它们才会构成危险；这意味着它们位于扩展的安装目标模式中，或位于它所依赖的某个扩展的目标模式中。因此，处理那些脚本尚未经过仔细审查的扩展时，一个经验法则是：只把它们安装到从未向任何不受信任用户授予、而且今后也不会授予 CREATE 权限的模式中。它们所依赖的任何扩展也应如此。\u003c/p\u003e\u003cp\u003e一般认为，\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 随附的扩展能够抵御这类安装时攻击，但少数依赖其他扩展的扩展除外。正如这些扩展的文档所述，它们应当安装到安全模式中，或者安装到与其所依赖扩展相同的模式中，或者同时满足这两点。\u003c/p\u003e\u003c/div\u003e\u003cp\u003e关于编写新扩展的信息，见\u003ca href=\"/docs/18/extend-extensions.html\" title=\"36.17. 将相关对象打包成扩展\"\u003e第 36.17 节\u003c/a\u003e。\u003c/p\u003e","key":"notes","title":"注解"},{"html":"\u003cp\u003e将 \u003ca href=\"/docs/18/hstore.html\" title=\"F.17. hstore — hstore 键/值数据类型\"\u003ehstore\u003c/a\u003e 扩展安装到当前数据库中，并把其对象放在 \u003ccode class=\"literal\"\u003eaddons\u003c/code\u003e 模式中：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eCREATE EXTENSION hstore SCHEMA addons;\n\u003c/pre\u003e\u003cp\u003e实现同样效果的另一种方式是：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eSET search_path = addons;\nCREATE EXTENSION hstore;\n\u003c/pre\u003e","key":"examples","title":"示例"},{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eCREATE EXTENSION\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-extension/?v=18\" title=\"ALTER EXTENSION\"\u003e\u003cspan class=\"refentrytitle\"\u003eALTER EXTENSION\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/drop-extension/?v=18\" title=\"DROP EXTENSION\"\u003e\u003cspan class=\"refentrytitle\"\u003eDROP EXTENSION\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"另见"}],"sections_same_as":"","synopsis_html":"CREATE EXTENSION [ IF NOT EXISTS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eextension_name\u003c/code\u003e\u003c/em\u003e\n    [ WITH ] [ SCHEMA \u003cem class=\"replaceable\"\u003e\u003ccode\u003eschema_name\u003c/code\u003e\u003c/em\u003e ]\n             [ VERSION \u003cem class=\"replaceable\"\u003e\u003ccode\u003eversion\u003c/code\u003e\u003c/em\u003e ]\n             [ CASCADE ]","synopsis_text":"CREATE EXTENSION [ IF NOT EXISTS ] extension_name\n[ WITH ] [ SCHEMA schema_name ]\n[ VERSION version ]\n[ CASCADE ]"}},"RequestedLocale":"zh-Hans","Fallback":false,"Versions":["10","11","12","13","14","15","16","17","18","19","20","9.1","9.2","9.3","9.4","9.5","9.6"],"Locales":["en","zh-Hans"],"Signatures":null,"Spellings":null,"SQLState":null,"Evidence":null}
