CentOS7.5安装PostgreSQL操作系统配置脚本

#!/bin/bash
# Created at 2021/02/22
# Author CZH
# This Script used to set postgresql environment.
# reference : https://github.com/digoal/blog/blob/master/201608/20160803_01.md
# reference : https://github.com/digoal/blog/blob/master/201611/20161121_01.md
#
#   Scripts Summary
#   1.func_check_root: run as root
#   2.func_set_hostname: set hostname or not.
#   3.func_inst_pack: install os packages.
#   4.func_disable_selinux: disable selinux
#   5.func_set_sysctl: set os kernel parameters.
#   6.func_set_dirty: set write disk ratio.
#   7.func_set_limits: set user open file and stack limits.
#   8.func_os_other: disk scheduler and disable THP. 
#   9.func_set_datadir: mkdir data directory and user env.
# Exit this shell when any error occur.
set -e
# Configure Global variables.
v_curdate=`date +%Y%m%d_%H%M%S`
v_memtotal=`cat /proc/meminfo |grep MemTotal|awk '{print $2}'`
# check current user is root or not.
function func_check_root()
{
if [ ${UID} -ne 0 ]
    then
        echo -e "\nPlease use root run this scripts!\n"
        exit 0
    else
        echo -e "\n>>>>>>>> This script will run as root ! <<<<<<<<\n"
fi
        echo -e "\nfunc func_check_root execute Successful!\n"
}
# setting hostname
function func_set_hostname()
{
read -p "Are you want set hostname ?(y/n)" v_set_choice
v_convert_choice=$(echo ${v_set_choice} | tr [A-Z] [a-z])
if [ ${v_convert_choice} == "y" ];
    then
        read -p "Please input your hostname: " v_hostname
        hostnamectl set-hostname ${v_hostname}
        echo -e "\nThe current hostname is $(hostname).\n"
elif [ ${v_convert_choice} == "n" ];
    then
        echo -e "\n Choice n , will continue.\n"
        echo -e "\nThe current hostname is $(hostname).\n"
else
        echo -e "\n Only accept input y/n !\n"
        exit 618
fi
        echo -e "\nfunc func_set_hostname execute Successful!\n"
}
# install packages
function func_inst_pack()
{ 
rpm -q bash        \
bzip2       \
zlib        \
zlib-devel    \
openssl     \
openssl-libs   \
perl        \
readline    \
readline-devel  \
sed         \
tar         \
zip  \
python  \
make    |grep -i 'not installed'|awk -F' ' '{print $2}'|xargs -r /usr/bin/yum install -y
        echo -e "\nfunc func_inst_pack execute Successful!\n"
}
# Disable SElinux
function func_disable_selinux()
{
sed -i 's/enforcing/disabled/g' /etc/selinux/config
setenforce 0
        echo -e "\nfunc func_disable_selinux execute Successful!\n"
}
# Configure os kernel parameter.
function func_set_sysctl()
{
# backup sysctl.conf
cp /etc/sysctl.conf /etc/sysctl.conf.bak.${v_curdate}
# 所有共享内存段相加大小限制(建议内存的80%)
v_shmall=`echo $(expr $(getconf _PHYS_PAGES) \* 8 / 10)`
# 最大单个共享内存段大小(建议为内存一半), > 9.2的版本已大幅降低共享内存的使用
v_shmmax=`echo $(expr $(getconf _PHYS_PAGES) / 2 \* $(getconf PAGE_SIZE))`
# configure sysctl.conf
cat >> /etc/sysctl.conf <>/etc/sysctl.conf <>/etc/sysctl.conf <> /etc/security/limits.d/20-nproc.conf <> /home/postgres/.bash_profile
export PS1='[\u@\h] (\w) \$ '
export PGPORT=${v_port}
export PGDATA=${v_datadir}
export LANG=en_US.utf8
export PGHOME=/usr/local/postgres
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib
export PATH=$PGHOME/bin:$PATH
export DATE=`date +"%Y%m%d%H%M"`
export MANPATH=$PGHOME/share/man:$MANPATH
export PGHOST=$PGDATA
export PGUSER=postgres
export PGDATABASE=postgres
alias rm='rm -i'
alias ll='ls -lh'
unalias vi
EOF
}
function func_set_datadir()
{
read -p "Do your want to make postgresql data directory ?(y/n)" v_mkdir_choice
v_convert_mkdir=$(echo ${v_mkdir_choice} | tr [A-Z] [a-z])
if [ ${v_convert_mkdir} == "y" ];
    then
        read -p 'Please input your PostgreSQL Data directory (default:(/data)): ' v_datadir
        v_datadir=${v_datadir:-/data}    # If user not input any directory,use the /data as directory.
        read -p 'Please input your PostgreSQL Port (default:(5432)): ' v_port
        v_port=${v_port:-5432}
            if [ -d ${v_datadir} ] ;
                then  echo -e "\nThe directory was exists! will continue \n"
            else  
                mkdir -p ${v_datadir}
                chown -R postgres:postgres ${v_datadir}
                chmod -R 775 ${v_datadir}
            fi
        func_set_userenv
        echo -e "\nThe data directory is ${v_datadir}.\n"
        echo -e "\nThe PostgreSQL port is ${v_port}.\n"  
elif [ ${v_convert_mkdir} == "n" ];
    then
        func_set_userenv
else
        echo -e "\n Only accept input y/n !\n"
        exit 618
fi
        echo -e "\nfunc func_set_datadir execute Successful!\n"
}
func_set_hostname
func_check_root
func_inst_pack
func_disable_selinux
func_set_sysctl
func_set_dirty
func_set_limits
func_os_other
func_set_datadir
echo -e "\n>>>>>>>> The operating system setup is finished, please reboot! <<<<<<<<\n"
请使用浏览器的分享功能分享到微信等