发现生产环境有用rpm安装,故整理安装rpm安装步骤,目的是准备walminer恢复数据用的环境
https://download.postgresql.org/pub/repos/yum/ 含多个版本
https://download.postgresql.org/pub/repos/yum/14/redhat/rhel-7-x86_64/ 仅14版本
postgresql14-server-14.8-1PGDG.rhel7.x86_64.rpm
postgresql14-libs-14.8-1PGDG.rhel7.x86_64.rpm
postgresql14-14.8-1PGDG.rhel7.x86_64.rpm
postgresql14-contrib-14.8-1PGDG.rhel7.x86_64.rpm
环境准备需要使用root用户进行操作。本文档以CentOS7.9发行版操作系统为例,命令如下。如若是其他的linux发行版,准备工作这一块内容操作步骤是差不多的,linux命令需要调整。
--上传操作系统镜像到/opt目录
[root@localhost ~]# ls -l /opt | grep Cen*
-rw-r--r--. 1 root root 4712300544 Aug 8 05:27 CentOS-7-x86_64-DVD-2009.iso
--挂载镜像
[root@localhost ~]# mount /opt/CentOS-7-x86_64-DVD-2009.iso /mnt/
[root@localhost ~]# df -h | grep mnt
/dev/loop0 4.4G 4.4G 0 100% /mnt
--设置开机挂载
cat << EOF >> /etc/fstab
/dev/loop0 /mnt iso9660 loop 0 0
EOF
--配置本地yum源文件
mkdir -p /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
cat >> /etc/yum.repos.d/os.repo <<"EOF"
[OS1]
name=OS
baseurl=file:///mnt
enabled=1
gpgcheck=0
EOF
首先需要安装一些必要的依赖,PostgreSQL安装的过程需要使用到这些linux包。
yum install libxslt libicu python36-libs python2-libs libperl.so
禁用和启用二选一
--禁用防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
--启用防火墙
firewall-cmd --zone=public --add-port=15400/tcp --permanent
firewall-cmd --zone=public --add-port=22/tcp --permanent
firewall-cmd --reload
--禁用防火墙区域偏移
sed -i 's/^AllowZoneDrifting=yes/AllowZoneDrifting=no/' /etc/firewalld/firewalld.conf
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
setenforce 0
cat >> /etc/security/limits.conf << "EOF"
#add by postgres
postgres soft nproc 65536
postgres hard nproc 65536
postgres soft nofile 65536
postgres hard nofile 65536
postgres soft stack 1024000
postgres hard stack 1024000
EOF
该配置在关闭linux访问终端session重新登录之后生效,ulimit -n的值会变成65535 。进行这一步操作的目的是防止linux操作系统内打开文件句柄数量的限制,避免不必要的故障。
echo deadline > /sys/block/sda/queue/scheduler
/sbin/blockdev --setra 8192 /dev/sda
/sbin/blockdev --getra /dev/sda
cp /etc/sysctl.conf /etc/sysctl.confbak
cat >> /etc/sysctl.conf <
#CentOS openEuler操作系统默认为关闭,可以跳过该步骤
sed -i 's/#RemoveIPC=no/RemoveIPC=no/g' /etc/systemd/logind.conf
systemctl daemon-reload
systemctl restart systemd-logind
cd /opt
rpm -ivh *.rpm
*会自动处理rpm安装顺序
rpm安装方式会自动创建postgres用户
二进制文件位置:/usr/pgsql-14/bin
如果采用默认数据目录,该步骤忽略
mkdir -p /pgdata
chown -R postgres:postgres /pgdata
chmod -R 755 /pgdata
根据情况,选择以下任意一种
su - postgres
cd /usr/pgsql-14/bin
./initdb
su - postgres
cd /usr/pgsql-14/bin
./initdb
--username=postgres \
-D /pgdata \
--encoding=UTF8 \
--lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8
参数说明:
--lc-collate: 字符串排序的顺序
--lc-ctype:字符分类
数据库里会创建1个和操作系统用户fuwa同名的用户,该数据库用户fuwa是超级用户。假如创建扩展需要切换的超级用户是fuwa,而不是postgres
--赋予临时目录权限
su - root
chmod -R 777 /var/run/postgresql
--创建用户
groupadd -g 5001 fuwa
useradd -G fuwa -g 5001 fuwa
echo "fuwa"|passwd --stdin fuwa
--初始化实例
su - fuwa
/usr/pgsql-14/bin/initdb -D /home/fuwa/data
--启库
su - fuwa
/usr/pgsql-14/bin/pg_ctl -D /home/fuwa/data start
--登录(需指定postgres用户,不然提示fuwa数据库不存在)
[fuwa@localhost log]$ /usr/pgsql-14/bin/psql postgres
--查看用户/模式/数据库(注意和postgres操作用户初始化实例的区别)
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
fuwa | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
postgres=# \dn
List of schemas
Name | Owner
--------+-------
public | fuwa
(1 row)
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
postgres | fuwa | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | fuwa | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/fuwa +
| | | | | fuwa=CTc/fuwa
template1 | fuwa | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/fuwa +
| | | | | fuwa=CTc/fuwa
(3 rows)
--切换用户(注意和postgres操作用户初始化实例的区别)
mix_db=> \c mix_db postgres
connection to server on socket "/var/run/postgresql/.s.PGSQL.5434" failed: FATAL: role "postgres" does not exist
Previous connection kept
mix_db=> \c mix_db fuwa
You are now connected to database "mix_db" as user "fuwa".
开机自启系统服务文件位置:/usr/lib/systemd/system/postgresql-14.service
systemctl start postgresql-14.service
systemctl status postgresql-14.service
--1.更改开机自启系统服务文件中数据目录位置
vi /usr/lib/systemd/system/postgresql-14.service
Environment=PGDATA=/var/lib/pgsql/14/data/ 替换为自定义数据目录
--2.启动
systemctl start postgresql-14.service
systemctl status postgresql-14.service
--启动命令
pg_ctl start -D /var/lib/pgsql/14/data/
--重启命令
pg_ctl restart -D /var/lib/pgsql/14/data/
--查看数据库运行状态
pg_ctl status -D /var/lib/pgsql/14/data/
--停止数据库
pg_ctl stop -D /var/lib/pgsql/14/data/
echo "##postgres user env configuration" >> /var/lib/pgsql/.bash_profile
cp /var/lib/pgsql/.bash_profile /var/lib/pgsql/.bash_profile
sed -i 's/^export PATH/#export PATH/' /var/lib/pgsql/.bash_profile
echo "#add by postgres" >> /var/lib/pgsql/.bash_profile
echo 'export PGHOME=/usr/pgsql-14' >> /var/lib/pgsql/.bash_profile
echo 'export PGPORT=5432' >> /var/lib/pgsql/.bash_profile
echo 'export PATH=$PGHOME/bin:$PATH' >> /var/lib/pgsql/.bash_profile
echo 'export MANPATH=$PGHOME/share/man:$MANPATH' >> /var/lib/pgsql/.bash_profile
echo 'export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH' >> /var/lib/pgsql/.bash_profile
echo 'export LANG="en_US.UTF-8"' >> /var/lib/pgsql/.bash_profile
echo 'export DATE=`date +"%Y%m%d%H%M"`' >> /var/lib/pgsql/.bash_profile
source /var/lib/pgsql/.bash_profile
配置文件:
位置:数据目录PGDATA目录
postgresql.conf --数据库参数
pg_hba.conf --用户访问权限文件
--创建归档目录
mkdir -p /var/lib/pgsql/14/pg_archive
--更改配置文件
cd $PGDATA
cp postgresql.conf postgresql.confbak
sed -i "/^#listen_addresses = 'localhost'/s/#listen_addresses = 'localhost'/listen_addresses = '*'/" postgresql.conf
sed -i "s/^#port = 5432/port = 5432/" postgresql.conf
sed -i 's/max_connections = 100/max_connections = 500/' postgresql.conf
sed -i "/^#wal_level/s/^#//" postgresql.conf #去掉注释
sed -i 's/#archive_mode = off/archive_mode = on/' postgresql.conf
sed -i "/^#archive_command = ''/s/#archive_command = ''/archive_command ='\/usr\/bin\/lz4 -q -z %p \/var\/lib\/pgsql\/14\/pg_archive/%f.lz4'/" postgresql.conf #-q取消警告-z强制压缩
sed -i "/^#log_destination = 'stderr'/s/#log_destination = 'stderr'/log_destination = 'csvlog'/" postgresql.conf
sed -i "/^#logging_collector = off/s/#logging_collector = off/logging_collector = on/" postgresql.conf
sed -i "/^#log_directory = 'log'/s/^#//" postgresql.conf #去掉注释
sed -i "/^#log_filename/s/^#//" postgresql.conf #去掉注释
sed -i "/^#log_file_mode/s/^#//" postgresql.conf #去掉注释
sed -i "/^#log_rotation_age/s/^#//" postgresql.conf #去掉注释
sed -i "/^#log_rotation_size/s/^#//" postgresql.conf #去掉注释
sed -i "/^shared_buffers = 128MB/s/shared_buffers = 128MB/shared_buffers = 1024MB/" postgresql.conf #物理内存25~40%
sed -i "/^#work_mem = 4MB/s/#work_mem = 4MB/work_mem = 30MB/" postgresql.conf
sed -i "/^#maintenance_work_mem = 64MB/s/#maintenance_work_mem = 64MB/maintenance_work_mem = 256MB/" postgresql.conf
sed -i "/^#temp_buffers = 8MB/s/#temp_buffers = 8MB/temp_buffers = 256MB/" postgresql.conf
max_connections = 100
shared_buffers =1GB
cd $PGDATA
cp pg_hba.conf pg_hba.confbak
cat > pg_hba.conf << EOF
# TYPE DATABASE USER ADDRESS METHOD
local all all trust
host all all 0.0.0.0/0 md5
EOF
第一行的内容表示local本机,all所有用户可以访问postgre的所有数据库,并且密码发送方式不需要加密(trust)。
第二行的内容用于远程访问,指定了可以访问postgreSql数据库的远程用户的ip范围, 0.0.0.0/0 表示所有ip都可以。如果你希望指定ip段,可以像这样去配置192.168.3.1/24(表示的ip范围是:192.168.3.1到192.168.3.255)。md5表示数据库访问密码使用md5的加密方式发送。
视情况执行如下脚本
--初始化postgres用户密码
alter user postgres with password '你设置的密码';
--创建新用户
create user top_sjjs with encrypted password 'top_sjjs';
alter user top_sjjs with superuser;
--创建数据库
create database mix_db owner top_sjjs;
grant all privileges on database mix_db to top_sjjs;
grant all privileges on all tables in schema public to top_sjjs;
--创建程序用户,用于操作数据库表里面的数据,完成增删改查
create user top_sjjs with encrypted password 'top_sjjs';
GRANT update,delete,insert,select ON ALL TABLES IN SCHEMA public TO top_sjjs;
包含配置归档参数,默认开启归档。
--更改配置文件
cp /pgdb/data/postgresql.conf /pgdb/data/postgresql.confbak
sed -i "/^#listen_addresses = 'localhost'/s/#listen_addresses = 'localhost'/listen_addresses = '*'/" /pgdb/data/postgresql.conf
sed -i "s/^#port = 5432/port = 5432/" /pgdb/data/postgresql.conf
sed -i 's/max_connections = 100/max_connections = 500/' /pgdb/data/postgresql.conf
sed -i "/^#wal_level/s/^#//" /pgdb/data/postgresql.conf #去掉注释
sed -i 's/#archive_mode = off/archive_mode = on/' /pgdb/data/postgresql.conf
sed -i "/^#archive_command = ''/s/#archive_command = ''/archive_command ='\/usr\/bin\/lz4 -q -z %p \/pgdb\/pg_archive\/%f.lz4'/" /pgdb/data/postgresql.conf #-q取消警告-z强制压缩
sed -i "/^#log_destination = 'stderr'/s/#log_destination = 'stderr'/log_destination = 'csvlog'/" /pgdb/data/postgresql.conf
sed -i "/^#logging_collector = off/s/#logging_collector = off/logging_collector = on/" /pgdb/data/postgresql.conf
sed -i "/^#log_directory = 'log'/s/^#//" /pgdb/data/postgresql.conf #去掉注释
sed -i "/^#log_filename/s/^#//" /pgdb/data/postgresql.conf #去掉注释
sed -i "/^#log_file_mode/s/^#//" /pgdb/data/postgresql.conf #去掉注释
sed -i "/^#log_rotation_age/s/^#//" /pgdb/data/postgresql.conf #去掉注释
sed -i "/^#log_rotation_size/s/^#//" /pgdb/data/postgresql.conf #去掉注释
sed -i "/^shared_buffers = 128MB/s/shared_buffers = 128MB/shared_buffers = 1024MB/" /pgdb/data/postgresql.conf #物理内存25~40%
sed -i "/^#work_mem = 4MB/s/#work_mem = 4MB/work_mem = 30MB/" /pgdb/data/postgresql.conf
sed -i "/^#maintenance_work_mem = 64MB/s/#maintenance_work_mem = 64MB/maintenance_work_mem = 256MB/" /pgdb/data/postgresql.conf
sed -i "/^#temp_buffers = 8MB/s/#temp_buffers = 8MB/temp_buffers = 256MB/" /pgdb/data/postgresql.conf
--重启数据库
systemctl restart postgres.service
/pgdb/pgsql/bin/psql -Upostgres -W -d postgres -h127.0.0.1 -p5432 -c "select pg_switch_wal();"
cat >> /var/spool/cron/postgres << "EOF"
# PostgresBegin
#设置归档策略:每晚零点10分删除7天前归档文件,视情况更改成1个月
10 00 * * * find /var/lib/pgsql/14/pg_archive -type f -name "0000000*" -mtime +7 -exec rm {} \; > /dev/null 2>&1
#设置日志保留2天,视情况更改成半年或3个月
00 01 * * * find /var/lib/pgsql/14/data/log -type f -name "postgresql*.log" -mtime +7 -exec rm {} \; > /dev/null 2>&1
00 01 * * * find /var/lib/pgsql/14/data/log -type f -name "postgresql*.csv" -mtime +7 -exec rm {} \; > /dev/null 2>&1
EOF
--问题描述
[root@localhost opt]# ls -l
total 8064
-rw-r--r--. 1 root root 1563508 Oct 22 09:09 postgresql14-14.8-1PGDG.rhel7.x86_64.rpm
-rw-r--r--. 1 root root 704024 Oct 22 09:03 postgresql14-contrib-14.8-1PGDG.rhel7.x86_64.rpm
-rw-r--r--. 1 root root 277080 Oct 22 09:03 postgresql14-libs-14.8-1PGDG.rhel7.x86_64.rpm
-rw-r--r--. 1 root root 5707012 Oct 22 09:03 postgresql14-server-14.8-1PGDG.rhel7.x86_64.rpm
[root@localhost opt]# rpm -ivh *.rpm
warning: postgresql14-14.8-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
error: Failed dependencies:
libicu is needed by postgresql14-14.8-1PGDG.rhel7.x86_64
libperl.so()(64bit) is needed by postgresql14-contrib-14.8-1PGDG.rhel7.x86_64
libpython3.6m.so.1.0()(64bit) is needed by postgresql14-contrib-14.8-1PGDG.rhel7.x86_64
libicui18n.so.50()(64bit) is needed by postgresql14-server-14.8-1PGDG.rhel7.x86_64
libicuuc.so.50()(64bit) is needed by postgresql14-server-14.8-1PGDG.rhel7.x86_64
--解决办法:
yum install libxslt libicu python36-libs python2-libs libperl.so
场景:以非root用户、postgres用户初始化实例
--创建用户
groupadd -g 5001 fuwa
useradd -G fuwa -g 5001 fuwa
echo "fuwa"|passwd --stdin fuwa
--初始化实例
[fuwa@localhost bin]$ /usr/pgsql-14/bin/initdb -D /home/fuwa/data
--启库
[fuwa@localhost log]$ /usr/pgsql-14/bin/pg_ctl -D /home/fuwa/data start
waiting for server to start....2023-10-22 12:54:58.947 CST [12172] LOG: redirecting log output to logging collector process
2023-10-22 12:54:58.947 CST [12172] HINT: Future log output will appear in directory "log".
stopped waiting
pg_ctl: could not start server
Examine the log output.
启库失败,查看日志提示
--问题描述
[fuwa@localhost log]$ /usr/pgsql-14/bin/pg_ctl -D /home/fuwa/data start
[fuwa@localhost log]$ tail -300f postgresql-Sun.log
2023-10-22 12:54:58.959 CST [12172] FATAL: could not create lock file "/var/run/postgresql/.s.PGSQL.5434.lock": Permission denied
2023-10-22 12:54:58.961 CST [12172] LOG: database system is shut down
解决办法
[root@localhost lib]# chmod -R 777 /var/run/postgresql