postgis 栅格数据_PostGIS入门篇 一 PostGIS安装

本文详述了在Linux环境中安装PostgreSQL数据库和PostGIS扩展的步骤,包括升级gcc、安装依赖、创建用户、编译安装PostgreSQL、配置环境变量、初始化数据库,以及安装PostGIS所需的各种库如geos、proj、gdal等。
摘要由CSDN通过智能技术生成

1.1 安装PostgreSQL

1.1.1 升级gcc

下载较新的gcc压缩文件并解压:

[root@ ~]# wget -c http://mirror.koddos.net/gcc/releases/gcc-9.2.0/gcc-9.2.0.tar.xz

[root@ ~]# tar xvf gcc-9.2.0.tar.xz

下载并安装gcc依赖:

[root@ ~]# cd gcc-9.2.0

[root@ gcc-9.2.0]# ./contrib/download_prerequisites

安装gcc,通过--prefix指定安装路径:

[root@ gcc-9.2.0]# ./configure --prefix=/usr/local/gcc-9.2.0 --enable-checking=release --enable-languages=c,c++ --disable-multilib

#服务器如果核数较多可以使用并行编译,比如有2核

# make -j 4

#-j后的并行数量应等于核数的2倍,不要太多。

[root@ gcc-9.2.0]# make -j 4

[root@ gcc-9.2.0]# make install

配置环境变量:

[root@ gcc-9.2.0]# vi /etc/profile

#将gcc加入path,注意path里有其他bin不要改,在后面追加,以免影响其他系统

export GCC_HOME=/usr/local/gcc-9.2.0

export PATH=$GDAL_HOME/bin:$GCC_HOME/bin:$PATH

# :wq! 保存退出

#重启生效:

[root@ gcc-9.2.0]# source /etc/profile

验证gcc安装:

[root@ ~]# gcc --version

gcc (GCC) 9.2.0

Copyright (C) 2019 Free Software Foundation, Inc.

This is free software; see the source for copying conditions. There is NO

warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

移除操作系统自带的低版本的yum安装的gcc,如果安装过将会卸载:

[root@ ~]# yum remove gcc

更新系统库依赖,避免编译时发生http://glibcxx_x.x.xxx not found等lib包因版本差异导致的错误:

#先全局查询libstdc++.so.6包所在位置

[root@ ~]# find / -name libstdc++.so.6

/usr/lib64/libstdc++.so.6

/usr/lib/libstdc++.so.6

/usr/local/gcc-9.2.0/lib64/libstdc++.so.6

#其中/usr/local是我们刚刚安装的,/lib与/lib64我们不清楚是不是残留,所以必须用新的直接替换即可。

[root@ ~]# cp /usr/local/gcc-9.2.0/lib64/libstdc++.so.6 /usr/lib

[root@ ~]# cp /usr/local/gcc-9.2.0/lib64/libstdc++.so.6 /usr/lib64

1.1.2 其他依赖安装

[root@ ~]# yum install -y vim-enhanced.x86_64 gcc-java apr apr-devel openssl openssl-devel java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64 perl-Module-Install.noarch readline-devel.x86_64

为了能在数据库中使用uuid,先要安装下uuid的库,(PostgreSQL13版本自带uuid,不需要安装这个):

# 下载uuid-1.6.2.tar.gz然后拷贝到安装服务器上。由于官方链接太烂,根本点不开,先暂时在百度网盘分享个资源:

