linux上离线安装PostgreSQL和插件PostGIS

由于公司业务需要安装PostgreSQL,需要部署在Linux服务器上。在实际生产环境中,服务器通常是不会连接外网的,基于官方文档和网上相关资料,总结出在离线环境下Linux服务器(CentOS7)以源码编译的方式安装PostgreSQL的详细步骤如下:
检查编译环境:

检查服务器上是否存在gcc-c++的环境,使用命令:
可以使用以下命令:

gcc -v
g++ -v
或rpm -qa | grep gcc-c++

如果不存在gcc-c++的环境:

离线安装步骤为:

1、获取相关rpm包,将其存放到服务器上某一路径
这里有两种方式:
方式1:从centos7的系统安装镜像中提取:解压镜像文件,进入"Packages"目录,取出如图1所示rpm包
方式2:访问镜像网站获取:http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/ ,将下载的安装包上传到某一目录下
接下来进入上传rpm包的路径,安装命令如下:

rpm -Uvh *.rpm --nodeps --force
随后使用
gcc -v
g++ -v
的命令进行测试一下

如果看到gcc版本和g++详细的版本信息,说明安装完成。

一、下载postgresql源码包

浏览器访问 https://www.postgresql.org/ftp/source/,选择对应版本;
这里我选择的是v9.6.3版本;
可以根据自己的需要下载对应的tar.gz源码包,然后将其上传到服务器指定路径。

二、解压

[root@node01 software]# tar -zxvf postgresql-9.6.3.tar.gz -C /opt/moudles/

建立用户和用户组
postgresql不能以root身份运行,要以其他用户身份运行

[root@node01 software]# rpm -qa | grep postgres
[root@node01 software]# groupadd postgres
[root@node01 software]# useradd -g postgres postgres
[root@node01 software]# passwd postgres
Changing password for user postgres.
New password: 
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
三、编译安装软件

#配置 ./configure --prefix=/opt/pgsql-9.6.3 可以指定目录

[root@node01 pgsq]# ./configure
1)编译
[root@node01 pgsq]# make
2)安装
[root@node01 pgsq]# .make install

这些执行完了,会在/usr/local/生成一个/usr/local/pgsql目录。这就是编译后安装的目录。

设置环境变量

vim /etc/profile

export LD_LBRARY_PATH=/usr/local/pgsql/lib:$LD_LIBRARY_PATH
export PATH=/usr/local/pgsql/bin:$PATH
export MANPATH=/usr/local/pgsql/man:$MANPATH
3)生效环境变量
[root@node01 pgsq]# source /etc/profile
4)新建数据文件目录
[root@node01 pgsq]# mkdir /usr/local/pgsql/data
5) 更改数据文件目录的属主为postgres
[root@node01 pgsq]# chown -R postgres.postgres /usr/local/pgsql/data

初始化数据data目录
[root@node01 pgsq]# su postgres
[postgres@node01 pgsq]$ cd /usr/local/pgsql/bin/
6) 设置local为C ,并且template1编码为UNICODE,使数据库,支持中文
[postgres@node01 bin]$ ./initdb --locale=C -E UNICODE -D ../data/
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory ../data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
7)可以看到初始化成功
Success. You can now start the database server using:

    ./pg_ctl -D ../data/ -l logfile start

配置数据库
[postgres@node01 bin]$ su root
密码:

[root@node01 bin]# touch /var/log/pgsql.log
8)更改日志文件的属主为postgres

[root@node01 bin]# chown postgres /var/log/pgsql.log
[root@node01 bin]# su postgres
[postgres@node01 bin]$ cd …/data/

修改配置使监听生效,取消下面两个注释

[postgres@node01 data]$ vim postgresql.conf 

listen_addresses = '*'          # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
port = 5432                             # (change requires restart)

9)启动 关闭数据库
  1. 启动
root@node01 bin]# su postgres
[postgres@node01 bin]$ ./pg_ctl start -D /usr/local/pgsql/data/  
waiting for server to start....2019-10-12 13:07:59.740 CST [25117] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2019-10-12 13:07:59.740 CST [25117] LOG:  listening on IPv6 address "::", port 5432
2019-10-12 13:07:59.743 CST [25117] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2019-10-12 13:07:59.764 CST [25118] LOG:  database system was shut down at 2019-10-12 12:58:58 CST
2019-10-12 13:07:59.780 CST [25117] LOG:  database system is ready to accept connections
 done
server started
  1. 关闭
[postgres@node01 bin]$ ./pg_ctl stop -D /usr/local/pgsql/data/  
waiting for server to start....2019-10-12 13:07:59.740 CST [25117] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2019-10-12 13:07:59.740 CST [25117] LOG:  listening on IPv6 address "::", port 5432
2019-10-12 13:07:59.743 CST [25117] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2019-10-12 13:07:59.764 CST [25118] LOG:  database system was shut down at 2019-10-12 12:58:58 CST
2019-10-12 13:07:59.780 CST [25117] LOG:  database system is ready to accept connections
 done
server started

此时我们远程连接还是不可以的

