PostGIS is a free and open-source database extender for the PostgreSQL Database Management System. It enhances PostgreSQL by adding additional functions such as area, union, intersection, distance, and data types, all of which enable the execution of location queries within SQL. With PostGIS, storing polygon and point data types within PostgreSQL becomes seamless.
In this comprehensive tutorial, we will guide you through the installation of PostGIS with PostgreSQL on CentOS 8.
Prerequisites
- A server running CentOS 8.
- A root password configured on your server.
Getting Started
Before you begin, it is necessary to install the PostGIS and EPEL repositories on your system. You can achieve this with the following command:
dnf -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
Subsequently, enable the Powertools repository and disable the default PostgreSQL repository using the following command:
dnf config-manager --set-enabled PowerTools dnf -qy module disable postgresql
Once you have completed these steps, you can proceed to the next stage.
Install PostGIS
To install PostGIS, execute the following command:
dnf install postgis25_12
After the installation process is finalized, you can verify the installed PostGIS package with the following command:
rpm -qi postgis25_12
You should see output similar to the following:
Name : postgis25_12 Version : 2.5.5 Release : 2.rhel8 Architecture: x86_64 Install Date: Monday 01 February 2021 11:59:37 PM EST Group : Unspecified Size : 29832534 License : GPLv2+ Signature : DSA/SHA1, Tuesday 10 November 2020 01:36:47 PM EST, Key ID 1f16d2e1442df0f8 Source RPM : postgis25_12-2.5.5-2.rhel8.src.rpm Build Date : Tuesday 10 November 2020 01:30:09 PM EST Build Host : koji-rhel8-x86-64-pgbuild Relocations : (not relocatable) Vendor : PostgreSQL Global Development Group URL : http://www.postgis.net/ Summary : Geographic Information Systems Extensions to PostgreSQL Description : PostGIS adds support for geographic objects to the PostgreSQL object-relational database. In effect, PostGIS "spatially enables" the PostgreSQL server, allowing it to be used as a backend spatial database for geographic information systems (GIS), much like ESRI's SDE or Oracle's Spatial extension. PostGIS follows the OpenGIS "Simple Features Specification for SQL" and has been certified as compliant with the "Types and Functions" profile.
Next, initialize the PostgreSQL database with the following command:
/usr/pgsql-12/bin/postgresql-12-setup initdb
Then, start the PostgreSQL service and configure it to start automatically at system reboot with the following command:
systemctl start postgresql-12.service systemctl enable postgresql-12.service
Create an Extension
At this point, both PostgreSQL and PostGIS have been installed. Now, let’s create an extension for PostGIS.
First, log in as the Postgres user using the following command:
su - postgres
Then, create a PostgreSQL user and database with the following commands:
createuser test_usr createdb test_postgis -O test_usr
Connect to the database with the following command:
psql -d test_postgis
You should see the following output:
psql (12.5) Type "help" for help.
Create a PostGIS extension using the command below:
CREATE EXTENSION postgis;
To verify the PostGIS version, use the following command:
select PostGIS_Full_Version();
The expected output should display the PostGIS version:
postgis_full_version --------------------------------------------------------------------------------------------------------------------------------------- POSTGIS="2.5.5" [EXTENSION] PGSQL="120" GEOS="3.8.1-CAPI-1.13.3" PROJ="Rel. 7.2.1, January 1st, 2021" GDAL="GDAL 3.2.1, released 2020/12/29" LIBXML="2.9.7" LIBJSON="0.13.1" LIBPROTOBUF="1.3.0" RASTER (1 row)
Finally, exit the Postgres shell with the following commands:
exit exit
Conclusion
Through this guide, you have successfully learned how to install PostGIS with PostgreSQL on CentOS 8. You are now ready to enhance your database with geometric data capabilities using PostGIS.
Frequently Asked Questions (FAQ)
Q: What is PostGIS?
A: PostGIS is an extension for the PostgreSQL database that adds support for geographic objects and enables spatial queries.
Q: Can I install PostGIS on operating systems other than CentOS 8?
A: Yes, PostGIS can be installed on a variety of operating systems such as Ubuntu, Debian, and other Linux distributions, as well as Windows and macOS.
Q: Is PostGIS difficult to learn?
A: While familiarity with SQL and database management is beneficial, PostGIS is designed to be an accessible and powerful tool for adding geographic information system (GIS) capabilities to PostgreSQL. Many learning resources are available to help you get started.
Q: What are the benefits of using PostGIS?
A: PostGIS allows spatial queries and supports geographic data types, enabling complex analyses and mappings for GIS applications. It transforms PostgreSQL into a feature-rich spatial database.