CentOS7 通过源码编译离线安装、配置PostgreSQL9.5及PostGIS2.2

安装postgresql

下载源码

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

找到对应版本的 tar.gz 安装包下载拷贝到服务器

解压 安装

tar -xvf postgresql-9.5.14.tar.gz

进入解压目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 安装一个gcc编译器
yum install gcc

# 安装make
yum install make

# 查看系统自带readline包
rpm -qa|grep readline

# 通过yum search readline进行搜索可以发现一个readline-devel包
yum search readline

# 安装readline-devel包
yum install readline-devel

# 安装zlib-devel包
yum install zlib-devel

# 配置安装信息 --prefix 安装目录
./configure --prefix=/usr/local/postgresql

# 编译
make

# 安装
make install

用户权限与环境变量

编译安装成功后,接下来要做的就是创建一个普通用户,因为默认超级用户(root)不能启动postgresql,所以需要创建一个普通用户来启动数据库,执行以下命令创建用户:

1
useradd postgres

接下来需要设置权限,将postgres的数据目录全部赋权给postgres用户(此处我将postgres的数据目录指定在在/usr/local/postgresql/data目录下):

1
chown -R postgres:postgres /usr/local/postgresql/

最后为了方便起见设置一下相关的环境变量,此处仅仅设置postgres用户的环境变量,所以首先通过 su - postgres 切换到postgres用户,打开 .bash_profile 文件并追加以下内容:

1
2
3
4
5
6
PGHOME=/usr/local/postgresql
export PGHOME
PGDATA=/usr/local/postgresql/data
export PGDATA
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin
export PATH

修改完成后可以通过 source ./.bash_profile 命令使其立即生效,接下来检验一下环境变量是否设置正确,切换任意目录输入 which psql 以及 psql -V 即可分别查看psql客户端的路径以及postgresql的数据库版本,如下:

1
2
3
4
5
[postgres@localhost ~]$ source ./.bash_profile
[postgres@localhost ~]$ which psql
/usr/local/postgresql/bin/psql
[postgres@localhost ~]$ psql -V
psql (PostgreSQL) 9.5.14

一切准备就绪之后接下来就可以初始化数据库了。

初始化数据库

由于配置了环境变量,所以此处我们直接执行initdb即可完成db初始化:

1
initdb

看到如下信息就说明初始化成功了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[postgres@localhost ~]$ initdb
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 "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /usr/local/postgresql/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
creating template1 database in /usr/local/postgresql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... 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.

Success. You can now start the database server using:

pg_ctl -D /usr/local/postgresql/data -l logfile start

同时在postgresql的目录可以看到生成的数据目录data以及该目录的相关数据和配置文件:

1
2
3
4
5
6
7
[postgres@localhost ~]$ ls /usr/local/postgresql/
bin data include lib share
[postgres@localhost ~]$ ls /usr/local/postgresql/data/
base pg_dynshmem pg_multixact pg_snapshots pg_tblspc postgresql.auto.conf
global pg_hba.conf pg_notify pg_stat pg_twophase postgresql.conf
pg_clog pg_ident.conf pg_replslot pg_stat_tmp PG_VERSION
pg_commit_ts pg_logical pg_serial pg_subtrans pg_xlog

如上图,base目录是表空间目录,global目录是相关全局变量的目录,pg_hba.conf和postgresql.conf在之前的博客也都提及了,一个是访问控制配置(127.0.0.1改为信任的客户端ip网段使其可以远程访问),一个是postgresql主配置文件(listen_address=’localhost’改为’*’星号使其监听整个网络),方便起见我这里将pg_hba.conf的ip地址修改为 0.0.0.0/0 ,而加密方式改为 md5 ,就表示需要密码访问,算是提供一个最低级的安全防护:

1
2
3
4
5
[postgres@localhost data]$ cd /usr/local/postgresql/data
[postgres@localhost data]$ vim pg_hba.conf
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5

而postgresql.conf就像上面说的那样修改一下listen_address使其监听整个网络即可:

1
2
[postgres@localhost data]$ vim postgresql.conf
修改#listen_addresses = 'localhost' 为 listen_addresses='*'

最后别忘记开放防火墙pg的5432端口,否则即使做了上面两处修改客户端依然无法连接postgresql,所以将5432端口加入到zone即可,依次运行以下命令(注意此处需要切回root用户,否则没有权限):

1
2
3
4
# 将5432端口加入防火墙白名单
firewall-cmd --zone=public --add-port=5432/tcp --permanent
# 重启防火墙使修改生效
firewall-cmd --reload