[uuid-1.6.2.tar.gz](https://pan.baidu.com/s/1NSHU8WrczfGDDsxYhEcAQQ)

[root@ ~]# tar -zxvf uuid-1.6.2.tar.gz

[root@ ~]# cd uuid-1.6.2

[root@ ~]# ./configure

[root@ ~]# make

[root@ ~]# make install

1.1.3 新建用户

[root@ ~]# useradd postgres

1.1.4 PG12源码编译安装

[root@ ~]# wget https://ftp.postgresql.org/pub/source/v12.1/postgresql-12.1.tar.gz

[root@ ~]# tar -zxvf postgresql-12.1.tar.gz

[root@ ~]# cd postgresql-12.1

[root@ postgresql-12.1]# ./configure --prefix=/home/postgres --enable-thread-safety --with-uuid=ossp --with-libs=/usr/local/lib --with-includes=/usr/local/include

[root@ postgresql-12.1]# make -j 4

[root@ postgresql-12.1]# make install

# 安装contrib工具包

[root@ postgresql-12.1]# cd contrib

[root@ contrib]# make

[root@ contrib]# make install

1.1.5 文件权限

#把程序安装目录全部赋权给postgres用户

[root@ contrib]# chown -R postgres.postgres /home/postgres/

1.1.6 配置环境变量

[root@ contrib]# su - postgres

[postgres@ ~]$ vi .bashrc

#编辑内容如下:

PGHOME=/home/postgres

export PGHOME

PGDATA=$PGHOME/data

export PGDATA

PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin

export PATH

# wq! 保存退出

#重启生效

[postgres@ ~]$ source .bashrc

1.1.7 初始化数据库与启动

[postgres@ ~]$ initdb -D $PGDATA

[postgres@ ~]$ pg_ctl start -D $PGDATA

#登录数据库

[postgres@ ~]$ psql

psql (12.1)

Type "help" for help.

postgres=#

到此为止,数据库已经完成安装,但只能本机连接,性能参数配置是默认的。有时候希望其他客户端也能连接,需要设置$PGDATA中的pg_hba.conf与postgresql.conf中相关参数,这些内容可以详细查看其他PG相关的博客和书籍。

2 安装PostGIS

本文安装PostGIS 3.0,3.0依赖的库有geos,proj,gdal,libxml,json-c,protobuf,如何要支持三维需安装sfcgal,如果要做路网分析需安装pgrouting。

2.1 安装geos

[root@ ~]# wget https://download.osgeo.org/geos/geos-3.8.0.tar.bz2

[root@ ~]# tar -jxvf geos-3.8.0.tar.bz2

[root@ ~]# cd geos-3.8.0

#指定目录安装

[root@ geos-3.8.0]# ./configure --prefix=/usr/local/geos-3.8.0

[root@ geos-3.8.0]# make -j 4

[root@ geos-3.8.0]# make install

2.2 安装proj

[root@ ~]# wget http://download.osgeo.org/proj/proj-6.2.1.tar.gz

[root@ ~]# tar -zxvf proj-6.2.1.tar.gz

[root@ ~]# cd proj-6.2.1

#指定目录安装

[root@ proj-6.2.1]# ./configure --prefix=/usr/local/proj-6.2.1

[root@ proj-6.2.1]# make -j 4

[root@ proj-6.2.1]# make install

2.3 安装gdal

[root@ ~]# wget https://download.osgeo.org/gdal/3.0.2/gdal-3.0.2.tar.gz

[root@ ~]# tar -zxvf gdal-3.0.2.tar.gz

[root@ ~]# cd gdal-3.0.2

#编译时间比较久,指定目录安装,且绑定已安装的pg

[root@ gdal-3.0.2]# ./configure --prefix=/usr/local/gdal-3.0.2 --with-pg=/home/postgres/bin/pg_config

[root@ gdal-3.0.2]# make -j 4

[root@ gdal-3.0.2]# make install

2.4 安装jsonc,libxml

[root@ ~]# wget https://github.com/json-c/json-c/archive/json-c-0.13.1-20180305.tar.gz

[root@ ~]# tar -zxvf json-c-0.13.1-20180305.tar.gz

[root@ ~]# cd json-c-0.13.1-20180305

[root@ json-c-0.13.1-20180305]# ./configure --prefix=/usr/local/json-c-0.13.1

[root@ json-c-0.13.1-20180305]# make -j 4

[root@ json-c-0.13.1-20180305]# make install

[root@ ~]# wget https://github.com/GNOME/libxml2/archive/v2.9.7.tar.gz

[root@ ~]# tar -zxvf libxml2-sources-2.9.7.tar.gz

[root@ ~]# cd libxml2-2.9.7

[root@ libxml2-2.9.7]# ./configure --prefix=/usr/local/libxml2-2.9.7

[root@ libxml22.5-2.9.7]# make -j 4

[root@ libxml2-2.9.7]# make install

2.5 安装protobuf,protobuf-c

[root@ ~]# wget https://github.com/protocolbuffers/protobuf/archive/v3.10.1.tar.gz

[root@ ~]# tar -zxvf protobuf-3.10.1.tar.gz

[root@ ~]# cd protobuf-3.10.1

[root@ protobuf-3.10.1]# ./configure --prefix=/usr/local/protobuf-3.10.1

[root@ protobuf-3.10.1]# make -j 4

[root@ protobuf-3.10.1]# make install

#配置环境变量,增加下protobuf-3.10.1/bin

[root@ ~]# vi /etc/profile

export PROTOBUF_HOME=/usr/local/protobuf-3.10.1

export PATH=$GCC_HOME/bin:$PROTOBUF_HOME/bin:$PATH

#保存退出

[root@ ~]# source /etc/profile

#验证protobuf执行程序

[root@ ~]# protoc --version

libprotoc 3.10.1

#protobuf安装成功

[root@ ~]# wget https://github.com/protobuf-c/protobuf-c/releases/download/v1.3.2/protobuf-c-1.3.2.tar.gz

[root@ ~]# tar -zxvf protobuf-c-1.3.2.tar.gz

[root@ ~]# cd protobuf-c-1.3.2

#导入protobuf的pkgconfig,否则"--No package 'protobuf' found"

[root@ protobuf-c-1.3.2]# export PKG_CONFIG_PATH=/usr/local/protobuf-3.10.1/lib/pkgconfig

[root@ protobuf-c-1.3.2]# ./configure --prefix=/usr/local/protobuf-c-1.3.2

[root@ protobuf-c-1.3.2]# make -j 4

[root@ protobuf-c-1.3.2]# make install

#配置环境变量,增加下protobuf-c-1.3.2/bin

[root@ ~]# vi /etc/profile

export PROTOBUF_HOME=/usr/local/protobuf-3.10.1

export PROTOBUFC_HOME=/usr/local/protobuf-c-1.3.2

export PATH=$GCC_HOME/bin:$PROTOBUF_HOME/bin:$PROTOBUFC_HOME/bin:$PATH

#保存退出

[root@ ~]# source /etc/profile

2.6 安装sfcgal(三维,可选)

sfcgal需要cmkae编译,需先安装下cmake:

[root@ ~]# wget https://github.com/Kitware/CMake/releases/download/v3.16.2/cmake-3.16.2.tar.gz

[root@ ~]# tar -zxvf cmake-3.16.2.tar.gz

[root@ ~]# cd cmake-3.16.2

[root@ cmake-3.16.2]# ./configure --prefix=/usr/local/cmake-3.16.2

[root@ cmake-3.16.2]# make -j 4

[root@ cmake-3.16.2]# make install

#配置环境变量

[root@ ~]# vi /etc/profile

export CMAKE_HOME=cmake-3.16.2

export PATH=$GCC_HOME/bin:$CMAKE_HOME/bin:$PROTOBUF_HOME/bin:$PROTOBUFC_HOME/bin:$PATH

#保存退出

[root@ ~]# source /etc/profile

sfcgal依赖boost,cgal,需要提前编译,编译默认目录,避免编译sfcgal时各种找不到库的问题。

[root@ ~]# yum install boost-devel

[root@ ~]# wget https://github.com/CGAL/cgal/archive/releases/CGAL-4.13.tar.gz

[root@ ~]# tar -zxvf CGAL-4.13.tar.gz

[root@ ~]# cd CGAL-4.13

[root@ CGAL-4.13]# mkdir build && cd build

#cmake不要指定安装路径

[root@ build]# cmake ..

[root@ build]# make

[root@ build]# make install

编译sfcgal:

[root@ ~]# wget https://github.com/Oslandia/SFCGAL/archive/v1.3.7.tar.gz

[root@ ~]# tar -zxvf v1.3.7.tar.gz

[root@ ~]# cd SFCGAL-1.3.7

[root@ SFCGAL-1.3.7]# mkdir build && cd build

[root@ build]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/sfcgal-1.3.7 ..

[root@ build]# make -j 4

[root@ build]# make install

pgrouting可以单独安装,在之后文章里会单独介绍。

2.7 安装PostGIS

配置ld.so.conf:

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

#编辑内容如下

include ld.so.conf.d/*.conf

/home/postgres/lib

/usr/local/proj-6.2.1/lib

/usr/local/gdal-3.0.2/lib

/usr/local/geos-3.8.0/lib

/usr/local//sfcgal-1.3.7/lib64

/usr/local/json-c-0.13.1/lib

/usr/local/libxml2-2.9.7/lib

/usr/local/protobuf-3.10.1/lib

/usr/local/protobuf-c-1.3.2/lib

#编辑完成后wq!保存退出

#保存配置,重启生效

[root@ ~]# ldconfig -v

安装postgis:

[root@ ~]# wget http://download.osgeo.org/postgis/source/postgis-3.0.0.tar.gz

[root@ ~]# tar -zxvf postgis-3.0.0.tar.gz

[root@ ~]# cd postgis-3.0.0

#根据安装不同的要求,选择任意一个configure

#基本安装,不带sfcgal

[root@ postgis-3.0.0]# ./configure --prefix=/home/postgres --with-gdalconfig=/usr/local/gdal-3.0.2/bin/gdal-config --with-pgconfig=/home/postgres/bin/pg_config --with-geosconfig=/usr/local/geos-3.8.0/bin/geos-config --with-projdir=/usr/local/proj-6.2.1 --with-xml2config=/usr/local/libxml2-2.9.7/bin/xml2-config --with-jsondir=/usr/local/json-c-0.13.1 --with-protobufdir=/usr/local/protobuf-c-1.3.2

# 带protobuf,sfcgal安装

[root@ postgis-3.0.0]# ./configure --prefix=/home/postgres --with-gdalconfig=/usr/local/gdal-3.0.2/bin/gdal-config --with-pgconfig=/home/postgres/bin/pg_config --with-geosconfig=/usr/local/geos-3.8.0/bin/geos-config --with-projdir=/usr/local/proj-6.2.1 --with-xml2config=/usr/local/libxml2-2.9.7/bin/xml2-config --with-jsondir=/usr/local/json-c-0.13.1 --with-protobufdir=/usr/local/protobuf-c-1.3.2 --with-sfcgal=/usr/local/sfcgal-1.3.7/bin/sfcgal-config

[root@ postgis-3.0.0]# make -j 4

[root@ postgis-3.0.0]# make install

可能报错什么can not found lsqlite3等错误:

yum install sqlite-devel

验证安装:

[root@ ~]# su - postgres

[postgres@ ~]$ psql

psql (12.1)

Type "help" for help.

postgres=# create database mytest;

CREATE DATABASE

postgres=# \c mytest

You are now connected to database "mytest" as user "postgres".

#验证postgis扩展

mytest=# create extension postgis;

CREATE EXTENSION

#验证栅格类数据需要的raster扩展

mytest=# create extension postgis_raster;

CREATE EXTENSION

#如果安装带有sfcgal,验证下三维sfcgal扩展

mytest=# create extension postgis_sfcgal;

CREATE EXTENSION

如果一切顺利,安装结束。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值