源码安装(生产库建议源码安装)
下载地址及官方文档
https://www.postgresql.org/ftp/source/
http://postgres.cn/docs/14/install-procedure.html
一、环境准备
1.1 系统参数修改
代码语言:javascript复制systemctl status firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
cat > /etc/sysctl.conf <<"EOF"
vm.swappiness=10
vm.zone_reclaim_mode=0
fs.aio-max-nr = 1048576
fs.file-max = 6815744
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048586
kernel.shmmax = 1288490188
kernel.shmall = 314572
kernel.shmmni = 4096
kernel.sem = 50100 64128000 50100 1280
EOF
sysctl -p
cat >> /etc/security/limits.conf <<"EOF"
* soft nofile 131072
* hard nofile 131072
* soft nproc 131072
* hard nproc 131072
* soft core unlimited
* hard core unlimited
* soft memlock 50000000
* hard memlock 50000000
EOF
echo "* - nproc unlimited" > /etc/security/limits.d/90-nproc.conf
echo "session required pam_limits.so" >> /etc/pam.d/login
# 关闭THP
root用户下
在vi /etc/rc.local最后添加如下代码
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
chmod x /etc/rc.d/rc.local
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
cat /sys/kernel/mm/transparent_hugepage/enabled
cat /sys/kernel/mm/transparent_hugepage/defrag
二、源码安装
2.1 下载源码包
代码语言:javascript复制下载源码包
wget https://ftp.postgresql.org/pub/source/v14.3/postgresql-14.3.tar.gz --no-check-certificate
2.2 安装依赖包
代码语言:javascript复制yum install -y cmake make gcc zlib zlib-devel gcc-c perl readline readline-devel
python36 tcl openssl ncurses-devel openldap pam flex
2.2 创建用户
代码语言:javascript复制groupadd postgres
useradd postgres
echo "postgres" | passwd --stdin postgres
chown postgres.postgres /home/postgres/.bash_profile
2.3 创建目录
代码语言:javascript复制-- 创建目录
mkdir -p /postgresql/{pgdata,archive,scripts,backup,pg14,soft}
chown -R postgres:postgres /postgresql
chmod -R 775 /postgresql
2.4 编译安装
代码语言:javascript复制 -- 编译
su - postgres
cd /postgresql/soft
tar zxvf postgresql-14.3.tar.gz
cd postgresql-14.3
./configure --prefix=/postgresql/pg14
make -j 16 && make install
# 编译完成,最后一行显示:All of PostgreSQL successfully made. Ready to install.
如果希望编译所有能编译的东西,包括文档(HTML和手册页)以及附加模块(contrib),这样键入:
make world -j 16 && make install-world
2.5 配置环境变量
代码语言:javascript复制-- 配置环境变量
cat >> ~/.bash_profile <<"EOF"
export PGPORT=5432
export PGDATA=/postgresql/pgdata
export PGHOME=/postgresql/pg14
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH:.
export PGHOST=$PGDATA
export PGUSER=postgres
export PGDATABASE=postgres
EOF
source ~/.bash_profile
2.6 初始化数据库
代码语言:javascript复制-- 初始化
su - postgres
/postgresql/pg14/bin/initdb -D /postgresql/pgdata -E UTF8 --locale=en_US.utf8 -U postgres
2.7 修改参数
代码语言:javascript复制 -- 修改参数
cat >> /postgresql/pgdata/postgresql.conf <<"EOF"
listen_addresses = '*'
port=5432
unix_socket_directories='/postgresql/pgdata'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%a.log'
log_truncate_on_rotation = on
EOF
cat > /postgresql/pgdata/pg_hba.conf << EOF
# TYPE DATABASE USER ADDRESS METHOD
host all all 0.0.0.0/0 md5
EOF
2.8 启动数据库
代码语言:javascript复制 -- 启动
su - postgres
pg_ctl start
pg_ctl status
pg_ctl stop
pg_ctl stop -m fast
2.9 配置系统服务
代码语言:javascript复制 -- 配置系统服务
cat > /etc/systemd/system/PG14.service <<"EOF"
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)
After=network.target
[Service]
Type=forking
User=pgsql
Group=pgsql
Environment=PGPORT=5433
Environment=PGDATA=/postgresql/pgdata
OOMScoreAdjust=-1000
ExecStart=/postgresql/pg14/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300
ExecStop=/postgresql/pg14/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/postgresql/pg14/bin/pg_ctl reload -D ${PGDATA} -s
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable PG14
systemctl start PG14
systemctl status PG14
3.0 安装插件
代码语言:javascript复制
su - postgres
psql
password postgres
or:
alter user postgres with password 'postgres';
-- 安装插件
create extension pageinspect;
create extension pg_stat_statements;
select * from pg_extension ;
select * from pg_available_extensions order by name;
无意中发现大佬写的文章 : https://www.modb.pro/db/13514 ,尽管与此文差别较多,供参考。