- 之前服务器已经安装了postgresql10+postgis2.5, 有个需求要使用GDAL,编译gdal3.0.4后,却没有PostgreSQL插件,多次配置编译后还是没有,仔细分析postgresql10+ postgis2.5是用yum安装的,缺少某些插件导致的。正好官网发布了Postgis-3.0.1,就重新搭建一个最新环境,PostGIS插件也安装成功了。
- 总结,有需要GDAL开发的同学,在linux服务器上,最好还是自己编译环境,初次编译是很痛苦,但是在此过程中,对插件之间的版本依赖关系,插件编译配置、问题定位,解决问题,shell脚本都能够全面的提升。最后还能得到一个最新版的环境,何乐而不为了!!!
- 在文章中,对错误有对应的分析,对使用linux命令有部分解释,对不熟悉linux的同学能加快安装速度
全套安装包下载
安装本文环境,预计需要1整天
一、环境准备
1、环境要求
The PostGIS Team is pleased to release PostGIS 3.0.1.
Best served with PostgreSQL 12.2, GEOS 3.8.0, SFCGAL 1.3.7, GDAL 3.0.4, PROJ 6.3.1, protobuf-c 1.3.3, json-c 0.13.1.
2、环境安装
先通过yum基础插件
yum install gcc gcc-c++ gcc-g77 make libtool flex bison autoconf automake bzip2-devel zlib-devel ncurses-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel pam-devel openssl-devel libxml2-devel gettext-devel pcre-devel
二、编译Postgresql-12.2
以下所有安装源文件放置均放在/usr/local/src/下
#用户组
groupadd postgres
#用户组添加用户
useradd -g postgres postgres
#数据库密码,设置强密码,大小写,特殊字符
passwd root@123456
mkdir -p /usr/local/pgsql
mkdir /usr/local/pgsql/data
chown -R postgres:postgres /usr/local/pgsql
cd /usr/local/src/
wget https://ftp.postgresql.org/pub/source/v12.2/postgresql-12.2.tar.gz
tar -xzvf postgresql-12.2.tar.gz
cd postgresql-12.2
./configure --prefix=/usr/local/pgsql --without-readline
make && make install
2、安装contrib目录工具
是第三方组织的一些工具代码,建议安装
cd contrib
make && make install
3、添加环境变量
vi打开配置文件后,按i插入,ESC退出编译,:wq保存,:q!不保存
vi /etc/profile
PG_HOME=/usr/local/pgsql
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 PKG_CONFIG_PATH LD_LIBRARY_PATH
source /etc/profile
查看Postgresql版本命令
pg_config --version
4、初始化数据库
#切换用户
su postgres
#初始化数据库
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
5、设置远程可访问数据库
#切换root
exit
vi /usr/local/pgsql/data/postgresql.conf
listen_address = '*'
port = 5432
vi /usr/local/pgsql/data/pg_hba.conf
host all all 0.0.0.0/0 md5
host all all 192.168.1.0/24 md5
host all all ::1/128 ident
6、数据库服务
#切换用户
su postgres
#启动数据库服务器
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
#创建数据库
/usr/local/pgsql/bin/createdb gisdb
#停止数据库服务
/usr/local/pgsql/bin/pg_ctl stop -D /usr/local/pgsql/data
#重新启动数据库
/usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data
到这里,数据库已经安装成功了,使用navicat连接试试吧
6、设置Postgresql自启动
新建postgresql.service文件,复制以下内容到postgresql.service
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
# Port number for server to listen on
Environment=PGPORT=5432
# Location of database directory
Environment=PGDATA=/usr/local/pgsql/data
# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog
# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
#ExecStartPre=/usr/local/pgsql/bin/postgresql-check-db-dir ${PGDATA}
ExecStart=/usr/local/pgsql/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300
ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/usr/local/pgsql/bin/pg_ctl reload -D ${PGDATA} -s
# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300
[Install]
WantedBy=multi-user.target
su root
cd /usr/lib/systemd/system
chmod 754 postgresql.service
systemctl enable postgresql.service
7 其它一些操作命令说明
启动,停止,重启
systemctl start postgresql.service
systemctl stop postgresql.service
systemctl restart postgresql.service
修改数据库密码
su - postgres
psql -U postgres
ALTER USER postgres WITH PASSWORD '123456'
\q
三、编译GDAL依赖
1、安装 sqlite3
wget https://www.sqlite.org/2020/sqlite-src-3310100.zip
unzip sqlite-src-3310100.zip
cd sqlite-src-3310100
bash ./configure --prefix=/usr/local/pgsql/plugin/sqlite3
make && make install
echo "/usr/local/pgsql/plugin/sqlite3/lib" > /etc/ld.so.conf.d/sqlite3.conf
ldconfig
2、安装 json-c-0.13.1
wget https://github.com/json-c/json-c/archive/json-c-0.13.1-20180305.tar.gz
tar -xzvf json-c-0.13.1-20180305.tar.gz
cd json-c-json-c-0.13.1-20180305
bash ./configure --prefix=/usr/local/pgsql/plugin/json-c
make && make install
echo "/usr/local/pgsql/plugin/json-c/lib" > /etc/ld.so.conf.d/json-c-0.13.1.conf
ldconfig
3、编译 protobuf-3.11.4,protobuf-c 1.3.3
- 编译protobuf-3.11.4
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.11.4/protobuf-cpp-3.11.4.tar.gz
tar -xzvf protobuf-cpp-3.11.4.tar.gz
cd protobuf-3.11.4
bash ./configure
make && make install
- 编译protobuf-c 1.3.3
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
./autogen.sh
bash ./configure
make && make install
出现找不到libprotobuf.so.22时
vi /etc/profile 添加配置
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
export PKG_CONFIG_PATH
source /etc/profile 配置生效
4、安装Proj 6.3.1
wget https://download.osgeo.org/proj/proj-6.3.1.tar.gz
tar -xf proj-6.3.1.tar.gz
cd proj-6.3.1
./configure --prefix=/usr/local/pgsql/plugin/proj
make && make install
echo "/usr/local/pgsql/plugin/proj/lib" > /etc/ld.so.conf.d/proj-6.3.1.conf
ldconfig
5、安装GEOS 3.8.0
wget http://download.osgeo.org/geos/geos-3.8.0.tar.bz2
tar -jxf geos-3.8.0.tar.bz2
cd geos-3.8.0
./configure --prefix=/usr/local/pgsql/plugin/geos
make && make install
echo "/usr/local/pgsql/plugin/geos/lib" > /etc/ld.so.conf.d/geos-3.8.0.conf
ldconfig
6、安装SFCGAL 1.3.7
由于SFCGAL需要依赖Boost、CGAL、GMP、MPFR这四个软件,所以具体总共需要安装以下四个软件:
boost-devel.x86_64
gmp-devel.x86_64
mpfr-devel.x86_64
CGAL-4.14
6.1 Boost-1.5.3
yum install boost boost-devel
6.2 GMP-6.1.2
wget https://gmplib.org/download/gmp/gmp-6.1.2.tar.bz2
tar -jxvf gmp-6.1.2.tar.bz2
cd gmp-6.1.2
bash ./configure --enable-cxx
make && make install
6.3 MPFR-4.0.2
wget https://www.mpfr.org/mpfr-current/mpfr-4.0.2.tar.gz
tar -xzvf mpfr-4.0.2.tar.gz
cd mpfr-4.0.2
./configure
make && make install
6.4 cgal-4.14
wget http://distfiles.macports.org/cgal/cgal-4.14.tar.xz
xz -d cgal-4.14.tar.xz
tar xvf cgal-4.14.tar
cd CGAL-4.14
mkdir build && cd build
cmake ..
make && make install
安装过程中会出现以下错误
解决方法: 修改/usr/include/boost/ cstdint.hpp 44行代码为:
#if defined(BOOST_HAS_STDINT_H) \
&& (!defined(__GLIBC__) \
|| defined(__GLIBC_HAVE_LONG_LONG) \
|| (defined(__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 17)))))
6.5 SFCGAL 1.3.7
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 ..
make && make install
echo "/usr/local/sfcgal/lib64" > /etc/ld.so.conf.d/sfcgal-1.3.7.conf
ldconfig
7、pcre-8.44安装
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
make && make intall
echo "/usr/local/pcre/lib" > /etc/ld.so.conf.d/pcre-8.44.conf
ldconfig
8、libxml2-2.9.10安装
wget http://xmlsoft.org/sources/libxml2-2.9.10.tar.gz
tar -xzvf libxml2-2.9.10.tar.gz
cd libxml2-2.9.10
./configure --prefix=/usr/local/libxml2
make && make intall
echo "/usr/local/libxml2/lib" > /etc/ld.so.conf.d/libxml2-2.9.10.conf
ldconfig
9、编译gdal.jar进行 java开发,不需要开发,可以跳过
9.1、 ant 1.10.7
wget https://mirrors.tuna.tsinghua.edu.cn/apache//ant/binaries/apache-ant-1.10.7-bin.tar.gz
tar -zxvf apache-ant-1.10.7-bin.tar.gz
mv apache-ant-1.10.7-bin /usr/local/ant
9.2、swig 4.0.1
wget http://prdownloads.sourceforge.net/swig/swig-4.0.1.tar.gz
tar -zxvf swig-4.0.1.tar.gz
cd swig-4.0.1
bash ./configure --prefix=/usr/local/swig
make && make install
9.3、配置profile
vim /etc/profile
ANT_HOME=/usr/local/ant
SWIG_HOME=/usr/local/swig
PATH=$ANT_HOME/bin:$SWIG_HOME/bin:$PATH
export ANT_HOME SWIG_HOME
source /etc/profile
查看版本
ant -version
swig -version
四、安装GDAL 3.0.4
1、编译
wget http://download.osgeo.org/gdal/3.0.4/gdal-3.0.4.tar.gz
tar -xf gdal-3.0.4.tar.gz
cd gdal-3.0.4
bash ./configure --prefix=/usr/local/pgsql/plugin/gdal --with-proj=/usr/local/pgsql/plugin/proj --with-geos=/usr/local/pgsql/plugin/geos/bin/geos-config --with-sqlite3=/usr/local/pgsql/plugin/sqlite3 --with-libjson-c=/usr/local/pgsql/plugin/json-c
make && make install
echo "/usr/local/pgsql/plugin/gdal/lib" > /etc/ld.so.conf.d/gdal-3.0.4.conf
ldconfig
2、错误
错误1:修改/usr/local/src/gdal-3.0.4/gcore/gdal_version.h.in为gdal_version.h
错误2:出现这个错误时,一般是sqlite3生成文件在/usr/local/lib下,必须移除后,重新编译sqlite3生成文件单独放置,如usr/local/sqlite3下,在gdal配置时指明--with-sqlite3
清除语句:rm要慎用,要确认清楚,删库跑路就是因为这 rm -rf /usr/local/lib/libsqlite3* /usr/local/bin/sqlite3 /usr/local/lib/pkgconfig/sqlite3.pc
错误3:缺少json-c静态库文件,可以通过指明静态库路径方式来解决,但这是临时解决方案。最好重新编译json-c到usr/local/json-c, 在gdal配置时指明--with-libjson-c
cd /usr/local/src/gdal-3.0.4/apps
/bin/sh /usr/local/src/gdal-3.0.4/libtool --mode=link --silent g++ -std=c++11 gdalserver.lo /usr/local/src/gdal-3.0.4/libgdal.la /usr/local/lib/libjson-c.a -o gdalserver
错误4: make: execvp: /usr/local/gdal-3.0.4/install-sh: Permission denied
看到这个问题,是不是很高兴,表明已经编译完成了,执行安装脚本,没权限
执行 chmod 777 install-sh,赋予最高权限
3 配置GDAL环境变量
通过vi /etc/profile去配置gdal/lib、gdal/bin、gdal/data吧,也可以把profile下载到本地改完再上传
GDAL_HOME=/usr/local/pgsql/plugin/gdal
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 LD_LIBRARY_PATH GDAL_DATA
4 GDAL安装完成,看看版本号和支持的数据格式吧
gdalinfo --version
ogr2ogr --formats
- 如只编译GDAL的同学,下面的内容可以不用看了,安装PostGIS的同学,请继续
- JavaWeb部署GDAL请看这 Linux系统编译、发布gdal项目
五、编译安装PostGIS
1、编译
wget https://download.osgeo.org/postgis/source/postgis-3.0.1.tar.gz
tar -xvzf postgis-3.0.1.tar.gz
cd postgis-3.0.1
./configure --with-pgconfig=/usr/local/pgsql/bin/pg_config --with-geosconfig=/usr/local/pgsql/plugin/geos/bin/geos-config --with-projdir=/usr/local/pgsql/plugin/proj --with-gdalconfig=/usr/local/pgsql/plugin/gdal/bin/gdal-config --with-jsondir=/usr/local/pgsql/plugin/json-c --with-pcredir=/usr/local/pcre
make && make install
2、安装fuzzystrmatch扩展
cd /usr/local/src/postgresql-12.2/contrib/fuzzystrmatch
make && make install
3、创建PostGiS数据库
- 为了PostGIS插件生效,停止启动服务
systemctl stop postgresql.service
systemctl start postgresql.service
- 创建数据库
#切换postgres用户
su - postgres
#登录PG数据库
psql
# 创建数据库
create database postgis;
#切换到postgis库中
\c postgis
#显示一下扩展模块
\dx
4、创建Postgresql扩展
create extension postgis;
create extension postgis_topology;
create extension fuzzystrmatch;
create extension postgis_tiger_geocoder;
5、问题分析
- 配置过程中发现的错误主要的是no gdal,GDALALLRegister找不到:
1:找不到gdal库,通过配置gdal环境变量解决
2:gdal库配置没有问题,在gdal检测中发生错误,最终还是gdal编译
敲重点:根据config.log分析错误原因
- 编译过程主要是各种库找到
1、个别文件通过设置软连接,如
ln -s /usr/local/lib64/libSFCGAL.so /usr/local/lib/libSFCGAL.so
...
2、依赖库比较多时,就直接把依赖库lib加入系统共享库,如
echo '/usr/local/pgsql/lib' >> /etc/ld.so.conf
echo '/usr/local/pgsql/plugin/gdal/lib' >> /etc/ld.so.conf
...
ldconfig