至此就配置完毕了,还可以通过 firewall-cmd --zone=public --list-ports 来查看已打开的端口列表再次确认一下,看到如下提示则说明已成功开启端口:

1
2
[root@localhost ~]# firewall-cmd --zone=public --list-ports
5432/tcp

至此配置相关的内容就全部完成了,最后就是启动并连接数据库了。

启动和连接

在初始化数据库结束时我们已经看到了启动命令,如下:

Success. You can now start the database server using:
pg_ctl -D /usr/local/postgresql/data -l logfile start

由于我们设置了环境变量,所以已经指定了数据目录PGDATA, -l 表示日志文件目录,通常需要指定,所以我们在/usr/local/postgresql根目录下再创建一个log目录用来存放日志文件(注意别忘记赋予可写的权限),
最后运行 pg_ctl start -l /usr/local/postgresql/log/pg_server.log 即可启动数据库,看到如下提示就说明启动成功了:

1
2
3
4
5
[root@localhost ~]# su - postgres
[postgres@localhost ~]$ mkdir /usr/local/postgresql/log
[postgres@localhost ~]$ chmod -R 777 /usr/local/postgresql/log
[postgres@localhost ~]$ pg_ctl start -l /usr/local/postgresql/log/pg_server.log
server starting

或者通过 ps -ef|grep postgres 查看一下postgres相关是否存在相关进程,如下图也可以说明已启动成功:

1
2
3
4
5
6
7
8
[postgres@localhost ~]$ ps -ef|grep postgres
postgres 21649 21648 0 14:20 pts/0 00:00:00 -bash
postgres 21674 1 0 14:23 pts/0 00:00:00 /usr/local/postgresql/bin/postgres
postgres 21676 21674 0 14:23 ? 00:00:00 postgres: checkpointer process
postgres 21677 21674 0 14:23 ? 00:00:00 postgres: writer process
postgres 21678 21674 0 14:23 ? 00:00:00 postgres: wal writer process
postgres 21679 21674 0 14:23 ? 00:00:00 postgres: autovacuum launcher process
postgres 21680 21674 0 14:23 ? 00:00:00 postgres: stats collector process

而在日志文件目录中可以看到数据库日志文件以及刚才的启动日志:

1
2
3
4
5
[postgres@localhost ~]$ cat /usr/local/postgresql/log/pg_server.log 
LOG: database system was shut down at 2020-10-23 13:56:22 CST
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started

启动成功后我们就可以通过postgresql自带的客户端工具 psql 来进行连接,直接输入 psql 看到版本信息则说明连接成功:

1
2
3
4
5
[postgres@localhost ~]$ psql
psql (9.5.14)
Type "help" for help.

postgres=#
1
2
3
4
5
6
# 启动服务
pg_ctl start -l /usr/local/postgresql/log/pg_server.log
# 停止服务
pg_ctl stop
# 重启服务
pg_ctl restart -l /usr/local/postgresql/log/pg_server.log

设置开机启动

首先要做的第一步就是找到pg的启动脚本示例,在pg的源码目录下的 contrib/start-scriptslinux 文件,将它拷贝一份到 /etc/init.d 目录下并重命名为postgresql:

1
cp /usr/local/postgresql/postgresql-9.5.14/contrib/start-scripts/linux /etc/init.d/postgresql

接下来修改脚本中的参数项,如下:

1
2
3
4
5
6
7
8
9
10
11
# Installation prefix pg主目录
prefix=/usr/local/postgresql

# Data directory pg的data目录
PGDATA="/usr/local/postgresql/data"

# Who to run the postmaster as, usually "postgres". (NOT "root") pg用户,切记不能用root,root用户无法启动pg数据库
PGUSER=postgres

# Where to keep a log file pg日志文件
PGLOG="$prefix/log/pg_server.log"

然后为所有用户添加改脚本的可执行权限:

1
chmod a+x /etc/init.d/postgresql

最后用root用户通过chkconfig命令将该脚本注册为开机启动即可:

1
chkconfig --add postgresql

接下来要做的第一件事就是设置postgres用户的密码(默认为空),用psql连接成功后直接输入 \password 即会提示输入两次密码,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
postgres=# \q
[postgres@localhost ~]$ psql
psql (9.5.14)
Type "help" for help.

postgres=# \password
Enter new password:
Enter it again:
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)

