centos7下编译安装postgis3

本文认为已安装PostgreSQL12,安装步骤如 Centos7安装PostgreSQL,最好按照前文先把pg安装好,否则,在postgis,pgrouting安装时,指定pg的安装目录,直接抄路径应该不对,读者要指向自己的安装位置等。

安装前准备

升级cmake

CGAL4-11。因为4-11需要CMake3.11以上

1
2
cmake -version
# cmake version 2.8.12.2
1
2
3
4
5
6
wget https://github.com/Kitware/CMake/releases/download/v3.16.8/cmake-3.16.8.tar.gz
tar -zxvf cmake-3.16.8.tar.gz
cd cmake-3.16.8
./bootstrap --prefix=/usr/local/cmake
gamke
gmake install

postgis还要安装其他依赖,比如GEOS,proj,GDAL,各版本需要的依赖版本,http://postgis.net/news/ ,如图。

本文基于GEOS 3.8.1,PROJ 6.3.2,GDAL 3.0.4,json-c 0.13.1,CGAL4.14.3,SFCGAL1.3.7,protobuf3.11.4,protobuf-c1.3.3,libxml2-2.9.10,pcre-8.44,PostGIS-3.0.1,pgrouting3.0.2安装。

安装依赖或插件

安装GEOS-3.8.1

1
2
3
4
5
wget https://download.osgeo.org/geos/geos-3.8.1.tar.bz2
tar -jxf geos-3.8.1.tar.bz2
cd geos-3.8.1
./configure --prefix=/usr/local/geos-3.8.1
make && make install

:blush:

安装proj-6.3.2

1
2
3
4
5
6
wget http://download.osgeo.org/proj/proj-6.3.2.tar.gz
tar -zxvf proj-6.3.2.tar.gz
cd proj-6.3.2
./configure --prefix=/usr/local/proj-6.3.2
# 编译时遇到下面的问题,说是sqlite版本太低!编译成功再进行下一步!
make && make install

遇到如图片的问题!

然后我在命令行输入sqlite3的时候,结果是3.22,哪来的3.7.17?

https://stackoverflow.com/questions/62154342/configure-error-package-requirements-sqlite3-3-7-4-were-not-met

原来是sqlite-devel的版本,但我查询之后是最新的!!于是我删了sqlite-devl,得到的是Checking for module 'sqlite3' No package 'sqlite3' found

暴力升级也没有用,不得不看日志的下半部分。于是搜索关键词PKG_CONFIG_PATH。找到https://my.oschina.net/zzop/blog/499908这篇文章,其中说到:

:point_right:

cp /usr/local/lib/pkgconfig/sqlite3.pc /usr/lib64/pkgconfig/

重新编译就好了!

安装GDAL

1
2
3
4
5
6
7
8
wget https://download.osgeo.org/gdal/3.0.4/gdal-3.0.4.tar.gz
tar -zxvf gdal-3.0.4.tar.gz
cd gdal-3.0.4
# 带pg的配置,具体需要啥可以./configure --help 查看
./configure --prefix=/usr/local/gdal-3.0.4 --with-pg=yes
make
# 编译时遇到下面的问题,成功则进行下一步!
make install

遇到

1
2
3
collect2: error: ld returned 1 exit status
make[1]: *** [GNUmakefile:82: gdalinfo] Error 1
make: *** [GNUmakefile:112: apps-target] Error 2

解决办法可能是make clean,紧接着按之前的编译安装!

参考文章:https://stackoverflow.com/questions/60218227/trying-to-install-gdal-3-0-4-on-red-hat-8

我的编译配置:

1
./configure --prefix=/usr/local/gdal-3.0.4 --with-proj=/usr/local/proj-6.3.2 --with-geos=/usr/local/geos-3.8.1/bin/geos-config --with-sqlite3=/usr/local/bin/sqlite3 --with-libjson-c=/usr/local/json-c-0.13.1 --with-pg=yes --with-python=/root/.virtualenvs/aigisss_py/bin/python3.6 

安装protubuf

1
2
3
tar -xzvf  protobuf-cpp-3.11.4.tar.gz
./configure --prefix=/usr/local/protobuf-3.11.4
make && make install

竟然说bash: ./configure: No such file or directory

解决方案

1
2
yum install automake
autoreconf -i

参考文章:https://stackoverflow.com/questions/24054761/configure-gives-error-in-ubuntu

出现编译错误

1
2
3
make[2]: *** [message.lo] Error 1 
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2

解决方案下载含有all的资源包

参考文章:https://github.com/protocolbuffers/protobuf/issues/6599

安装protobuf-c

1
2
3
4
5
6
wget https://github.com/protobuf-c/protobuf-c/releases/download/v1.3.3/protobuf-c-1.3.3.tar.gz
tar -xzvf protobuf-c-1.3.3.tar.gz
cd protobuf-c-1.3.3
export PKG_CONFIG_PATH=/usr/local/protobuf-3.11.4/lib/pkgconfig
./configure --prefix=/usr/local/protobuf-c-1.3.3
make && make install

安装SFCGAL 1.3.7

