Ensure you have created or connected to the target database where you want to enable PostGIS. For example, a database named gis_portfolio.
Open the Query Tool for your database in pgAdmin and run the following SQL commands:
CREATE EXTENSION IF NOT EXISTS postgis;
CREATE EXTENSION IF NOT EXISTS postgis_topology;
If successful, you should see a message like:
Query returned successfully in ...
To confirm PostGIS is enabled, run this query:
SELECT PostGIS_Full_Version();
Expected output format:
POSTGIS="3.x.x" [Build info] GEOS="..." PROJ="..."
This step confirms spatial functionality by creating a table with geometry data.
CREATE TABLE test_locations (
id SERIAL PRIMARY KEY,
name TEXT,
geom GEOMETRY(Point, 4326)
);
Add a spatial point for Timau, Kenya:
INSERT INTO test_locations (name, geom)
VALUES ('Timau', ST_SetSRID(ST_MakePoint(37.0, -0.083), 4326));
Query the table with spatial output:
SELECT name, ST_AsText(geom) FROM test_locations;
CREATE EXTENSION FailsError Example:
ERROR: could not open extension control file "postgis.control"
This means PostGIS is not installed on your PostgreSQL server.
You need to install it first. For example, on Ubuntu:
sudo apt update
sudo apt install postgis postgresql-14-postgis-3
Check your PostgreSQL version and adjust the package name accordingly.
ST_Within, ST_Intersects, and ST_Buffer to perform advanced geospatial queries.