阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

RedHat7下源码安装PostGIS

151次阅读
没有评论

共计 3593 个字符,预计需要花费 9 分钟才能阅读完成。

本文介绍在 RedHat7 环境下安装使用 PostGIS 的流程。

1. PostgreSQL

1.1 yum 安装 PostgreSQL

这个比较简单,直接使用 yum 安装即可。

$ sudo yum install -y postgresql-server postgresql-devel libxml2 libxml2-devel

顺便安装 postgresql-devel、libxml2-devel,后边编译安装 PostGIS 会用到。

postgresql.x86_64               9.2.13-1.1
postgresql-devel.x86_64         9.2.13-1.1
postgresql-libs.x86_64          9.2.13-1.1
postgresql-server.x86_64        9.2.13-1.1
libxml2                         2.9.1-6
libxml2-devel.x86_64            2.9.1-6

然后切换到 postgres 账户。

$ sudo su postgres
postgres $ 

1.2 初始化 PostgreSQL

确认 PostgreSQL 数据目录。

postgres $ cat /var/lib/pgsql/.bash_profile
[-f /etc/profile ] && source /etc/profile

PGDATA=/var/lib/pgsql/data
export PGDATA

执行初始化操作。

postgres $ initdb

目录 /var/lib/pgsql/data 下存储了 PostgreSQL 的所有数据文件和配置。

1.3 启动 PostgreSQL

使用 pg_ctl 启动 PostgreSQL。

postgres $ pg_ctl start

使用 psql 客户端连接。

postgres $ psql
psql (9.2.13)
输入 "help" 来获取帮助信息.

postgres=# \l
                                         资料库列表
       名称       |  拥有者  | 字元编码 |  校对规则   |    Ctype    |       存取权限        
------------------+----------+----------+-------------+-------------+-----------------------
 postgres         | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 template0        | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
                  |          |          |             |             | postgres=CTc/postgres
 template1        | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
                  |          |          |             |             | postgres=CTc/postgres

2. PostGIS

2.1 准备源码包

准备 gdal、proj、geos 和 postgis 的源码包,postgis 版本注意和 postgresql 保持兼容。

兼容信息可以查看: http://trac.osgeo.org/postgis/wiki/UsersWikiPostgreSQLPostGIS

$ wget http://download.osgeo.org/gdal/2.2.3/gdal-2.2.3.tar.gz
$ wget http://download.osgeo.org/proj/proj-4.8.0.tar.gz
$ wget http://download.osgeo.org/geos/geos-3.3.3.tar.bz2
$ wget http://download.osgeo.org/postgis/source/postgis-2.2.6.tar.gz

2.2 解压编译安装 gdal、proj、geos 和 postgis

依次解压、编译、安装以上软件包。

$ tar xf gdal-2.2.3.tar.gz && cd gdal-2.2.3 && ./configure --prefix=/usr/local/gdal && make && sudo make install
$ tar xf proj-4.8.0.tar.gz && cd proj-4.8.0 && ./configure --prefix=/usr/local/proj && make && sudo make install
$ tar xf geos-3.3.3.tar.bz2 && cd geos-3.3.3 && ./configure --prefix=/usr/local/geos && make && sudo make install
$ tar xf postgis-2.2.6.tar.gz && cd postgis-2.2.6 && ./configure -prefix=/usr/local/postgis --with-geosconfig=/usr/local/geos/bin/geos-config --with-projdir=/usr/local/proj --with-gdalconfig=/usr/local/gdal/bin/gdal-config && make && sudo make install

2.3 配置 ldconfig

将 gdal、proj、geos 的 lib 目录添加到 ldconfig。

$ sudo cat /etc/ld.so.conf
include ld.so.conf.d/*.conf

/usr/local/gdal/lib/
/usr/local/proj/lib/
/usr/local/geos/lib/

$ sudo ldconfig

2.4 创建空间数据库模板

# 创建无空间特性数据库
postgres $ createdb template_postgis
# 创建相关空间数据库相关的函数,类型,操作符等
postgres $ psql -f /usr/share/pgsql/contrib/postgis-2.2/postgis.sql -d template_postgis
postgres $ psql -f /usr/share/pgsql/contrib/postgis-2.2/rtpostgis.sql -d template_postgis
# 验证空间数据库版本
postgres $ psql template_postgis
psql (9.2.13)
输入 “help” 来获取帮助信息。
template_postgis=# select postgis_full_version();
                                                          postgis_full_version                                                           
———————————————————————————————————————————————
 POSTGIS=”2.2.6 r16006″ GEOS=”3.3.3-CAPI-1.7.4″ PROJ=”Rel. 4.8.0, 6 March 2012″ GDAL=”GDAL 2.2.3, released 2017/11/20″ LIBXML=”2.9.1″RASTER
(1 行记录)
template_postgis=# \d

                    关联列表
架构模式  |        名称        |  型别  |  拥有者 
———-+——————-+——–+———-
public  | geography_columns | 视观表 | postgres
public  | geometry_columns  | 视观表 | postgres
public  | raster_columns    | 视观表 | postgres
public  | raster_overviews  | 视观表 | postgres
public  | spatial_ref_sys  | 资料表 | postgres
(5 行记录)

2.5 根据空间数据库模板创建新的空间数据库

postgres $ createdb -T template_postgis new_database

3. 简单测试

测试点 (0, 0) 是否在指定的多边形内。

new_database=# select ST_Within(ST_GeomFromText('POINT(0 0)', 4326), ST_GeomFromText('POLYGON((1 1, 1 -1, -1 -1, -1 1, 1 1))', 4326)) ;
 st_within 
-----------
 t
(1 行记录)

详细语法规则可以参考 PostGis 使用手册:http://www.postgres.cn/docs/PostGis-2.2.0dev_Manual.pdf

本文永久更新链接地址:http://www.linuxidc.com/Linux/2018-01/150298.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-22发表,共计3593字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中