PostGIS是一个Postgresql的扩展,增加了对存储和操作空间数据类型的支持。
当我们构建在地图上存储、操作和可视化数据的软件应用程序时,通常需要使用空间数据存储。
本次项目也需要使用空间数据,因此尝试在PostgreSQL16.4环境安装postgis3.5。
准备工作
环境以CentOS为例。
下载源码包
首先先下载PostgreSQL16.4和postgis3.5的源码包。
postgresql下载地址:
https://www.postgresql.org/download/
复制
postgis下载地址:
https://postgis.net/development/source_code/
复制
将下载好的包上传至/app/database目录下。
安装依赖
配好yum源后安装依赖包:
yum groupinstall -y "Development Tools" "Legacy UNIX Compatibility" yum install -y bison flex readline* zlib-devel gcc* gmake
复制
主机名、用户组、目录配置
修改主机名,多个节点的话应配好映射。
创建用户和组:
建议非root用户管理,因此创建postgres用户:
useradd postgres passwd postgres
复制
创建目录并授权
一个是软件安装目录,一个是数据目录,
可以类比mysql的basedir和datadir;
mkdir -p /usr/local/pg16 mkdir -p /data/pg16 chown -R postgres. /usr/local/pg16 chown -R postgres. /data/pg16 chmod 700 -R /data/pg16
复制
OS优化
主机OS为centos7.9。
操作系统参数优化:
vim /etc/sysctl.conf kernel.shmmax = 68719476736 kernel.shmall = 4294967296 kernel.shmmni = 4096 kernel.sem = 50100 64128000 50100 1280 fs.file-max = 7672460 net.ipv4.ip_local_port_range = 9000 65000 net.core.rmem_default = 1048576 net.core.rmem_max = 4194304 net.core.wmem_default = 262144 net.core.wmem_max = 1048576
复制
按需要配置进行相应调整,主要调整共享内存相关的参数。
通常建议将 kernel.shmmax 设置为 shared_buffers 的 1.5 到 2 倍。
生效配置:
sysctl -p
复制
限制参数:
vim /etc/security/limits.conf * soft nofile 131072 * hard nofile 131072 * soft nproc 131072 * hard nproc 131072 * soft stack unlimited * hard stack unlimited * hard memlock 50000000 * soft memlock 50000000
复制
建议关闭numa,设置IO策略为deadline(机械)或者noop(SSD):
测试机numa本身未开启,如需关闭需要编辑/etc/default/grub,在GRUB_CMDLINE_LINUX一栏添加numa=off;
查看IO策略,此处以xvda为例:
cat /sys/block/xvda/queue/scheduler
复制
显示为deadline或noop都可以(算法不同,noop更倾向于饿死读而优先写,适合SSD)。避免使用cfq。
echo noop > /sys/block/xvda/queue/scheduler
复制
这种方式重启后会失效,如果想持久化则需要将命令放在/etc/rc.local中,或者直接修改grub:
vim /etc/default/grub
复制
在GRUB_CMDLINE_LINUX一栏添加elevator=noop,再重新生成grub2.cfg配置文件,或者:
grubby --update-kernel=ALL --args="elevator=noop"
复制
重启生效。
源码安装postgresql16.4
cd /app/database tar -xf postgresql-16.4.tar.gz
复制
然后授权给postgres用户,切换到postgres用户
cd postgresql-16.4
复制
./configure --help查看可定制的参数,其中打印的结果中–with类的都是打开一些功能,此外还有–without,–disable等,具体参考官网;
此处指定安装目录和端口,不指定的话默认端口是5432;这些参数后面初始化也可以指定
./configure --prefix=/usr/local/pg16 --with-python --with-perl --enable-nls --with-readline
复制
这步结果会显示缺失的依赖,我这里还需要额外安装:
yum install libicu-devel -y yum install perl-ExtUtils-Embed -y yum install python3 python3-devel -y
复制
安装完成后重新configure。
编译全部可用功能:
gmake world
复制
把所有编译好的进行安装(本质其实就是建目录+拷贝):
gmake install -world
复制
配置postgres用户的环境变量:
cd ~ vim .bash_profile export PGDATA=/data/pg16 export LANG=en_US.utf8 export PGHOME=/usr/local/pg16 export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH export DATE=`date +"%Y%m%d%H%M"` export PATH=$PGHOME/bin:$PATH:. export MANPATH=$PGHOME/share/man:$MANPATH export PGUSER=postgres source .bash_profile
复制
检查版本
psql --version
复制
如果正常显示说明环境变量配置成功。
初始化数据、启动及基本配置
简易初始化(创建实例),在postgres用户下执行:
initdb -D /data/pg16 -w
复制
生产环境建议:
initdb -A md5 -D $PGDATA -E utf8 --locale=C -W
复制
-A 是加密方式
-D 是数据目录
-E 是字符集 (默认即utf8)
执行之后会提示输入密码。
可以按照初始化打印的结果来启动,完整命令:
pg_ctl -D /data/pg16/ -l logfile start
复制
如果配了环境变量也可以直接pg_ctl start来启动,因为pgdata能识别到;
重启:pg_ctl restart
关闭:pg_ctl stop
通过pg_ctl --help可以查看完整的选项,
其中关闭和重启选项中的shutdown modes包括smart、fast、immediate;
smart是最安全的关闭方法,等待所有客户端连接关闭之后才关闭;
fast是用的生产中最多的,自动杀掉连接,回滚未完成事务;
immediate相当于kill -9;
对应的语句:
pg_ctl -D /data/pg16/ stop -ms pg_ctl -D /data/pg16/ stop -mf pg_ctl -D /data/pg16/ stop -mi
复制
此外启动数据库还可以通过脚本启动,需要切换回root用户执行(使用此脚本需要修改脚本中prefix、PGDATA等内容。)
/opt/postgresql-16.4/contrib/start-scripts/linux
复制
远程连接还需配置实例级别的访问控制:
cd $PGDATA vim pg_hba.conf host DBname username 10.10.10.0/24 md5 host all all 0.0.0.0/0 md5
复制
md5的未知写reject就不允许这一条登录;
如果两条有重叠,则按从上到下顺序读取,一旦一条生效,后面的则不会读到。
此外由于默认只监听本地,还需要配置linstener才能监听到,以全部监听为例:
vim postgresql.conf listen_addresses = '0.0.0.0'
复制
重启实例后生效:
pg_ctl restart -mf
复制
用超管postgres以TCP/IP方式登录验证:
psql -d postgres -h 10.5.208.28 -p 5432 -U postgres
复制
源码安装postgis3.5
解压:
tar -xf postgis-3.5.0.tar.gz
复制
configure:
cd postgis-3.5.0 ./configure
复制
可能会出现各种问题。
gis依赖项比较多,官网描述如下:
- PostgreSQL 12 - 17. A complete installation of PostgreSQL (including server headers) is required. PostgreSQL is available from https://www.postgresql.org .
For a full PostgreSQL / PostGIS support matrix and PostGIS/GEOS support matrix refer to https://trac.osgeo.org/postgis/wiki/UsersWikiPostgreSQLPostGIS- GNU C compiler (
gcc
). Some other ANSI C compilers can be used to compile PostGIS, but we find far fewer problems when compiling withgcc
.- GNU Make (
gmake
ormake
). For many systems, GNUmake
is the default version of make. Check the version by invokingmake -v
. Other versions ofmake
may not process the PostGISMakefile
properly.- Proj reprojection library. Proj 6.1 or above is required. The Proj library is used to provide coordinate reprojection support within PostGIS. Proj is available for download from https://proj.org/ .
- GEOS geometry library, version 3.8.0 or greater, but GEOS 3.12+ is required to take full advantage of all the new functions and features. GEOS is available for download from https://libgeos.org .
- LibXML2, version 2.5.x or higher. LibXML2 is currently used in some imports functions (ST_GeomFromGML and ST_GeomFromKML). LibXML2 is available for download from https://gitlab.gnome.org/GNOME/libxml2/-/releases.
- JSON-C, version 0.9 or higher. JSON-C is currently used to import GeoJSON via the function ST_GeomFromGeoJson. JSON-C is available for download from https://github.com/json-c/json-c/releases/.
- GDAL, version 3+ is preferred. This is required for raster support. https://gdal.org/download.html.
- If compiling with PostgreSQL+JIT, LLVM version >=6 is required https://trac.osgeo.org/postgis/ticket/4125.
之前用yum安装:
yum install geos* -y yum install proj* -y yum install protobuf-c* -y ...
复制
发现版本不对,下面一一处理。
proj
rpm -qa |grep proj
复制
使用rpm -e --nodeps 选出其中proj相关的低版本包卸载掉。
然后我们去官网下6.1版本以上的。
https://proj.org/en/9.4/download.html
复制
我们这里下载7.2.1版本。
proj-7.2.1依赖sqlite3>=3.11版本
因此我们下载:
https://www.sqlite.org/download.html
复制
选择源码包,本次使用的是sqlite-autoconf-3470000.tar.gz
上传、解压后配置安装目录,编译安装
./configure --prefix=/usr/local make make install
复制
配置环境变量:
find / -name "pkgconfig" -print export PKG_CONFIG_PATH=/usr/lib64/pkgconfig:/usr/share/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/lib64/pkgconfig:$PKG_CONFIG_PATH
复制
可将此环境变量写入/etc/profile
如果误卸载原来自带的3.7则yum会有问题,解决方法参考:
https://blog.csdn.net/qq_42883074/article/details/111033849
复制
sqlite安装完成后,然后安装如下依赖:
yum -y install libxslt pkg-devel yum install libtiff-devel -y yum install curl-devel -y
复制
然后正式安装。
解压proj,进入解压目录
tar -xf proj-7.2.1.tar.gz cd proj-7.2.1
复制
生成配置、构建
./configure --prefix=/usr/local/pg16/plugin/proj make make install
复制
修改ld.so.conf
echo "/usr/local/pg16/plugin/proj/lib" >> /etc/ld.so.conf ldconfig
复制
geos
遇到报错:
error: PostGIS requires GEOS >= 3.8.0
复制
需要升级GEOS,先把原版本卸载:
rpm -qa |grep geos yum remove geos* -y rpm -qa |grep geos
复制
如果不卸载,后续虽然编译安装能成功,但加载扩展插件时会报undefined symbol: GEOSVoronoiDiagram的错误。
然后进入官网下载,本次使用3.8:
https://libgeos.org/usage/download/
复制
上传服务器后解压、使用autoconf工具 用configure.ac 文件生成 configure 脚本:
tar -xf geos-3.8.4.tar.bz2 cd geos-3.8.4 autoconf
复制
如果出现error: possibly undefined macro的报错,则运行:
autoreconf --install
复制
然后删除,重新生成configure脚本。
生成configure 脚本后,生成配置、构建
./configure --prefix=/usr/local/pg16/plugin/geos make make install
复制
修改ld.so.conf
echo "/usr/local/pg16/plugin/geos/lib" >> /etc/ld.so.conf ldconfig
复制
如果不进行此步骤,后续加载扩展插件时会报找不到共享对象文件的错误,
其他依赖同理。
gdal
官网下载地址:
https://gdal.org/en/latest/download_past.html#download-past
复制
要求使用3版本以上,本次使用3.2.3
上传、解压、配置、编译安装:
cd gdal-3.2.3/ ./configure --prefix=/usr/local/pg16/plugin/gdal --with-proj=/usr/local/pg16/plugin/proj make make install
复制
修改ld.so.conf
echo "/usr/local/pg16/plugin/gdal/lib" >> /etc/ld.so.conf ldconfig
复制
protobuf和protobuf-c
protobuf和protobuf-c互为依赖。
要先安装protobuf2.6.1以上的版本后,才能正常编译出protobuf-c的bin、lib等
要求protobuf-c>=1.1.0版本。
检查已安装版本:
rpm -qa |grep protobuf
复制
使用rpm -e --nodeps卸载即可。
下载地址:
https://github.com/protobuf-c/protobuf-c/tags https://github.com/protocolbuffers/protobuf/tags
复制
这里我们选择protobuf-3.20.3.tar.gz和protobuf-c-1.4.1.tar.gz
上传解压后,先安装protobuf
进入解压后的目录执行:
./autogen.sh ./configure --prefix=/usr/local/protobuf make make install
复制
配环境变量:
echo '/usr/local/protobuf/lib/' >> /etc/ld.so.conf ldconfig echo 'export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/' >> /etc/profile echo 'export PROTOBUF=/usr/local/protobuf' >> /etc/profile source /etc/profile
复制
再安装protobuf-c
./autogen.sh ./configure --prefix=/usr/local/protobuf-c/ make make install
复制
配环境变量:
echo "/usr/local/protobuf-c/lib/" >> /etc/ld.so.conf ldconfig echo 'export PATH=$CMAKE_HOME/bin:$PROTOBUF_HOME/bin:$PATH:/usr/local/protobuf-c/bin' >> /etc/profile echo 'export PATH=$CMAKE_HOME/bin:$PROTOBUF_HOME/bin:$PATH:/usr/local/protobuf-c/bin:/usr/local/gadl/bin' >> /etc/profile source /etc/profile
复制
再次configure可能会出现如下报错,则需要按照下文编译安装指定参数:
configure: error: unable to find protobuf-c/protobuf-c.h using CPPFLAGS. You can disable MVT and Geobuf support using --without-protobuf
复制
json-c和libxml
要求0.9版本及以上,我们使用0.13.1
下载:
https://github.com/json-c/json-c/archive/json-c-0.13.1-20180305.tar.gz
复制
编译安装:
cd json-c-json-c-0.13.1-20180305 ./configure --prefix=/usr/local/json-c/ make make install
复制
修改ld.so.conf
echo "/usr/local/json-c/lib/" >> /etc/ld.so.conf ldconfig
复制
安装libxml:
yum install libxml2-devel -y
复制
sfcgal
在官方news中发现pg>9.6并且GEOS>3.6中,已经将postgis_sfcgal(三维)与postgis分离。
CGAL、SFCGAL如需要安装请参考:
http://www.icodebang.cn/article/413818 https://zhuanlan.zhihu.com/p/143690286
复制
sfcgal需要cmkae编译,需先安装下cmake
下载地址:
https://github.com/Kitware/CMake/releases/tag/v3.16.9
复制
cmake需要安装openssl-devel
yum install openssl-devel -y
复制
我们下载CMake-3.16.9.tar.gz,上传,解压,然后源码安装:
cd CMake-3.16.9 ./configure --prefix=/usr/local/cmake make make install
复制
配环境变量,在/etc/profile添加:
export CMAKE_HOME=/usr/local/cmake export PATH=$GCC_HOME/bin:$CMAKE_HOME/bin:$PROTOBUF_HOME/bin:$PROTOBUFC_HOME/bin:$PATH
复制
之后source生效。
sfcgal依赖boost,cgal,需要提前编译,编译默认目录,避免编译sfcgal时各种找不到库的问题。
安装boost
yum install boost-devel -y
复制
CGAL下载地址:
https://github.com/CGAL/cgal/tags
复制
本次使用cgal-4.14.3.tar.gz
上传解压后编译安装:
cd cgal-4.14.3 cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/cgal make make install
复制
修改ld.so.conf
echo "/usr/local/cgal/lib/" >> /etc/ld.so.conf ldconfig
复制
SFCGAL下载地址:
https://github.com/Oslandia/SFCGAL/archive/
复制
本次使用v1.3.8.tar.gz
上传解压后编译安装:
cd SFCGAL-1.3.8 cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/sfcgal make -j 4 make install
复制
修改ld.so.conf:
echo "/usr/local/sfcgal/lib64" >> /etc/ld.so.conf ldconfig
复制
正式安装PostGIS
cd postgis-3.5.0 ./configure --prefix=/usr/local/pg16/plugin/postgis --with-pgconfig=/usr/local/pg16/bin/pg_config --with-geosconfig=/usr/local/pg16/plugin/geos/bin/geos-config --with-projdir=/usr/local/pg16/plugin/proj --with-gdalconfig=/usr/local/pg16/plugin/gdal/bin/gdal-config --with-protobufdir=/usr/local/protobuf-c --with-jsondir=/usr/local/json-c --with-sfcgal=/usr/local/sfcgal/bin/sfcgal-config make make install
复制
参考:
https://blog.csdn.net/weixin_44011559/article/details/136856670 https://blog.csdn.net/alwaysbefine/article/details/142470815 https://blog.csdn.net/ws972361669/article/details/129286157 https://www.modb.pro/db/1684329321233195008
复制
加载扩展插件
进入数据库客户端执行:
SELECT name, default_version,installed_version FROM pg_available_extensions WHERE name LIKE 'postgis%' or name LIKE 'address%';
复制
显示可用扩展结果为:
name | default_version | installed_version ------------------------------+-----------------+------------------- postgis | 3.5.0 | postgis_tiger_geocoder | 3.5.0 | postgis_raster | 3.5.0 | postgis_topology | 3.5.0 | postgis_sfcgal | 3.5.0 | address_standardizer | 3.5.0 | address_standardizer_data_us | 3.5.0 | (7 rows)
复制
查看是否已安装:
\dx postgis*
复制
现在尚未加载,需要我们进行加载安装:
CREATE EXTENSION postgis; CREATE EXTENSION postgis_tiger_geocoder; CREATE EXTENSION postgis_raster; CREATE EXTENSION postgis_topology; CREATE EXTENSION postgis_sfcgal;
复制
其中postgis_tiger_geocoder会报错:
ERROR: required extension "fuzzystrmatch" is not installed
复制
我们进入软件目录进行安装:
cd /app/database/postgresql-16.4/contrib/fuzzystrmatch make make install
复制
然后加载:
CREATE EXTENSION fuzzystrmatch; CREATE EXTENSION postgis_tiger_geocoder;
复制
查看版本详细信息:
SELECT postgis_full_version();
复制
查看当前数据库实例中已安装和启用的扩展
SELECT * FROM pg_extension;
复制
如果需要卸载extension,可以使用以下命令:
DROP EXTENSION postgis;
复制
至此PostGIS 3.5已安装完成。