In PostgreSQL, the keyword EXTENSION is used inside the commands CREATE EXTENSION, ALTER EXTENSION, and DROP EXTENSION to install, upgrade, or remove a self-contained bundle of SQL objects. An extension encapsulates database functionality (for example, PostGIS, pgcrypto, or hstore) so that it can be versioned, upgraded, and removed as a single unit. When you run CREATE EXTENSION, PostgreSQL looks for a corresponding control file in the shared extension directory, executes the associated SQL script, and records the installed version in pg_extension. ALTER EXTENSION handles upgrades or configuration changes, while DROP EXTENSION removes all objects that belong to the extension in a single transaction. Extensions improve maintainability by isolating vendor-supplied code from user code, enable easier migrations, and support dependency management. The keyword is not part of the ANSI SQL standard and is mainly available in PostgreSQL-compatible systems. Attempting to use it in MySQL, SQL Server, Oracle, or SQLite will produce syntax errors.
extension_name
(text) - Name of the extension to manageIF NOT EXISTS / IF EXISTS
(clause) - Optional guard to avoid errors when the extension already exists or is absentSCHEMA
(identifier) - Target schema in which to install the extension objectsVERSION
(text) - Specific version to install or update to; omit for defaultCASCADE
(clause) - Automatically drops objects that depend on the extension when removingCREATE EXTENSION, DROP EXTENSION, ALTER EXTENSION, pg_extension catalog, PL/pgSQL, SCHEMA
PostgreSQL 9.1 (2011)
You must either be a superuser or have the CREATE privilege on the current database plus USAGE on the target schema.
Yes. Use the WITH SCHEMA clause in CREATE EXTENSION to place all objects inside that schema, keeping the extension isolated from public.
Query the catalog view pg_extension or run `\dx` in psql to see name, version, and schema for each installed extension.
No. EXTENSION is PostgreSQL specific. Other RDBMS products implement similar functionality through different mechanisms or not at all.