前言
安装环境
1.操作系统:centos 7.9
2.需要配置本地yum源,参考如下说明
https://blog.csdn.net/dishugj/article/details/156936729
安装前准备
1.#创建目录并赋权
mkdir /u01/soft/
mkdir /u01/app/postgre16.11
mkdir /u01/app/postgre16.11/pgdata
chown -R postgres:postgres /u01
chmod -R 775 /u01
2.创建用户和用户组
创建用户组postgres并创建用户postgres
groupadd postgres
useradd -g postgres postgres
id postgres uid=1001(postgres) gid=1001(postgres) groups=1001(postgres)
3.关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
关闭selinuX
vim /etc/sysconfig/selinux
SELINUX=disabled
4.检查NTP服务(集群必须检查,单机可以不检查)
一、安装依赖:
yum install -y bison flex readline-devel zlib zlib-devel gcc openssl-devel Iibicu-devel
#若上面安装依赖不能满足,可选择性安装下列依赖(一般上述依赖即可,无需 下列依赖)
yum install -y gcc gcc-c++ epel-release Ilvm5.0 Ilvm5.0-devel clang libicu-devel perl-ExtUtils-Embed zlib-devel openssl openssl-devel pam-devel libxml2-devel libxslt-devel openldap-devel systemd-devel tcl-devel python-deve! yum install -y Ilvm5.0 Ilvm5.0-devel T
二、生产内网环境需要下载源代码上传到linux系统服务器
https://www.postgresql.org/ftp/source/
#互联网环境通过 wget 方式获取源码包,本文以 postgresql-16. 1 为例进行实验
#切换目录
cd /u01/soft/
#以root 执行下面步骤
wget https://ftp. postgresql.org/pub/source/v16. 1/postgresql-16. 1. tar. gz
tar -vxf postgresql-16. 1.tar. gz
cd postgresql-16.1
tar -vxf postgresql-16. 1.tar. gz cd postgresql-16.1
chmod +x configure
./configure --help
./configure --prefix=/u01/app/postgre16.11 --with-pgport=1521 --with-blocksize=8 --with-wal-blocksize=8 --with-segsize=5
参数说明: #数据块按 8K 划分(--with-blocksize,单位为K,默认为 8K)
#WAL块尺寸按 8k 划分(--with-wal-blocksize,单位为 K,默认为 8K)
#数据文件按 5G 一个切分(--with-segsize,单位为 G,默认为 1G)
#--prefix指定软件的安装路径
三、编译安装
make (以root运行)
.........
make install (以root运行)
.........
去软件安装目录/u01/app/postgre16.11 查看是否生产下列 4 个文件,判断安装是否成功。
root@localhost postgre16.11\]# ll total 12 drwxr-xr-x. 2 root root 4096 Apr 20 23:26 bin drwxr-xr-x. 4 root root 4096 Apr 20 23:26 include drwxr-xr-x. 4 root root 4096 Apr 20 23:26 lib drwxr-xr-x. 3 root root 24 Apr 20 23:26 share ## 四、配置环境变量 设置postgres 用户的环境变量(环境变量一定要设置正确了,否则初始化数 居库的时候会失败 su - postgres vi \~/.bash_profi le 添加内容: export PGPORT=1521 export PGHOME=/u01/app/postgre16.11 export PGDATA=/u01/app/postgre16.11/pgdata export PG_DATA=$PGDATA export PATH=$PGHOME/bin:$PATH export MANPATH=$PGHOME/share/man:$MANPATH export LANG=en_US.UTF-8 export DATE='date +"%Y%m%d%H%M"' export LD_LIBRARY_PATH=$PGHOME/1ib:$LD_LIBRARY_PATH export PGUSER=postgres export PGDATABASE=postgres cd $PGDATA 环境变量生效 source \~/.bash_profile ## 五、初始化数据库 su - postgres initdb -D $PGDATA -U postgres --locale=en_US.UTF8 -E UTF8 --wal-segsize=1024 #WAL 段尺寸按 16MB 划分(--with-wal-segsize,单位为 M,默认为 16MB),可以设置为最大1G,一般情况不调整。 ## 六、修改数据库参数 (后面有需要再修改也可以,此处不想修改可直接跳过) vim /u01/app/postgre16.11/pgdata/postgresql.conf 修改内容: listen_addresses = '\*' #监听所有用户,默认监听本地localhost port = 1521 #数据库端口号,默认 5432 #客户端访问控制文件配置 vim /u01/app/postgre16.11/pgdata /pg_ hba.conf 添加内容: host all all 0.0.0. 0/0 md5 # 允许所有用户通过MD5 认证登陆 ## 七、pg_ctl手动启动数据库 #切换到postgres_用户 su -- postgres pg_ctl -D /u01/app/postgre16.11/pgdata -l logfile start 或者 pg_ctl. start -mf #启动数据服务 pg_ctl stop -mf #关闭数据库服务 pg_ctl restart -mf #重启数据库服务 #查看pg 端口号: netstat -natplgrep post I ## 八、修改postgres数据库用户密码 su -- postgres psql #登录数据库psql客户端为postgres用户设置密码 ALTER USER postgres WITH encrypted PASSWORD 'postgres'; #执行 SQL ## 九、启动配置(可选配置) 1. # 使用 system进行管理(root): vim /usr/lib/systemd/system/postgresql-16.service 添加内容: # It's not recommended to modify this file in-place, because it will be # overwritten during package upgrades. It is recommended to use systemd # "dropin" feature; i.e. create file with suffix .conf under # /etc/systemd/system/UNITNAME.service.d directory overriding the # unit's defaults. Look at systemd.unit(5) manual page for more info. \[Unit
Description=PostgreSQL 16 database server
After=syslog.target
After=network-online.target
Service
Type=forking
User=postgres
Group=postgres
Where to send early-startup messages from the server (before the logging
options of postgresql.conf take effect)
This is normally controlled by the global default set by systemd
StandardOutput=syslog
Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
... but allow it still to be effective for child processes
(note that these settings are ignored by Postgres releases before 9.5)
Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
Environment=PG_OOM_ADJUST_VALUE=0
Environment=PGDATA=/u01/app/postgre16.11/pgdata
##ExecStartPre=/usr/libexec/postgresql-check-db-dir %N
Even though the $PGDATA variable is exported (postmaster would accept that)
use the -D option here so PGDATA content is printed by /bin/ps and by
'systemctl status'.
ExecStart=/u01/app/postgre16.11/bin/pg_ctl -D {PGDATA} -l {PGDATA}/logfile start
ExecStop=/u01/app/postgre16.11/bin/pg_ctl stop -mf
ExecReload=/u01/app/postgre16.11/bin/pg_ctl -D ${PGDATA} reload
KillMode=mixed
KillSignal=SIGINT
No artificial start/stop timeout (rhbz#1525477, pgrpms#2786).
TimeoutSec=300
TimeoutStartSec=0
TimeoutStopSec=1h
Install
WantedBy=multi-user.target
2.# 加载 system配置:
systemctl daemon-reload
3.# 使用 systemctl启动:
systemctl start postgresql-16.service
4.设置开机自启
systemctl enable postgresql-16.service
systemctl start postgresql-16.service