gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
12KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>CREATE EXTENSION</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="sql-createeventtrigger.html" title="CREATE EVENT TRIGGER" /><link rel="next" href="sql-createforeigndatawrapper.html" title="CREATE FOREIGN DATA WRAPPER" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">CREATE EXTENSION</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createeventtrigger.html" title="CREATE EVENT TRIGGER">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-createforeigndatawrapper.html" title="CREATE FOREIGN DATA WRAPPER">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATEEXTENSION"><div class="titlepage"></div><a id="id-1.9.3.64.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE EXTENSION</span></h2><p>CREATE EXTENSION — install an extension</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CREATE EXTENSION [ IF NOT EXISTS ] <em class="replaceable"><code>extension_name</code></em>
  4. [ WITH ] [ SCHEMA <em class="replaceable"><code>schema_name</code></em> ]
  5. [ VERSION <em class="replaceable"><code>version</code></em> ]
  6. [ FROM <em class="replaceable"><code>old_version</code></em> ]
  7. [ CASCADE ]
  8. </pre></div><div class="refsect1" id="id-1.9.3.64.5"><h2>Description</h2><p>
  9. <code class="command">CREATE EXTENSION</code> loads a new extension into the current
  10. database. There must not be an extension of the same name already loaded.
  11. </p><p>
  12. Loading an extension essentially amounts to running the extension's script
  13. file. The script will typically create new <acronym class="acronym">SQL</acronym> objects such as
  14. functions, data types, operators and index support methods.
  15. <code class="command">CREATE EXTENSION</code> additionally records the identities
  16. of all the created objects, so that they can be dropped again if
  17. <code class="command">DROP EXTENSION</code> is issued.
  18. </p><p>
  19. Loading an extension requires the same privileges that would be
  20. required to create its component objects. For most extensions this
  21. means superuser or database owner privileges are needed.
  22. The user who runs <code class="command">CREATE EXTENSION</code> becomes the
  23. owner of the extension for purposes of later privilege checks, as well
  24. as the owner of any objects created by the extension's script.
  25. </p></div><div class="refsect1" id="id-1.9.3.64.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">IF NOT EXISTS</code></span></dt><dd><p>
  26. Do not throw an error if an extension with the same name already
  27. exists. A notice is issued in this case. Note that there is no
  28. guarantee that the existing extension is anything like the one that
  29. would have been created from the currently-available script file.
  30. </p></dd><dt><span class="term"><em class="replaceable"><code>extension_name</code></em></span></dt><dd><p>
  31. The name of the extension to be
  32. installed. <span class="productname">PostgreSQL</span> will create the
  33. extension using details from the file
  34. <code class="literal">SHAREDIR/extension/</code><em class="replaceable"><code>extension_name</code></em><code class="literal">.control</code>.
  35. </p></dd><dt><span class="term"><em class="replaceable"><code>schema_name</code></em></span></dt><dd><p>
  36. The name of the schema in which to install the extension's
  37. objects, given that the extension allows its contents to be
  38. relocated. The named schema must already exist.
  39. If not specified, and the extension's control file does not specify a
  40. schema either, the current default object creation schema is used.
  41. </p><p>
  42. If the extension specifies a <code class="literal">schema</code> parameter in its
  43. control file, then that schema cannot be overridden with
  44. a <code class="literal">SCHEMA</code> clause. Normally, an error will be raised if
  45. a <code class="literal">SCHEMA</code> clause is given and it conflicts with the
  46. extension's <code class="literal">schema</code> parameter. However, if
  47. the <code class="literal">CASCADE</code> clause is also given,
  48. then <em class="replaceable"><code>schema_name</code></em> is
  49. ignored when it conflicts. The
  50. given <em class="replaceable"><code>schema_name</code></em> will be
  51. used for installation of any needed extensions that do not
  52. specify <code class="literal">schema</code> in their control files.
  53. </p><p>
  54. Remember that the extension itself is not considered to be within any
  55. schema: extensions have unqualified names that must be unique
  56. database-wide. But objects belonging to the extension can be within
  57. schemas.
  58. </p></dd><dt><span class="term"><em class="replaceable"><code>version</code></em></span></dt><dd><p>
  59. The version of the extension to install. This can be written as
  60. either an identifier or a string literal. The default version is
  61. whatever is specified in the extension's control file.
  62. </p></dd><dt><span class="term"><em class="replaceable"><code>old_version</code></em></span></dt><dd><p>
  63. <code class="literal">FROM</code> <em class="replaceable"><code>old_version</code></em>
  64. must be specified when, and only when, you are attempting to install
  65. an extension that replaces an <span class="quote">“<span class="quote">old style</span>”</span> module that is just
  66. a collection of objects not packaged into an extension. This option
  67. causes <code class="command">CREATE EXTENSION</code> to run an alternative installation
  68. script that absorbs the existing objects into the extension, instead
  69. of creating new objects. Be careful that <code class="literal">SCHEMA</code> specifies
  70. the schema containing these pre-existing objects.
  71. </p><p>
  72. The value to use for <em class="replaceable"><code>old_version</code></em> is determined by the
  73. extension's author, and might vary if there is more than one version
  74. of the old-style module that can be upgraded into an extension.
  75. For the standard additional modules supplied with pre-9.1
  76. <span class="productname">PostgreSQL</span>, use <code class="literal">unpackaged</code>
  77. for <em class="replaceable"><code>old_version</code></em> when
  78. updating a module to extension style.
  79. </p></dd><dt><span class="term"><code class="literal">CASCADE</code></span></dt><dd><p>
  80. Automatically install any extensions that this extension depends on
  81. that are not already installed. Their dependencies are likewise
  82. automatically installed, recursively. The <code class="literal">SCHEMA</code> clause,
  83. if given, applies to all extensions that get installed this way.
  84. Other options of the statement are not applied to
  85. automatically-installed extensions; in particular, their default
  86. versions are always selected.
  87. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.64.7"><h2>Notes</h2><p>
  88. Before you can use <code class="command">CREATE EXTENSION</code> to load an extension
  89. into a database, the extension's supporting files must be installed.
  90. Information about installing the extensions supplied with
  91. <span class="productname">PostgreSQL</span> can be found in
  92. <a class="link" href="contrib.html" title="Appendix F. Additional Supplied Modules">Additional Supplied Modules</a>.
  93. </p><p>
  94. The extensions currently available for loading can be identified from the
  95. <a class="link" href="view-pg-available-extensions.html" title="51.66. pg_available_extensions"><code class="structname">pg_available_extensions</code></a>
  96. or
  97. <a class="link" href="view-pg-available-extension-versions.html" title="51.67. pg_available_extension_versions"><code class="structname">pg_available_extension_versions</code></a>
  98. system views.
  99. </p><div class="caution"><h3 class="title">Caution</h3><p>
  100. Installing an extension as superuser requires trusting that the
  101. extension's author wrote the extension installation script in a secure
  102. fashion. It is not terribly difficult for a malicious user to create
  103. trojan-horse objects that will compromise later execution of a
  104. carelessly-written extension script, allowing that user to acquire
  105. superuser privileges. However, trojan-horse objects are only hazardous
  106. if they are in the <code class="varname">search_path</code> during script
  107. execution, meaning that they are in the extension's installation target
  108. schema or in the schema of some extension it depends on. Therefore, a
  109. good rule of thumb when dealing with extensions whose scripts have not
  110. been carefully vetted is to install them only into schemas for which
  111. CREATE privilege has not been and will not be granted to any untrusted
  112. users. Likewise for any extensions they depend on.
  113. </p><p>
  114. The extensions supplied with <span class="productname">PostgreSQL</span> are
  115. believed to be secure against installation-time attacks of this sort,
  116. except for a few that depend on other extensions. As stated in the
  117. documentation for those extensions, they should be installed into secure
  118. schemas, or installed into the same schemas as the extensions they
  119. depend on, or both.
  120. </p></div><p>
  121. For information about writing new extensions, see
  122. <a class="xref" href="extend-extensions.html" title="37.17. Packaging Related Objects into an Extension">Section 37.17</a>.
  123. </p></div><div class="refsect1" id="id-1.9.3.64.8"><h2>Examples</h2><p>
  124. Install the <a class="link" href="hstore.html" title="F.16. hstore">hstore</a> extension into the
  125. current database, placing its objects in schema <code class="literal">addons</code>:
  126. </p><pre class="programlisting">
  127. CREATE EXTENSION hstore SCHEMA addons;
  128. </pre><p>
  129. Another way to accomplish the same thing:
  130. </p><pre class="programlisting">
  131. SET search_path = addons;
  132. CREATE EXTENSION hstore;
  133. </pre><p>
  134. </p><p>
  135. Update a pre-9.1 installation of <code class="literal">hstore</code> into
  136. extension style:
  137. </p><pre class="programlisting">
  138. CREATE EXTENSION hstore SCHEMA public FROM unpackaged;
  139. </pre><p>
  140. Be careful to specify the schema in which you installed the existing
  141. <code class="literal">hstore</code> objects.
  142. </p></div><div class="refsect1" id="id-1.9.3.64.9"><h2>Compatibility</h2><p>
  143. <code class="command">CREATE EXTENSION</code> is a <span class="productname">PostgreSQL</span>
  144. extension.
  145. </p></div><div class="refsect1" id="id-1.9.3.64.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-alterextension.html" title="ALTER EXTENSION"><span class="refentrytitle">ALTER EXTENSION</span></a>, <a class="xref" href="sql-dropextension.html" title="DROP EXTENSION"><span class="refentrytitle">DROP EXTENSION</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-createeventtrigger.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createforeigndatawrapper.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE EVENT TRIGGER </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CREATE FOREIGN DATA WRAPPER</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1