PostGIS is a free tool for PostgreSQL that helps you work with geographic data. It allows you to store, manage, and analyze spatial data, which is useful for things like maps and location-based services. Installing PostGIS on your Ubuntu system can make your PostgreSQL database much more powerful.
In this guide, we will show you how to install and set up PostGIS on Ubuntu step by step. This guide is for everyone, no matter how much experience you have. We will start from updating your system and go all the way to creating a special database for spatial data and giving users the right permissions. By the end, you’ll be ready to use PostGIS for all your spatial data needs.
Prerequisites
- Ubuntu 22.04 or 20.04 installed on your system.
- A working PostgreSQL installation.
- Sudo access or root privileges.
Step 1: Update Your System
Before installing any new software, it’s a good practice to update your system packages to the latest available versions. Run the following commands to update your system:
sudo apt update
sudo apt upgrade
Step 2: Install PostgreSQL and PostGIS
Install PostgreSQL and PostGIS using the following commands:
sudo apt install postgresql-14-postgis-3
This command will install the PostgreSQL 14 and PostGIS 3 packages. If you are using an older version of PostgreSQL, replace ’14’ with the appropriate version number.
Step 3: Create a Spatially-Enabled Database
To create a spatially-enabled database, you’ll first need to create a new database and user. Run the following commands:
sudo su - postgres
createdb my_spatial_db
createuser my_spatial_user -P
Replace ‘my_spatial_db’ and ‘my_spatial_user’ with your desired database and user names, respectively. You’ll be prompted to set a password for the new user.
Next, enable PostGIS on the newly created database by running the following commands:
psql my_spatial_db -c "CREATE EXTENSION postgis;"
psql my_spatial_db -c "CREATE EXTENSION postgis_topology;"
Again, replace ‘my_spatial_db’ with the name of your database.
Step 4: Grant Permissions to the Spatial User
To grant permissions to the spatial user, run the following commands:
psql my_spatial_db -c "GRANT ALL PRIVILEGES ON DATABASE my_spatial_db TO my_spatial_user;"
psql my_spatial_db -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO my_spatial_user;"
psql my_spatial_db -c "GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO my_spatial_user;"
Replace ‘my_spatial_db’ and ‘my_spatial_user’ with your database and user names.
Step 5: Verify PostGIS Installation
To verify that PostGIS has been successfully installed and configured, run the following command:
psql my_spatial_db -c "SELECT PostGIS_version();"
You can expect to see output similar to the following:
Output:postgis_version --------------------------------------- 3.1.4 USE_GEOS=1 USE_PROJ=1 USE_STATS=1 (1 row)
This output shows that PostGIS version 3.1.4 is installed and enabled on the ‘my_spatial_db’ database. The exact version number may vary depending on the installed package. The additional information (USE_GEOS, USE_PROJ, USE_STATS) indicates the enabled dependencies and features within the PostGIS installation.
Conclusion
Great job! You have now installed and set up PostGIS on your Ubuntu Linux. With PostGIS in your PostgreSQL database, you can now store, manage, and query geographic data easily. This will help you create better applications, whether you’re working on maps, GIS applications, or location-based services.
As you use PostGIS more, you’ll find many useful features that make handling spatial data easier. Remember, the PostGIS community is active and always improving, so keep up with the latest updates and best practices to get the most out of this powerful tool.