Bootstrap

pgsql16及postgis安装

postgis 下载

登录 postgis 的官方网站:

PostGIS

下载符合 postgresql 数据库版本的 postgis 安装包。

postgresql 16.0

gcc 4.8.5

make 3.82

proj 8.2.1

libxml2 2.9.9

gdal 3.5.3

geos 3.6.6

protobuf 3.6.1

protobuf-c-1.4.1

postgis-3.4.0

各种依赖包安装地址

postgresql

https://www.postgresql.org/ftp/source/

gcc

https://gcc.gnu.org/releases.html

proj

https://proj.org/download.html

libxml2

https://download.gnome.org/sources/libxml2/

json-c

https://s3.amazonaws.com/json-c_releases/releases/index.html

gdal

https://github.com/OSGeo/gdal/releases

geos

https://libgeos.org/usage/download/

protobuf 

https://github.com/google/protobuf/releases

protobuf-c

https://github.com/protobuf-c/protobuf-c/releases

安装 postgresql

1、编译,指定安装postgresql的路径

./configure --prefix=/usr/local/pgsql

2、编译:

make

3、安装:

make install

4、创建用户组和用户:

groupadd postgres

useradd -g postgres postgres

5、创建数据库库文件存储目录、给postgres赋予权限:

mkdir /usr/local/pgsql/data

cd /usr/local/pgsql

chown postgres.postgres data

chmod 750 /usr/local/pgsql/data

6、编辑~/.bash_profile文件

#vi ~/.bash_profile

设置以下的环境变量,

export PGHOME=/usr/local/pgsql

export PGDATA=/usr/local/pgsql/data

PATH=$PATH:$PGHOME/bin

export PATH

7、初始化数据库目录:

切换用户(root也可以)

su - postgresql

初始化数据 -D指定初始化创建的数据库的文件路径

/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data

8、启动数据库

[postgres@test ~]$ pg_ctl -D /usr/local/pgsql/data -l logfile start

[postgres@test ~]$ ps -ef | grep postgres

安装 libxml2
1、上传安装包

[postgres@test ~]$ cd /opt/postgresql/soft

[postgres@test soft]$ rz

2、解压缩

[postgres@test soft]$ xz -d libxml2-2.9.9.tar.xz

[postgres@test soft]$ tar -xvf libxml2-2.9.9.tar

3、安装

[postgres@test soft]$ cd libxml2-2.9.9

[postgres@test libxml2-2.9.9]$ ./configure --prefix=/usr/local/libxml2

[postgres@test libxml2-2.9.9]$ make && make install

......

make[4]: Entering directory `/opt/postgresql/soft/libxml2-2.9.9/python'

CC libxml.lo

libxml.c:14:20: fatal error: Python.h: No such file or directory

#include <Python.h>

^

compilation terminated.

make[4]: *** [libxml.lo] Error 1

make[4]: Leaving directory `/opt/postgresql/soft/libxml2-2.9.9/python'

make[3]: *** [all-recursive] Error 1

make[3]: Leaving directory `/opt/postgresql/soft/libxml2-2.9.9/python'

make[2]: *** [all] Error 2

make[2]: Leaving directory `/opt/postgresql/soft/libxml2-2.9.9/python'

make[1]: *** [all-recursive] Error 1

