PostgreSQL CREATE EXTENSION: How to Use It
In this article
The CREATE EXTENSION command in PostgreSQL loads a contributed extension (like pgcrypto, postgis, or uuid-ossp) into your database. It makes the extension's functions, data types, and operators available for use. You need superuser privileges or the database owner role to run it, and the extension's files must already be installed on your server (usually via your operating system's package manager).
What You'll Need
- Access to a PostgreSQL server (local or remote) with a client like
psqlor a GUI such as pgAdmin. - Superuser privileges or the
CREATEprivilege on the target database (often the database owner). - The extension package installed on the server. For example, on Debian/Ubuntu, install
postgresql-contrib; on Red Hat/CentOS,postgresql-contriborpostgresqlXX-contrib. Some extensions like PostGIS require separate packages.
Step-by-Step
- Connect to the database where you want the extension. Use
psql -d yourdbor connect via your GUI. You can also run\c yourdbif already in psql. - Check if the extension is available by listing available extensions:
SELECT * FROM pg_available_extensions;. Look for the name and whether it's installed. - Run the CREATE EXTENSION command with the extension name. For example:
CREATE EXTENSION pgcrypto;. This installs the extension into the current database. - Verify the installation by checking
\dxin psql or queryingSELECT * FROM pg_extension;. You should see the extension listed. - Use the extension – for example, after installing
pgcrypto, you can callgen_random_uuid()orcrypt()functions.
Common Mistakes
- Running as a non-privileged user: If you get
permission denied, you need superuser or database owner rights. - Wrong database: Extensions are per-database. If you install in one database, it's not automatically in another.
- Extension not installed on server: If you see
extension "foo" is not available, the package isn't installed on the server filesystem. - Using the wrong name: Some extensions have versions (e.g.,
postgisvspostgis_topology). Check the exact name inpg_available_extensions.
Tips
- Use
IF NOT EXISTSto avoid errors if the extension is already present:CREATE EXTENSION IF NOT EXISTS pgcrypto; - Specify a schema if you want to control where objects are created:
CREATE EXTENSION hstore SCHEMA public;(requires superuser for some extensions). - Use
WITH SCHEMAcarefully – some extensions must be in a specific schema (likepostgisoften goes topublic). - Update extensions with
ALTER EXTENSION ... UPDATEwhen new versions are available. - Check dependencies – some extensions require others (e.g.,
postgis_tiger_geocoderdepends onpostgis). Install dependencies first.
Troubleshooting
- "extension ... is not available" – Install the contrib package on the server. For example, on Ubuntu:
sudo apt install postgresql-contrib(or the version-specific package likepostgresql-16-contrib). Then restart PostgreSQL if needed. - "permission denied to create extension" – Log in as a superuser (e.g.,
postgres) or have the database owner run the command. You can also grant theCREATEprivilege on the database to a user. - "could not open extension control file" – The extension files aren't in the expected directory. This usually means the package isn't installed. Reinstall and ensure the PostgreSQL version matches.
- "function ... does not exist" – After installing, you might need to search_path to include the schema where the extension was installed. Use
SET search_path = public, extension_schema;or qualify calls with schema. - Version mismatch – If you have multiple PostgreSQL versions, ensure you installed the contrib package for the same version as your server.
Frequently Asked Questions
Can I use CREATE EXTENSION without superuser?
Yes, if you have the CREATE privilege on the database and the extension is marked as "trusted" (like pgcrypto). Untrusted extensions (e.g., postgis) require superuser.
How do I remove an extension?
Use DROP EXTENSION extension_name;. Be aware that this drops all objects created by the extension.
Can I install an extension in multiple databases?
Yes, you must run CREATE EXTENSION separately in each database where you need it. There's no global install.
What does CREATE EXTENSION actually do?
It reads the extension's control file and SQL scripts, then runs them to create functions, types, and other objects in the current database. It also records the extension in the pg_extension catalog.
How do I see which extensions are installed?
Use \dx in psql or query SELECT * FROM pg_extension;.
Can I update an extension to a newer version?
Yes, use ALTER EXTENSION extension_name UPDATE TO 'new_version';. Check available versions with SELECT * FROM pg_available_extension_versions WHERE name = 'extension_name';.