此处我们暂且将密码修改为123456,通过 \l 命令即可查看数据库列表,然后输出 \q ,关于psql工具的使用在此处就不再做过多赘述。最后也是最重要的一点就是验证非本地客户端工具的连接了,毕竟我们是要做数据库服务器的,这里我选择的工具是Navicat Premium,在主机(Windows10)打开Navicat与虚拟机中的postgresql服务器进行连接测试,结果可以连接成功,同理停止数据库可以使用命令 pg_ctl stop 来关闭postgresql服务,很简单,至此关于源码编译安装postgresql就已经全部结束了。

安装PostGIS

要安装postgis需要先安装geos、libxml2、proj4、gdal、json-c这几个包我们就来一步一步的弄吧:

proj4

下载地址:http://proj4.org/download.html,选择proj4-4.9.2.tar.gz

1
wget https://download.osgeo.org/proj/proj-4.9.2.tar.gz

解压源码包

1
proj-4.9.2.tar.gz

进入解压后的目录

1
cd proj-4.9.2

配置安装路径

1
./configure --prefix=/usr/local/proj-4.9.2

编译并安装

1
make && make install

geos

下载地址:http://trac.osgeo.org/geos,选择geos-3.5.1.tar.bz2

安装步骤与上述 proj4 一样,这里需要注意,geos-3.5.1.tar.bz2 包类型为 bzip2 ,需要安装解压工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 下载
wget http://download.osgeo.org/geos/geos-3.5.1.tar.bz2
# 安装 bzip2 解压工具
yum install bzip2
# 解压
tar -jxvf geos-3.5.1.tar.bz2
# 进入目录
cd geos-3.5.1
# 配置安装路径
./configure --prefix=/usr/local/geos-3.5.1
# 安装 gcc-c++
yum -y install gcc-c++.x86_64
# 编译并安装
make && make install

libxml2

下载地址 : http://xmlsoft.org/sources/或ftp://xmlsoft.org/libxml2,选择libxml2-2.9.0.tar.gz

1
2
3
4
5
6
7
8
9
10
# 下载
wget http://xmlsoft.org/sources/libxml2-2.9.0.tar.gz
# 解压
tar -zxvf libxml2-2.9.0.tar.gz
# 进入目录
cd libxml2-2.9.0
# 配置安装路径
./configure --prefix=/usr/local/libxml2-2.9.0
# 编译并安装
make && make install

gdal

下载地址:http://trac.osgeo.org/gdal/wiki/DownloadSource,选择gdal-1.10.0.tar.gz

1
2
3
4
5
6
7
8
9
10
# 下载
wget http://download.osgeo.org/gdal/1.10.0/gdal-1.10.0.tar.gz
# 解压
tar -zxvf gdal-1.10.0.tar.gz
# 进入目录
cd gdal-1.10.0
# 配置安装路径
./configure --prefix=/usr/local/gdal-1.10.0
# 编译并安装
make && make install

json-c

下载地址:https://s3.amazonaws.com/json-c_releases/releases/json-c-0.12.1.tar.gz

1
2
3
4
5
6
7
8
9
10
# 下载
wget https://s3.amazonaws.com/json-c_releases/releases/json-c-0.12.1.tar.gz
# 解压
tar -zxvf json-c-0.12.1.tar.gz
# 进入目录
cd json-c-0.12.1
# 配置安装路径
./configure --prefix=/usr/local/json-c-0.12.1
# 编译并安装
make && make install

PostGIS

下载地址:http://download.osgeo.org/postgis/source/ ,选择postgis-2.2.4.tar.gz

1
2
3
4
5
6
7
8
9
10
# 下载
wget http://download.osgeo.org/postgis/source/postgis-2.2.4.tar.gz
# 解压
tar -zxvf postgis-2.2.4.tar.gz
# 进入目录
cd postgis-2.2.4
# 配置安装路径
./configure --with-pgconfig=/usr/local/postgresql/bin/pg_config --with-xml2config=/usr/local/libxml2-2.9.0/bin/xml2-config --with-geosconfig=/usr/local/geos-3.5.1/bin/geos-config --with-projdir=/usr/local/proj-4.9.2 --with-gdalconfig=/usr/local/gdal-1.10.0/bin/gdal-config
# 编译并安装
make && make install

至此PostGIS已经安装完成,测试创建postgis扩展一切正常。

参考地址: https://blog.csdn.net/wlwlwlwl015/article/details/53256358

文章作者: 晓风残月
文章链接: https://blog.shofcy.cn/2020/10/23/PostgreSQLOfflineInstall/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 晓风残月Shofcy
打赏
  • 微信
  • 支付宝

评论