make[1]: Leaving directory `/opt/postgresql/soft/libxml2-2.9.9'

make: *** [all] Error 2

报错没有找到 python 的头文件,安装 python 开发包解决

[postgres@test libxml2-2.9.9]$ exit

logout

[root@test postgresql-14.6]# mkdir /usr/local/libxml2

[root@test postgresql-14.6]# chown postgres:postgres /usr/local/libxml2

[root@test postgresql-14.6]# yum install python-devel

[root@test postgresql-14.6]# su - postgres

Last login: Tue Feb 28 10:01:38 CST 2023 on pts/1

[postgres@test ~]$ cd /opt/postgresql/soft/libxml2-2.9.9

[postgres@test libxml2-2.9.9]$ make && make install

安装 geos
1、上传安装包

[postgres@test ~]$ cd /opt/postgresql/soft

[postgres@test soft]$ rz

2、解压缩

[postgres@test soft]$ tar -xvf geos-3.6.6.tar.bz2

3、安装

[postgres@test soft]$ cd geos-3.6.6

[postgres@test geos-3.6.6]$ ./configure --prefix=/usr/local/geos

[postgres@test geos-3.6.6]$ exit

logout

[root@test postgresql-14.6]# mkdir /usr/local/geos

[root@test postgresql-14.6]# chown postgres:postgres /usr/local/geos

[root@test postgresql-14.6]# su - postgres

[postgres@test ~]$ cd /opt/postgresql/soft/geos-3.6.6

[postgres@test geos-3.6.6]$ make && make install

安装 proj
1、上传安装包

[postgres@test ~]$ cd /opt/postgresql/soft

[postgres@test soft]$ rz

2、解压缩

[postgres@test soft]$ tar -xvf proj-8.2.1.tar.gz

3、安装

[postgres@test soft]$ cd proj-8.2.1

[postgres@test proj-8.2.1]$ ./configure --prefix=/usr/local/proj

......

enabled, pthread

checking for SQLITE3... configure: error: Package requirements (sqlite3 >= 3.11) were not met:

No package 'sqlite3' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you

installed software in a non-standard prefix.

Alternatively, you may set the environment variables SQLITE3_CFLAGS

and SQLITE3_LIBS to avoid the need to call pkg-config.

See the pkg-config man page for more details.

proj8 对于 sqlite 数据库有版本要求,重新安装 sqlite 数据库

sqlite下载地址:

https://www.sqlite.org/download.html

上传并安装

[postgres@test ~]$ cd /opt/postgresql/soft

[postgres@test soft]$ rz

[postgres@test soft]$ tar -xvf sqlite-autoconf-3410000.tar.gz

[postgres@test soft]$ cd sqlite-autoconf-3410000

[postgres@test sqlite-autoconf-3410000]$ ./configure --prefix=/usr/local/sqlite

[postgres@test sqlite-autoconf-3410000]$ exit

[root@test postgresql-14.6]# mkdir /usr/local/sqlite

[root@test postgresql-14.6]# su - postgres

[postgres@test ~]$ cd /opt/postgresql/soft/sqlite-autoconf-3410000

[postgres@test sqlite-autoconf-3410000]$ make && make install

修改链接

[root@test postgresql-14.6]# cd /usr/bin/

[root@test postgresql-14.6]# mv sqlite3 sqlite3_old

[root@test postgresql-14.6]# ln -s /usr/local/sqlite/bin/sqlite3 sqlite3

继续安装 proj

[root@test postgresql-14.6]$ su - postgres

[postgres@test ~]$ cd /opt/postgresql/soft/proj-8.2.1

[postgres@test proj-8.2.1]$ ./configure --prefix=/usr/local/proj

报错找不到 libtiff-4 包,服务器已经安装了

[postgres@test proj-8.2.1]$ rpm -qa libtiff

libtiff-4.0.3-35.el7.x86_64

尝试安装开发包,发现可以解决问题

[root@test postgresql-14.6]# yum install libtiff-devel

继续安装 proj

[postgres@test proj-8.2.1]# ./configure --prefix=/usr/local/proj

报错缺少 curl 包

[root@test postgresql-14.6]# yum install curl

继续安装 proj

[postgres@test proj-8.2.1]# ./configure --prefix=/usr/local/proj

[postgres@test proj-8.2.1]# exit

[root@test postgresql-14.6]# mkdir /usr/local/proj

[root@test postgresql-14.6]# chown postgres:postgres /usr/local/proj

[root@test postgresql-14.6]# su - postgres

[postgres@test ~]$ cd /opt/postgresql/soft/proj-8.2.1

[postgres@test ~]$ make && make install

安装 gdal
1、上传安装包

[postgres@test ~]$ cd /opt/postgresql/soft

[postgres@test soft]$ rz

2、解压缩

[postgres@test soft]$ tar -xvf gdal-3.5.3.tar.gz

3、安装

[postgres@test soft]$ exit

[root@test postgresql-14.6]# mkdir /usr/local/gdal

[root@test postgresql-14.6]# chown postgres:postgres /usr/local/gdal

[root@test postgresql-14.6]# su - postgres

[postgres@test ~]$ cd /opt/postgresql/soft/gdal-3.5.3

[postgres@test gdal-3.5.3]# ./configure --prefix=/usr/local/gdal

报错找不到 PROJ 包,添加环境变量到 /etc/profile 也不见效,选择手动指定proj安装目录的方式解决。

应该再尝试在 /etc/ld.so.conf 中配置库包位置试试

[postgres@test gdal-3.5.3]# ./configure --prefix=/usr/local/gdal --with-proj=/usr/local/proj/

[postgres@test gdal-3.5.3]# make && make install

安装 json-c
1、上传安装包

[postgres@test ~]$ cd /opt/postgresql/soft

[postgres@test soft]$ rz

2、解压缩

[postgres@test soft]$ tar -xvf json-c-0.10.tar.gz

3、安装

[postgres@test soft]$ exit

[root@test postgresql-14.6]# mkdir /usr/local/jons-c

[root@test postgresql-14.6]# chown postgres:postgres /usr/local/jons-c

[root@test postgresql-14.6]# su - postgres

[postgres@test ~]$ cd /opt/postgresql/soft/json-c-0.10

[postgres@test json-c-0.10]$ ./configure --prefix=/usr/local/json-c

[postgres@test json-c-0.10]$ make && make install

安装 postgis
1、上传安装包

[postgres@test ~]$ cd /opt/postgresql/soft

[postgres@test soft]$ rz

2、解压缩

[postgres@test soft]$ tar -xvf postgis-3.2.4.tar.gz

3、安装

[postgres@test postgis-3.2.4]# ./configure --prefix=/usr/local/postgis

[postgres@test postgis-3.2.4]# ./configure --prefix=/usr/local/postgis --with-geosconfig=/usr/local/bin/geos-config

......

configure: WARNING: "Could not find json-c"

checking for PROTOBUFC... no

libprotobuf-c not found in pkg-config

checking protobuf-c/protobuf-c.h usability... no

checking protobuf-c/protobuf-c.h presence... no

checking for protobuf-c/protobuf-c.h... no

configure: error: unable to find protobuf-c/protobuf-c.h using CPPFLAGS. You can disable MVT and Geobuf support using --without-protobuf

报错找不到 protobuf-c 包,尝试使用 yum 安装,安装完成之后问题未解决

手动下载 protobuf-c 进行安装

安装 protobuf-c

下载地址

https://github.com/protobuf-c/protobuf-c/releases

上传并安装

[postgres@test ~]$ cd /opt/postgresql/soft

[postgres@test soft]$ rz

[postgres@test soft]# tar -xvf protobuf-c-1.4.1.tar.gz

[postgres@test soft]$ exit

[root@test postgresql-14.6]# mkdir /usr/local/protobuf-c

[root@test postgresql-14.6]# chown postgres:postgres /usr/local/protobuf-c

[root@test postgresql-14.6]# su - postgres

[postgres@test soft]# cd /opt/postgresql/soft/protobuf-c-1.4.1

[postgres@test protobuf-c-1.4.1]# ./configure --prefix=/usr/local/protobuf-c

protobuf-c 依赖与 protobuf 包,需要先安装 protobuf 包

安装 protobuf 包

https://github.com/google/protobuf/releases

上传并安装

[postgres@test ~]$ cd /opt/postgresql/soft

[postgres@test soft]$ rz

[postgres@test soft]# tar -xvf protobuf-all-3.6.1.tar.gz

[postgres@test soft]$ exit

[root@test postgresql-14.6]# mkdir /usr/local/protobuf

[root@test postgresql-14.6]# chown postgres:postgres /usr/local/protobuf

[root@test postgresql-14.6]# su - postgres

[postgres@test ~]$ cd /opt/postgresql/soft/protobuf-3.6.1

[postgres@test protobuf-c-1.4.1]$ ./configure --prefix=/usr/local/protobuf

[postgres@test protobuf-c-1.4.1]$ make && make install

继续安装 protobuf-c

[postgres@test protobuf-c-1.4.1]$ ./configure --prefix=/usr/local/protobuf-c

[postgres@test protobuf-c-1.4.1]$ make && make install

继续安装 postgis

[postgres@test postgis-3.2.4]$ ./configure --prefix=/usr/local/postgis --with-geosconfig=/usr/local/bin/geos-config

如果还报 protobuf-c 找不到的问题,把 protobuf-c 的库加入到环境变量中去

vim /etc/profile

export LD_LIBRARY_PATH=/usr/local/protobuf/lib:$LD_LIBRARY_PATH

如果还报其他依赖文件找不到可以指定依赖包路径

./configure --prefix=/usr/local/postgis  --with-geosconfig=/usr/local/geos/bin/geos-config --with-gdalconfig=/usr/local/gdal/bin/gdal-config --with-projdir=/usr/local/proj

[postgres@test postgis-3.2.4]$ make && make install

postgresql 接入 postgis 扩展
1、创建一个数据库

[postgres@test ~]$ psql

gisdb=# create database gisdb;

2、加载 postgis 插件

gisdb=# create extension postgis;

ERROR: could not load library "/usr/local/pgsql-14.6/lib/postgis-3.so": libgeos_c.so.1: cannot open shared object file: No such file or directory

将所安装的软件库都加入到共享库解决上述问题

[root@test ~]# vim /etc/ld.so.conf

/usr/local/gdal/lib

/usr/local/geos/lib

/usr/local/proj/lib

/usr/local/pgsql/lib

/usr/local/json-c/lib

/usr/local/libxml2/lib

/usr/local/protobuf/lib

/usr/local/protobuf-c/lib

[root@test ~]# ldconfig

gisdb=# create extension postgis;

CREATE EXTENSION


至此安装完成

;