由于SFCGAL需要依赖Boost、CGAL、GMP、MPFR这四个软件,所以具体总共需要安装以下四个软件:

  1. boost-devel.x86_64

  2. gmp-devel.x86_64

  3. mpfr-devel.x86_64

  4. CGAL-4.14

    为了安装pgrouting3.0.2需要安装boost1.53以上,使用yum install boost boost-devel只能安装版本1.53, 我使用源码安装的是1.68

    1
    2
    3
    4
    5
    wget https://dl.bintray.com/boostorg/release/1.68.0/source/boost_1_68_0.tar.gz
    tar -xzvf boost_1_68_0.tar.gz
    cd boost_1_68_0
    ./bootstrap.sh
    ./b2 install --with=all
    1
    2
    yum install gmp-devel.x86_64
    yum install mpfr-devel.x86_64

安装CGAL

1
2
3
4
5
6
7
wget http://distfiles.macports.org/cgal/cgal-4.14.3.tar.xz
xz -d CGAL-4.14.3.tar.xz
tar -xvf CGAL-4.14.3.tar
cd CGAL-4.14.3
mkdir build && cd build
cmake ..
make && make install

安装pcre

1
2
3
4
5
6
7
wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
tar -xzvf pcre-8.44.tar.gz
cd pcre-8.44
./configure --enable-utf8 --prefix=/usr/local/pcre-8.44
make && make intall
echo "/usr/local/pcre/lib" > /etc/ld.so.conf.d/pcre-8.44.conf
ldconfig

安装SFCGAL

遇到以下这个问题:

1
2
c++: internal compiler error: Killed (program cc1plus)
Please submit a full bug report

解决方案:

1
2
3
4
5
6
7
8
sudo dd if=/dev/zero of=/swapfile bs=64M count=16
#count的大小就是增加的swap空间的大小,64M是块大小,所以空间大小是bs*count=1024MB
sudo mkswap /swapfile
#把刚才空间格式化成swap格式
chmod 0600 /swapfile
#该目录权限,不改的话,在下一步启动时会报“swapon: /swapfile: insecure permissions 0644, 0600 suggested.”错误
sudo swapon /swapfile
#使用刚才创建的swap空间

安装编译:

1
2
3
4
5
6
wget  https://github.com/Oslandia/SFCGAL/archive/v1.3.7.tar.gz
tar -zxvf SFCGAL-1.3.7.tar.gz
cd SFCGAL-1.3.7
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/sfcgal-1.3.7 ..
make && make install

装完之后释放空间:

1
2
3
swapoff -a
#详细的用法可以:swapoff --help
#查看当前内存使用情况:free -m

参考文章:https://blog.csdn.net/qq_27148893/article/details/88936044

安装PostGIS

1
./configure --prefix=/usr/local/postgis-3.0.1 --with-gdalconfig=/usr/local/gdal-3.0.4/bin/gdal-config --with-pgconfig=/opt/pg12/bin/pg_config --with-geosconfig=/usr/local/geos-3.8.1/bin/geos-config --with-projdir=/usr/local/proj-6.3.2 --with-xml2config=/usr/local/libxml2-2.9.10/bin/xml2-config --with-jsondir=/usr/local/json-c-0.13.1 --with-protobufdir=/usr/local/protobuf-c-1.3.3 --with-sfcgal=/usr/local/sfcgal-1.3.7/bin/sfcgal-config --with-pcredir=/usr/local/pcre-8.44
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
PG_HOME=/opt/pg12

LD_LIBRARY_PATH=$PG_HOME/lib:$LD_LIBRARY_PATH
PATH=$PG_HOME/bin:$PATH

PKG_CONFIG_PATH=$PG_HOME/lib/pkgconfig:$PKG_CONFIG_PATH

export CMAKE_HOME=/usr/bin/cmake
export PROTOBUF_HOME=/usr/local/protobuf-3.11.4

GDAL_HOME=/usr/local/gdal-3.0.4
GDAL_DATA=$GDAL_HOME/share/gdal
LD_LIBRARY_PATH=$GDAL_HOME/lib:/usr/local/lib64:$JRE_HOME/lib:$LD_LIBRARY_PATH
PATH=$GDAL_HOME/bin:$PATH

export PATH=$CMAKE_HOME/bin:$PROTOBUF_HOME/bin:/usr/local/protobuf-c-1.3.3/bin:$PATH
export PKG_CONFIG_PATH LD_LIBRARY_PATH
export LD_LIBRARY_PATH GDAL_DATA

创建postgis扩展

1
2
3
4
5
6
7
su - postgres
psql
create database gistest;
\c gistest
create extension postgis;
#如果安装了sfcgal,创建扩展测试下
create extension postgis_sfcgal;

安装PgRouting

1
2
3
4
5
6
7
8
9
wget  https://github.com/pgRouting/pgrouting/releases/download/v3.0.2/pgrouting-3.0.2.tar.gz
tar -zxvf pgrouting-3.0.2.tar.gz
cd pgrouting-3.0.2
mkdir build && cd build
#引入postgres的环境变量
source /home/postgres/.bashrc
cmake ..
make
make install

验证安装:

1
2
3
4
5
su - postgres
psql
create database gistest;
\c gistest
create extension pgrouting;

验证:sql查询

1
SELECT ST_AsX3D(ST_Extrude(ST_Buffer(ST_GeomFromText('POINT(100 90)'), 50, 'quad_segs=2'),0,0,30));

以上就完成安装了!!

参考文章:


centos7下编译安装postgis3
https://www.gishai.top/blog/posts/a70e035f.html
作者
Hai
发布于
2020年6月5日
更新于
2023年11月7日
许可协议