Extensions (PostGIS, uuid-ossp)
PostgreSQL has a built-in extension system for adding optional functionality — new data types, functions, operators, and index types — without modifying the core server. An extension is packaged code that you opt into per-database, and just as easily remove.
Installing and Listing Extensions
managing-extensions.sql
-- Install an extension into the current database CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; -- Remove it DROP EXTENSION IF EXISTS "uuid-ossp"; -- See which extensions are currently installed in this database SELECT * FROM pg_extension; -- See which extensions are AVAILABLE to install (bundled with this server) SELECT * FROM pg_available_extensions;
In psql, the shortcut for listing installed extensions is \dx. Many extensions are bundled with a standard PostgreSQL installation (via the contrib package on most Linux distributions) but not enabled until you run CREATE EXTENSION; others, like PostGIS, are separate installs that add themselves to pg_available_extensions once present on the server.
Notable Extensions
Extension | What It Adds |
|---|---|
postgis | Geospatial data types and functions — points, polygons, distance/containment queries, spatial indexes. One of the most capable open-source GIS systems in existence, not just a PostgreSQL add-on. |
uuid-ossp | Functions for generating UUIDs ( |
pgcrypto | Cryptographic functions — hashing, symmetric/asymmetric encryption, and password hashing helpers usable directly in SQL. |
pg_stat_statements | Tracks execution statistics for every distinct query the server runs — call counts, total/average time, rows returned. Essential for finding slow queries in production. |
postgis-example.sql
CREATE EXTENSION IF NOT EXISTS postgis;
CREATE TABLE stores (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
location GEOGRAPHY(POINT)
);
INSERT INTO stores (name, location)
VALUES ('Downtown', ST_MakePoint(-73.9857, 40.7484));
-- Find stores within 5km of a given point
SELECT name FROM stores
WHERE ST_DWithin(location, ST_MakePoint(-73.98, 40.75)::geography, 5000);