PostgreSQL Extensions: Install & Use Guide
In this article
PostgreSQL extensions add features like new data types, functions, and performance tools directly to your database. This guide walks you through finding, installing, enabling, and using extensions, plus how to troubleshoot common issues. You'll need PostgreSQL installed and access to a database with superuser or owner privileges.
What You'll Need
- A running PostgreSQL server (version 9.1 or later, though newer extensions may require newer versions).
- Command-line access (psql or another client) or a GUI like pgAdmin.
- Privileges to create extensions in the target database (usually superuser or database owner).
- For some extensions, the operating system package (e.g.,
postgresql-contrib) may need to be installed separately.
Step-by-Step
- Check what's available. Connect to your database with psql and run:
This lists all extensions your server knows about, along with default versions and descriptions. If the one you want isn't listed, you may need to install its OS package first.SELECT * FROM pg_available_extensions; - Install the OS package (if needed). For extensions that come with PostgreSQL (like
pgcryptoorhstore), install thecontribpackage. On Debian/Ubuntu:sudo apt install postgresql-contrib. On RHEL/CentOS:sudo yum install postgresql-contrib. For third-party extensions, follow their specific installation instructions (often viamakeandmake install). - Enable the extension in your database. Extensions are per-database. Connect to the target database (e.g.,
\c mydb) and run:
For example:CREATE EXTENSION IF NOT EXISTS extension_name;CREATE EXTENSION pgcrypto;This creates the extension's objects in your database. - Verify the installation. Run
\dxin psql to list installed extensions. You should see your extension and its version. You can also check with:SELECT * FROM pg_extension; - Use the extension. The functions and types are now available. For example, with
pgcrypto, you can hash a password:
WithSELECT crypt('mypassword', gen_salt('bf'));hstore, you can store key-value pairs:
Refer to the extension's documentation for its specific features.SELECT 'a=>1, b=>2'::hstore; - Update the extension (when needed). If a new version is available, run:
This applies upgrade scripts. Always back up your database before upgrading extensions.ALTER EXTENSION extension_name UPDATE; - Drop the extension (if no longer needed). To remove it from a database:
This removes all objects created by the extension. Use with caution.DROP EXTENSION extension_name;
Common Mistakes
- Creating the extension in the wrong database. Extensions are not global; you must run
CREATE EXTENSIONin each database that needs it. - Forgetting to install the OS package. If
pg_available_extensionsdoesn't list your desired extension, the package is likely missing. - Using an extension that requires a higher PostgreSQL version. Check the extension's requirements against your server version.
- Assuming all extensions are bundled. Many popular extensions (like PostGIS) are separate projects and need individual installation.
- Ignoring schema issues. Some extensions install into a specific schema. If you use a custom schema, you might need to specify it in
CREATE EXTENSIONwithSCHEMA schema_name.
Tips
- Use
CREATE EXTENSION IF NOT EXISTSto avoid errors when re-running scripts. - Check the extension's documentation for any required configuration parameters (e.g.,
shared_preload_librariesinpostgresql.conf). Some extensions likepg_stat_statementsneed a server restart after setting that parameter. - For development, you can create a separate database to test extensions before applying them to production.
- Use
\dx+ extension_namein psql to see detailed information about the extension's objects. - Keep your PostgreSQL version updated; newer extensions often require newer server features.
Troubleshooting
- "Extension "name" is not available" – The extension's files are not installed. Install the OS package or build from source. After installing, restart PostgreSQL if necessary.
- "Permission denied to create extension" – You need superuser or database owner privileges. Ask your DBA to grant you
CREATEon the database or run the command as a superuser. - "Extension "name" has no installation script" – This usually means the extension's control file is missing or the version is incompatible. Reinstall the extension package.
- "Function does not exist" when using extension functions – The extension may not be installed in the current database or schema. Check
\dxand yoursearch_path. - PostgreSQL won't start after installing an extension – Some extensions require adding a library to
shared_preload_librariesinpostgresql.conf. If you added it, ensure the library path is correct and the file exists. Remove the line to recover. - Upgrade fails – If
ALTER EXTENSION UPDATEfails, you may need to manually run upgrade scripts or reinstall the extension. Back up your data first.
Frequently Asked Questions
Do extensions work across all databases?
No. Each database must have the extension created separately. However, you can create the extension in the template1 database so new databases inherit it.
Can I use multiple extensions together?
Yes, you can create as many extensions as you need in the same database, as long as they don't conflict. Some extensions may depend on others, which PostgreSQL handles automatically.
How do I find the exact version of an installed extension?
Run \dx or SELECT extversion FROM pg_extension WHERE extname = 'name';
Do I need to restart PostgreSQL after creating an extension?
Usually no. Most extensions are loaded on demand. However, extensions that require a shared library (like pg_stat_statements) may need a restart if you add them to shared_preload_libraries.
Can I drop an extension and then recreate it?
Yes, but dropping removes all objects and data associated with the extension. Make sure you have a backup or export any needed data first.
Are extensions safe to use in production?
Many are, but always test in a staging environment first. Check the extension's maturity, maintenance status, and community feedback. Some extensions are experimental or have security implications.