【注意】最后更新于 November 13, 2021,文中内容可能已过时,请谨慎使用。
PostgreSQL是一个开源的对象-关系数据库系统,经过十多年的开发和改进,PostgreSQL已在可靠性、稳定性上已经十分强大,并且被应用的十分广泛。
很多云原生方向的产品例如开源镜像仓库Harbor、API网关Kong都使用了PostgreSQL。本文将介绍从源码编译安装PostgreSQL 13.5的具体过程。
安装环境和准备工作
两台CentOS 7服务器如下:
1
2
|
192.168.100.151 node1
192.168.100.152 node2
|
下载PostgreSQL的源码压缩包:
1
|
curl -O -k https://ftp.postgresql.org/pub/source/v13.5/postgresql-13.5.tar.gz
|
解压缩源码:
1
|
tar -zxvf postgresql-13.5.tar.gz
|
安装编译工具及相关依赖:
1
|
yum install -y gcc bison gcc-c++ readline readline-devel zlib zlib-devel perl perl-devel systemd-devel
|
在这两台服务器上创建postgres
用户:
创建postgresql相关目录:
1
2
3
|
mkdir /home/postgres/data
mkdir /home/postgres/init
chown -R postgres:postgres /home/postgres
|
将使用/home/postgres/data
作为数据库的数据目录。
编译安装PostgreSQL
进入到源码解压缩目录:
configure执行配置准备构建环境:
1
2
3
4
5
6
7
8
|
./configure --with-pgport=5432 \
--prefix=/usr/local/pgsql \
--with-systemd \
--with-blocksize=8 \
--with-segsize=1 \
--with-wal-blocksize=8 \
--without-readline \
--datadir=/home/postgres/init
|
编译安装:
初始化数据库
执行下面的命令对数据库进行初始化,数据库的初始化需要切换到postgres
用户:
1
2
|
su postgres
/usr/local/pgsql/bin/initdb -D /home/postgres/data
|
创建systemd配置文件/etc/systemd/system/pgserver.service
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)
[Service]
Type=notify
User=postgres
ExecStart=/usr/local/pgsql/bin/postgres -D /home/postgres/data
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0
[Install]
WantedBy=multi-user.target
|
配置开机启动并启动PostgreSQL:
1
|
systemctl enable pgserver --now
|
默认使用postgres
用户可以本机登录到数据库:
1
2
3
4
5
6
|
cd /usr/local/pgsql/bin
./psql -p5432 -h127.0.0.1 -Upostgres
psql (13.5)
Type "help" for help.
postgres=#
|
修改数据库用户postgres
的密码:
连接相关配置
修改/home/postgres/data/postgresql.conf
配置文件,设置监听地址:
修改/home/postgres/data/pg_hba.conf
(host-based authentication file)配置连接方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
host all all 192.168.100.0/24 md5
|
修改配置后重启数据库服务即可:
1
|
systemctl restart pgserver
|
总结
本文在两台CentOS 7服务器上从源码编译安装了PostgreSQL,当前这两台服务器的PostgreSQL数据库是相互独立,它们之间没有任何关系。
下一节将介绍基于repmgr
的postgresql主备高可用方案,将这两台PosgreSQL服务器配置成主备高可用架构。
参考