User Agents

Web browsers and everyday web tech, explained

Drop Extension in PostgreSQL: How to Remove Extensions

· 4 min read · Extensions

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The DROP EXTENSION command in PostgreSQL removes an installed database extension and optionally its associated objects. This command is used to clean up unused or outdated extensions, freeing up system resources and simplifying database maintenance. Before running this command, ensure you understand its impact: it can remove functions, types, schemas, and other database objects that were created or managed by the extension. This guide walks through proper usage, common pitfalls, and how to troubleshoot issues.

What You'll Need

  • Access to a PostgreSQL database with appropriate privileges (typically CREATE or superuser role)
  • Knowledge of the extension name you want to remove
  • A database connection via psql, a GUI tool like pgAdmin, or another client
  • Backup of critical data if the extension manages important objects

Step-by-Step

  1. Connect to your PostgreSQL database
    Use psql or another client to log in. For example:
    psql -U username -d database_name
  2. List installed extensions
    Run this SQL command to see what extensions are currently active:
    \dx
    This returns a list of all installed extensions. Identify the exact name of the one you want to drop.
  3. Check extension dependencies
    Before dropping, verify what objects depend on the extension. Use:
    SELECT objtype, object_schema, object_name FROM pg_dependable_objects WHERE extname = 'extension_name';
    Replace extension_name with the actual extension. This helps avoid breaking dependent objects.
  4. Decide whether to cascade removal
    By default, DROP EXTENSION fails if other database objects depend on it. Decide whether to use the CASCADE option to automatically drop dependent objects, or resolve dependencies manually.
  5. Run the DROP EXTENSION command
    To remove the extension safely (without dropping unrelated objects), use:
    DROP EXTENSION extension_name;
    If dependencies exist and you accept their removal, use:
    DROP EXTENSION extension_name CASCADE;
    Replace extension_name with the actual name, such as postgis or hstore.
  6. Verify removal
    After execution, check that the extension is gone:
    \dx
    The extension should no longer appear in the list.
  7. Monitor for application impact
    If the extension was used by an application (e.g., PostGIS for spatial queries), test related functionality to ensure compatibility or adjust code accordingly.

Common Mistakes

  • Dropping without checking dependencies: Using DROP EXTENSION without CASCADE fails if objects depend on it; using CASCADE without review can delete critical functions or tables.
  • Typing the wrong extension name: Misspelling causes an error. Use \dx to confirm the exact spelling and version.
  • Running as a non-privileged user: Only users with CREATE privilege on the database or superuser status can drop extensions. Ensure your role has sufficient permissions.
  • Forgetting backups: Some extensions create custom types or functions that are hard to recreate. Always back up the schema before dropping.
  • Assuming DROP EXTENSION deletes data: It removes the extension’s schema and objects but does not delete regular table data unless explicitly tied to those objects.

Tips

  • Always test DROP EXTENSION in a development or staging environment before running in production.
  • Use \dx+ in psql to see detailed information about an extension, including its schema and description.
  • Consider renaming or disabling an extension temporarily instead of dropping it, especially if unsure.
  • Document which extensions are used in your application stack to avoid accidental removal.
  • Use version control for database schema changes, including extension management, to track modifications.

Troubleshooting

If DROP EXTENSION fails, the most common cause is dependent objects. PostgreSQL prevents removal unless you use CASCADE or resolve dependencies manually. If you get an error like cannot drop extension "xyz" because other objects depend on it, run:
SELECT pg_describe_object(depender.objid, depender.objsubid, 'pg_depend') FROM pg_depend depender WHERE depender.refobjid = (SELECT oid FROM pg_extension WHERE extname = 'extension_name')
This shows which objects are blocking the drop.

If you lack permissions, you’ll see a permission denied error. Contact your database administrator or switch to a superuser role.

If the command succeeds but expected objects remain, verify the extension name and check if multiple versions exist. Some extensions install into specific schemas—confirm the schema was also dropped if intended.

If application queries break afterward, check logs for missing functions or types. Reinstalling the extension may be necessary if removal was accidental.

Frequently Asked Questions

What does DROP EXTENSION do in PostgreSQL?

It removes a database extension and optionally its dependent objects, unregistering it from the database catalog.

Does DROP EXTENSION delete my data?

No, it does not delete regular table data. However, if the extension manages custom types or data stored in its schema, those may be removed.

Can I recover an extension after dropping it?

Yes, you can reinstall it using CREATE EXTENSION, provided it’s available in your PostgreSQL installation.

What is the difference between DROP EXTENSION and DROP SCHEMA?

DROP EXTENSION removes an extension and its tracked objects; DROP SCHEMA removes a schema and its contents but doesn’t unregister the extension from the catalog.

Why does DROP EXTENSION fail with a dependency error?

Other database objects (functions, types, tables) depend on the extension. Use CASCADE to remove them too, or drop dependencies manually first.

Can I drop an extension used by other databases?

No. Extensions are database-specific. Dropping in one database does not affect others, even on the same server.