Comment on page
CREATE EXTENSION — install an extension
CREATE EXTENSION [ IF NOT EXISTS ] extension_name
[ WITH ] [ SCHEMA schema_name ]
[ VERSION version ]
[ CASCADE ]
CREATE EXTENSIONloads a new extension into the current database. There must not be an extension of the same name already loaded.
Loading an extension essentially amounts to running the extension's script file. The script will typically create new SQL objects such as functions, data types, operators and index support methods.
CREATE EXTENSIONadditionally records the identities of all the created objects, so that they can be dropped again if
DROP EXTENSIONis issued.
The user who runs
CREATE EXTENSIONbecomes 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.
Loading 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 trusted in its control file, then it can be installed by any user who has
CREATEprivilege 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.
IF NOT EXISTS
Do 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.
The name of the extension to be installed. PostgreSQL will create the extension using details from the file
The 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.
If the extension specifies a
schemaparameter in its control file, then that schema cannot be overridden with a
SCHEMAclause. Normally, an error will be raised if a
SCHEMAclause is given and it conflicts with the extension's
schemaparameter. However, if the
CASCADEclause is also given, then
schema_nameis ignored when it conflicts. The given
schema_namewill be used for installation of any needed extensions that do not specify
schemain their control files.
Remember 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.
The 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.
Automatically install any extensions that this extension depends on that are not already installed. Their dependencies are likewise automatically installed, recursively. The
SCHEMAclause, 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.
Before you can use
CREATE EXTENSIONto load an extension into a database, the extension's supporting files must be installed. Information about installing the extensions supplied with PostgreSQL can be found in Additional Supplied Modules.
以超級使用者身份安裝擴展需要信任擴展的作者以安全的方式編寫擴展安裝腳本。對於惡意用戶來說，創建特洛伊木馬物件並不是非常困難，這些對象將損害以後粗心編寫的擴展腳本的執行，從而允許該用戶獲得超級用戶許可權。但是，特洛伊木馬物件僅在腳本執行期間位於search_path中時才是危險的，這意味著它們位於擴展的安裝目標架構中或它所依賴的某個擴展的架構中。因此，在處理其腳本未經仔細審查的擴展時，一個好的經驗法則是僅將它們安裝到尚未授予任何不受信任的使用者的 CREATE 許可權的架構中。對於它們所依賴的任何擴展也是如此。
將 hstore 延伸功能安裝到目前的資料庫中，將其物件置於 SCHEMA addons 中：
CREATE EXTENSION hstore SCHEMA addons;
SET search_path = addons;
CREATE EXTENSION hstore;
CREATE EXTENSION 是 PostgreSQL 的延伸功能。