如何在CentOS 7上安装PostGIS

时间:2020-02-23 14:32:10  来源:igfitidea点击:

如何在CentOS 7上安装PostGIS?本教程将引导我们完成在CentOS 7上安装PostGIS的步骤。PostGIS是PostgreSQL数据库扩展,它允许将GIS(地理信息系统)对象存储在数据库中。

PostGIS包括对基于GiST的R-Tree空间索引的支持,以及对GIS对象进行分析和处理的功能。它是根据知识共享署名-相同方式共享3.0许可获得许可的开源软件。

对于CentOS 8,使用:如何在CentOS 8上安装PostGIS

安装PostgreSQL数据库服务器

PostgreSQL是usinf PostGIS功能的依赖项。使用下面的教程将其安装在CentOS 7上。

如何在CentOS 7上安装PostgreSQL 11

如何在CentOS 7上安装PostgreSQL 12

将EPEL储存库添加到CentOS 7

EPEL存储库上有许多依赖项。运行以下命令以在CentOS 7计算机上安装epel存储库。

sudo yum -y install epel-release

在CentOS 7上安装PostGIS

安装PostgreSQL并添加EPEL存储库后,继续从添加的PostgreSQL安装PostGIS。使用以下命令添加了存储库。

PostgreSQL 12上的PostGIS:

sudo yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install postgis25_12

PostgreSQL 11上的PostGIS

sudo yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install postgis25_11

检查PostgreSQL版本的PostGIS v2.5版本。可以使用以下方法检查安装的版本:

$rpm -qi postgis25_11
 Name        : postgis25_11
 Version     : 2.5.1
 Release     : 4.rhel7
 Architecture: x86_64
 Install Date: Wed 20 Nov 2019 10:37:35 PM CET
 Group       : Applications/Databases
 Size        : 32649231
 License     : GPLv2+
 Signature   : DSA/SHA1, Wed 02 Jan 2019 12:45:12 PM CET, Key ID 1f16d2e1442df0f8
 Source RPM  : postgis25_11-2.5.1-4.rhel7.src.rpm
 Build Date  : Wed 02 Jan 2019 12:44:54 PM CET
 Build Host  : koji-centos7-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.

启用PostGIS Spatial功能

我们需要先在数据库上激活PostGIS功能,然后才能存储空间数据。下面的示例将向我们展示如何创建数据库和激活空间功能。

1.切换到postgres用户。

sudo -i -u postgres

2.创建测试用户/数据库。

-bash-4.2$createuser test_user
-bash-4.2$createdb test_db -O test_user

3.连接到测试数据库:

-bash-4.2$psql -d test_db
 psql (11.2)
 Type "help" for help.

4.在数据库上启用PostGIS扩展:

test_db=# CREATE EXTENSION postgis;
 CREATE EXTENSION

5.验证

test_db=# SELECT PostGIS_version();
             postgis_version            
 2.5 USE_GEOS=1 USE_PROJ=1 USE_STATS=1
 (1 row)
test_db=# \q

有关用法教程和高级配置,请参阅PostGIS官方文档。