10)数据库操作
  1. 创建数据库
[postgres@node01 bin]$ ./createdb demo
[postgres@node01 bin]$ 2019-10-12 13:09:07.060 CST [25133] FATAL:  no pg_hba.conf entry for host "172.16.2.103", user "postgres", database "demo"
2019-10-12 13:09:24.168 CST [25134] FATAL:  no pg_hba.conf entry for host "172.16.2.103", user "postgres", database "demo"
2019-10-12 13:09:40.652 CST [25136] FATAL:  no pg_hba.conf entry for host "172.16.2.103", user "postgres", database "demo"
  1. 创建用户
[postgres@node01 bin]$ ./createuser -A -D -E -P dm
Enter password for new role: 
Enter it again: 
  1. 登录数据库
[postgres@node01 bin]$ ./psql -d dm -U dm
psql (10.1)
Type "help" for help.

dm=>
dm=> help
You are using psql, the command-line interface to PostgreSQL.
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit
dm=> \q

注意:要在这里使用中文需要先执行
dm=> set client_encoding to 'gbk';
接下来安装postgresql的插件PostGIS-2.1.8

前提条件:
安装PostGIS之前必须先安装proj,geos,gdal.

1) 安装proj4

下载proj-4.8.0.tar.gz的链接为:http://download.osgeo.org/proj/proj-4.8.0.tar.gz

[root@node01 tools]# tar xf proj-4.8.0.tar.gz

[root@node01 tools]# cd proj-4.8.0

[root@node01 proj-4.8.0]# ./configure --prefix=/usr/local/pgsql/plugin/proj

[root@node01 proj-4.8.0]# make && make install

Waiting for about 1min



#配置和加载动态链接库:

[root@node01 proj-4.8.0]# vim /etc/ld.so.conf.d/proj-4.8.0.conf

/usr/local/pgsql/plugin/proj/lib

[root@node01 proj-4.8.0]# ldconfig
2) 安装geos

下载geos-3.4.2.tar.bz2的链接为:http://download.osgeo.org/geos/geos-3.4.2.tar.bz2

[root@node01 tools]# bzip2 -d geos-3.4.2.tar.bz2

[root@node01 tools]# tar xf geos-3.4.2.tar

[root@node01 geos-3.4.2]# ./configure --prefix=/usr/local/pgsql/plugin/geos

......

Swig: false

Python bindings: false

Ruby bindings: false

PHP bindings: false

#这些可忽略

[root@node01 geos-3.4.2]# make && make install

Waiting for about 5min

#配置和加载动态链接库:

[root@node01 geos-3.4.2]# vim /etc/ld.so.conf.d/geos-3.4.2.conf

/usr/local/pgsql/plugin/geos/lib

[root@node01 geos-3.4.2]# ldconfig
3) 安装gdal

下载gdal-2.0.1.tar.gz的链接为:http://download.osgeo.org/gdal/2.0.1/gdal-2.0.1.tar.gz

[root@node01 tools]# tar xf gdal-2.0.1.tar.gz

[root@node01 tools]# cd gdal-2.0.1

[root@node01 gdal-2.0.1]# ./configure --prefix=/usr/local/pgsql/plugin/gdal

[root@node01 gdal-2.0.1]#make && make install

Waiting for about 15min



#配置和加载动态链接库:

[root@node01 gdal-2.0.1]# vim /etc/ld.so.conf.d/gdal-2.0.1.conf

/usr/local/pgsql/plugin/gdal/lib

[root@node01 gdal-2.0.1]# ldconfig
3) 安装PostGIS

下载postgis-2.1.8.tar.gz的链接为:http://download.osgeo.org/postgis/source/postgis-2.1.8.tar.gz

[root@node01 tools]# tar xf postgis-2.1.8.tar.gz

./configure --help参数:

  --prefix=PREFIX         install architecture-independent files in PREFIX [/usr/local]

  --with-pgconfig=FILE    specify an alternative pg_config file

  --with-geosconfig=FILE  specify an alternative geos-config file

  --with-projdir=PATH     specify the PROJ.4 installation directory

  --without-raster        Disable the raster extension



[root@node01 postgis-2.1.8]# ./configure --prefix=/usr/local/pgsql/plugin/postgis \

--with-pgconfig=/usr/local/pgsql/bin/pg_config \

--with-geosconfig=/usr/local/pgsql/plugin/geos/bin/geos-config \

--with-gdalconfig=/usr/local/pgsql/plugin/gdal/bin/gdal-config \

--with-projdir=/usr/local/pgsql/plugin/proj

.........

