- 之前服務(wù)器已經(jīng)安裝了postgresql10+postgis2.5, 有個(gè)需求要使用GDAL,編譯gdal3.0.4后谬哀,卻沒有PostgreSQL插件玷坠,多次配置編譯后還是沒有甥啄,仔細(xì)分析postgresql10+ postgis2.5是用yum安裝的刑顺,缺少某些插件導(dǎo)致的玻孟。正好官網(wǎng)發(fā)布了Postgis-3.0.1笆载,就重新搭建一個(gè)最新環(huán)境扑馁,PostGIS插件也安裝成功了。
- 總結(jié)凉驻,有需要GDAL開發(fā)的同學(xué)腻要,在linux服務(wù)器上,最好還是自己編譯環(huán)境沿侈,初次編譯是很痛苦闯第,但是在此過程中,對(duì)插件之間的版本依賴關(guān)系缀拭,插件編譯配置咳短、問題定位,解決問題蛛淋,shell腳本都能夠全面的提升咙好。最后還能得到一個(gè)最新版的環(huán)境,何樂而不為了:趾伞9葱А!
- 在文章中,對(duì)錯(cuò)誤有對(duì)應(yīng)的分析层宫,對(duì)使用linux命令有部分解釋杨伙,對(duì)不熟悉linux的同學(xué)能加快安裝速度
安裝本文環(huán)境,預(yù)計(jì)需要1整天
一萌腿、環(huán)境準(zhǔn)備
1限匣、環(huán)境要求
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、環(huán)境安裝
先通過yum基礎(chǔ)插件
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
#數(shù)據(jù)庫密碼,設(shè)置強(qiáng)密碼米死,大小寫,特殊字符
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、添加環(huán)境變量
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脯丝、初始化數(shù)據(jù)庫
#切換用戶
su postgres
#初始化數(shù)據(jù)庫
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
5、設(shè)置遠(yuǎn)程可訪問數(shù)據(jù)庫
#切換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伏伐、數(shù)據(jù)庫服務(wù)
#切換用戶
su postgres
#啟動(dòng)數(shù)據(jù)庫服務(wù)器
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
#創(chuàng)建數(shù)據(jù)庫
/usr/local/pgsql/bin/createdb gisdb
#停止數(shù)據(jù)庫服務(wù)
/usr/local/pgsql/bin/pg_ctl stop -D /usr/local/pgsql/data
#重新啟動(dòng)數(shù)據(jù)庫
/usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data
到這里,數(shù)據(jù)庫已經(jīng)安裝成功了晕拆,使用navicat連接試試吧
6藐翎、設(shè)置Postgresql自啟動(dòng)
新建postgresql.service文件,復(fù)制以下內(nèi)容到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 其它一些操作命令說明
啟動(dòng)实幕,停止吝镣,重啟
systemctl start postgresql.service
systemctl stop postgresql.service
systemctl restart postgresql.service
修改數(shù)據(jù)庫密碼
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
出現(xiàn)找不到libprotobuf.so.22時(shí)
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這四個(gè)軟件府蛇,所以具體總共需要安裝以下四個(gè)軟件:
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
安裝過程中會(huì)出現(xiàn)以下錯(cuò)誤
解決方法: 修改/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進(jìn)行 java開發(fā)务荆,不需要開發(fā)妆距,可以跳過
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吸耿、錯(cuò)誤
錯(cuò)誤1:修改/usr/local/src/gdal-3.0.4/gcore/gdal_version.h.in為gdal_version.h
錯(cuò)誤2:出現(xiàn)這個(gè)錯(cuò)誤時(shí),一般是sqlite3生成文件在/usr/local/lib下酷窥,必須移除后咽安,重新編譯sqlite3生成文件單獨(dú)放置,如usr/local/sqlite3下蓬推,在gdal配置時(shí)指明--with-sqlite3
清除語句:rm要慎用妆棒,要確認(rèn)清楚,刪庫跑路就是因?yàn)檫@ rm -rf /usr/local/lib/libsqlite3* /usr/local/bin/sqlite3 /usr/local/lib/pkgconfig/sqlite3.pc
錯(cuò)誤3:缺少json-c靜態(tài)庫文件,可以通過指明靜態(tài)庫路徑方式來解決沸伏,但這是臨時(shí)解決方案糕珊。最好重新編譯json-c到usr/local/json-c, 在gdal配置時(shí)指明--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
錯(cuò)誤4: make: execvp: /usr/local/gdal-3.0.4/install-sh: Permission denied
看到這個(gè)問題,是不是很高興毅糟,表明已經(jīng)編譯完成了红选,執(zhí)行安裝腳本,沒權(quán)限
執(zhí)行 chmod 777 install-sh姆另,賦予最高權(quán)限
3 配置GDAL環(huán)境變量
通過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安裝完成蝶防,看看版本號(hào)和支持的數(shù)據(jù)格式吧
gdalinfo --version
ogr2ogr --formats
- 如只編譯GDAL的同學(xué),下面的內(nèi)容可以不用看了明吩,安裝PostGIS的同學(xué)间学,請(qǐng)繼續(xù)
- JavaWeb部署GDAL請(qǐng)看這 Linux系統(tǒng)編譯、發(fā)布gdal項(xiàng)目
五印荔、編譯安裝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擴(kuò)展
cd /usr/local/src/postgresql-12.2/contrib/fuzzystrmatch
make && make install
3躏鱼、創(chuàng)建PostGiS數(shù)據(jù)庫
- 為了PostGIS插件生效氮采,停止啟動(dòng)服務(wù)
systemctl stop postgresql.service
systemctl start postgresql.service
- 創(chuàng)建數(shù)據(jù)庫
#切換postgres用戶
su - postgres
#登錄PG數(shù)據(jù)庫
psql
# 創(chuàng)建數(shù)據(jù)庫
create database postgis;
#切換到postgis庫中
\c postgis
#顯示一下擴(kuò)展模塊
\dx
4、創(chuàng)建Postgresql擴(kuò)展
create extension postgis;
create extension postgis_topology;
create extension fuzzystrmatch;
create extension postgis_tiger_geocoder;
5染苛、問題分析
- 配置過程中發(fā)現(xiàn)的錯(cuò)誤主要的是no gdal鹊漠,GDALALLRegister找不到:
1:找不到gdal庫,通過配置gdal環(huán)境變量解決
2:gdal庫配置沒有問題主到,在gdal檢測中發(fā)生錯(cuò)誤,最終還是gdal編譯
敲重點(diǎn):根據(jù)config.log分析錯(cuò)誤原因
- 編譯過程主要是各種庫找到
1躯概、個(gè)別文件通過設(shè)置軟連接登钥,如
ln -s /usr/local/lib64/libSFCGAL.so /usr/local/lib/libSFCGAL.so
...
2、依賴庫比較多時(shí)娶靡,就直接把依賴庫lib加入系統(tǒng)共享庫,如
echo '/usr/local/pgsql/lib' >> /etc/ld.so.conf
echo '/usr/local/pgsql/plugin/gdal/lib' >> /etc/ld.so.conf
...
ldconfig