PostgreSQL是一个开源的对象-关系数据库系统,经过十多年的开发和改进,PostgreSQL已在可靠性、稳定性上已经十分强大,并且被应用的十分广泛。 很多云原生方向的产品例如开源镜像仓库Harbor、API网关Kong都使用了PostgreSQL。本文将介绍从源码编译安装PostgreSQL 13.5的具体过程。
安装环境和准备工作
两台CentOS 7服务器如下:
192.168.100.151 node1
192.168.100.152 node2下载PostgreSQL的源码压缩包:
curl -O -k https://ftp.postgresql.org/pub/source/v13.5/postgresql-13.5.tar.gz解压缩源码:
tar -zxvf postgresql-13.5.tar.gz安装编译工具及相关依赖:
yum install -y gcc bison gcc-c++ readline readline-devel zlib zlib-devel perl perl-devel systemd-devel在这两台服务器上创建postgres用户:
useradd postgres创建postgresql相关目录:
mkdir /home/postgres/data
mkdir /home/postgres/init
chown -R postgres:postgres /home/postgres将使用/home/postgres/data作为数据库的数据目录。
编译安装PostgreSQL
进入到源码解压缩目录:
cd postgresql-13.5configure执行配置准备构建环境:
./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编译安装:
make
make install初始化数据库
执行下面的命令对数据库进行初始化,数据库的初始化需要切换到postgres用户:
su postgres
/usr/local/pgsql/bin/initdb -D /home/postgres/data创建systemd配置文件/etc/systemd/system/pgserver.service:
[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:
systemctl enable pgserver --now默认使用postgres用户可以本机登录到数据库:
cd /usr/local/pgsql/bin
./psql -p5432 -h127.0.0.1 -Upostgres
psql (13.5)
Type "help" for help.
postgres=# 修改数据库用户postgres的密码:
postgres=# \password连接相关配置
修改/home/postgres/data/postgresql.conf配置文件,设置监听地址:
listen_addresses = '*'修改/home/postgres/data/pg_hba.conf(host-based authentication file)配置连接方式:
# 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修改配置后重启数据库服务即可:
systemctl restart pgserver总结
本文在两台CentOS 7服务器上从源码编译安装了PostgreSQL,当前这两台服务器的PostgreSQL数据库是相互独立,它们之间没有任何关系。
下一节将介绍基于repmgr的postgresql主备高可用方案,将这两台PosgreSQL服务器配置成主备高可用架构。