PostGIS is now configured for x86_64-unknown-linux-gnu

 -------------- Compiler Info -------------

  C compiler:           gcc -g -O2

  C++ compiler:         g++ -g -O2

  SQL preprocessor:     /usr/bin/cpp -w -traditional-cpp -P

 -------------- Dependencies --------------

  GEOS config:          /opt/geos-3.4.2/bin/geos-config

  GEOS version:         3.4.2

  GDAL config:          /usr/local/bin/gdal-config

  GDAL version:         2.0.1

  PostgreSQL config:    /usr/local/pgsql/bin/pg_config

  PostgreSQL version:   PostgreSQL 9.6.3

  PROJ4 version:        48

  Libxml2 config:       /usr/bin/xml2-config

  Libxml2 version:      2.7.6

  JSON-C support:       no

  PostGIS debug level:  0

  Perl:                 /usr/bin/perl

 --------------- Extensions ---------------

  PostGIS Raster:       enabled

  PostGIS Topology:     enabled

  SFCGAL support:       disabled

 -------- Documentation Generation --------

  xsltproc:             

  xsl style sheets:     

  dblatex:             

  convert:             

  mathml2.dtd:          http://www.w3.org/Math/DTD/mathml2/mathml2.dtd

编译

[root@node01 postgis-2.1.8]# make

....

PostGIS was built successfully. Ready to install.

[root@node01 postgis-2.1.8]# make install

......

make[2]: Leaving directory `/home/tools/postgis-2.1.8/extensions/postgis_topology'

make[1]: Leaving directory `/home/tools/postgis-2.1.8/extensions'

Wait for about 1min

5) 检查PostGIS是否安装成功
node01:postgres:/usr/local/pgsql:>psql

postgres=# select * from pg_available_extensions where name like 'postgis%';

          name          | default_version | installed_version |                               comment                               

------------------------+-----------------+-------------------+---------------------------------------------------------------------

 postgis                | 2.1.8           |                   | PostGIS geometry, geography, and raster spatial types and functions

 postgis_tiger_geocoder | 2.1.8           |                   | PostGIS tiger geocoder and reverse geocoder

 postgis_topology       | 2.1.8           |                   | PostGIS topology spatial types and functions

postgres=# create extension postgis;

postgres=# create extension postgis_topology;

postgres=# create extension fuzzystrmatch;

postgres=# create extension postgis_tiger_geocoder;

postgres=# \dx

                                            List of installed extensions

          Name          | Version |   Schema   |                             Description                             

------------------------+---------+------------+---------------------------------------------------------------------

fuzzystrmatch          | 1.1     | public     | determine similarities and distance between strings

plpgsql                | 1.0     | pg_catalog | PL/pgSQL procedural language

postgis                | 2.1.8   | public     | PostGIS geometry, geography, and raster spatial types and functions

postgis_tiger_geocoder | 2.1.8   | tiger      | PostGIS tiger geocoder and reverse geocoder

postgis_topology       | 2.1.8   | topology   | PostGIS topology spatial types and functions

(1)报错

(1)
[root@node01 postgis-2.1.8]# ./configure --with-pgconfig=/usr/local/pgsql/bin/pg_config --with-geosconfig=/opt/geos-3.4.2/bin/geos-config --with-projdir=/opt/proj-4.8.0/ --without-sfcgal

......

checking for xml2-config... no

configure: error: could not find xml2-config from libxml2 within the current path. You may need to try re-running configure with a --with-xml2config parameter.

解决方法:

查看是否存在xml2-config文件,结果不存在

[root@node01 postgis-2.1.8]# find / -name "xml2-config"

查看是否已经安装了libxml2和libxml2-devel

[root@node01 postgis-2.1.8]# rpm -qa |grep  libxml2

libxml2-2.7.6-14.el6.x86_64

libxml2-python-2.7.6-14.el6.x86_64

安装libxml2-devel:

[root@node01 postgis-2.1.8]# yum install libxml2-devel -y

[root@node01 postgis-2.1.8]# rpm -qa |grep  libxml2

libxml2-2.7.6-21.el6_8.1.x86_64

libxml2-python-2.7.6-21.el6_8.1.x86_64

libxml2-devel-2.7.6-21.el6_8.1.x86_64

可以看到此时已经有xml2-config文件

[root@node01 postgis-2.1.8]# find / -name "xml2-config"

/usr/bin/xml2-config

最后再执行上面的编译命令可成功。

(2)创建extension时报错

postgres=# create extension postgis;

ERROR:  could not load library "/usr/local/pgsql/lib/rtpostgis-2.1.so": libgdal.so.20: cannot open shared object file: No such file or directory

[root@node01 postgis-2.1.8]# ls -ltr /usr/local/pgsql/lib/rtpostgis-2.1.so

-rwxr-xr-x. 1 root root 1435637 Jul 12 05:44 /usr/local/pgsql/lib/rtpostgis-2.1.so

[root@node01 postgis-2.1.8]# ls -ltr /usr/local/pgsql/lib/libgdal.so.20

ls: cannot access /usr/local/pgsql/lib/libgdal.so.20: No such file or directory

[root@node01 postgis-2.1.8]# find / -name 'libgdal.so.20'

/usr/local/lib/libgdal.so.20

/home/tools/gdal-2.0.1/.libs/libgdal.so.20

[root@node01 postgis-2.1.8]# cp /usr/local/lib/libgdal.so.20 /usr/local/pgsql/lib/

参考链接为:https://blog.csdn.net/u010177412/article/details/82150207

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值