PostgreSQL 10~14 安装及卸载脚本(仅适用于CentOS7,其他系统未验证)

PostgreSQL 10~14 安装及卸载脚本

新建文本直接把代码复制进去,赋予755权限,直接运行。

  • 选项1:安装pgsql,默认账号密码都为postgres
  • 选项2:删除之前安装的pgsql(慎用)
  • 选项3:重置postgres账号的密码
    注意:文本格式需要为Unix(LF),防火墙及SELinux要关闭
    image
点击查看代码
#!/bin/bash

#//--------------------------------------------------------------------
#// pgsql10_14.sh
#//
#// CHANGE HISTORY
#// 2022-05-30 烽火三月 v1.00  NEW
#//
#//--------------------------------------------------------------------

#//-----------------------------------
#// SHELL VARIABLES
#//-----------------------------------
   LOG_FILE_DIR="/tmp/pgsql10_14_log"
   LOG_FILENAME="log_for_pgsql10_14"_$(date +'%Y-%m-%d-%H%M')".log"
   Log_Save_Path=$LOG_FILE_DIR/$LOG_FILENAME
  USER_HOME_DIR=${HOME}
             RC=1
		OLD_IFS=$IFS

#//-----------------------------------
#// SHELL FUNCTION
#//-----------------------------------
function log {
        echo ${USER} ":" $(date +'%Y-%m-%d %H:%M:%S: ') "$1" >> $Log_Save_Path
}

function config_backup() {
basepath=$LOG_FILE_DIR`dirname $1`
mkdir -p $basepath
command_check "cp -ax $1 $basepath"
}

function setting_config_file() {
    config_backup $3
    command_check "sed -in 's/$1/$2/' $3"
}

function command_check() {
eval $1
RC=$?
if [[ $RC -eq 0 ]]; then
    out_str="Command $1 Excute Success!"
    sw_color_out 0 "$out_str"
    log out_str
else
    out_str="Command $1 Excute Failed!"	
	sw_color_out $RC "$out_str"
    log out_str
fi
sleep 0.25
}

function read_sw() {
    case $1 in
	y|Y)
	    return 0
	    ;;
	*)
	    return 1
	    ;;
	esac
}

function install_pgsql() {
clear

sw_color_out 3 "Start Install PGSQL $1"
sleep 0.25
command_check "sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm"
wait
command_check "sudo yum install -y postgresql$1-server"
wait
command_check "sudo /usr/pgsql-$1/bin/postgresql-$1-setup initdb"
wait
setting_config_file "host.*all.*all.*127.0.0.1\\/32.*ident" "host all all 0.0.0.0\\/0 md5" "/var/lib/pgsql/$1/data/pg_hba.conf"
wait
command_check "sudo systemctl enable postgresql-$1"
wait
command_check "sudo systemctl start postgresql-$1"
wait
command_check "sudo su - postgres -c \"psql -c \\\"alter user postgres with password 'postgres';\\\"\""
sleep 0.25
command_check "sudo su - postgres -c \"psql -c \\\"alter system set port = 5432;\\\"\""
sleep 0.25
command_check "sudo su - postgres -c \"psql -c \\\"alter system set listen_addresses = '*';\\\"\""
sleep 0.25
command_check "sudo su - postgres -c \"psql -c \\\"alter system set max_connections = 1000;\\\"\""
sleep 0.25
command_check "sudo systemctl restart postgresql-$1"
sleep 2.75
init "PGSQL $1 Install Finished!"
}

function menu_sw() {
    case $1 in
	1)
	    clear
		echo "*Enter q|Q return Top."
	    read -p "Enter PostgreSQL Version Num(10~14):" chw
		case $chw in
		    10|11|12|13|14)
			   n1=`rpm -qa postgres* | wc -l`
			   `ls /var/lib/pgsql`
			   RC=$?
			   n3=`netstat -ntlp | grep 5432 | wc -l`

			   if [[  $n1 -eq 0 ]] && [[ $RC -eq 2 ]] && [[ $n3 -eq 0 ]];then
			       install_pgsql $chw
			   else
			       init "PGSQL already installed.You need to remove it first."
			   fi
		    ;;
			q|Q)
			   init
			;;
			*)
			   sw_color_out 3 "Enter Right Version Num."
			   sleep 0.5
			   menu_sw 1
			;;
		esac
	    ;;
	2)
	    clear
	    echo "*Enter q|Q return Top."
	    read -p "Enter y|Y to confirm remove:" chw
		case $chw in
		    y|Y)
			   command_check "sudo yum -y remove postgres*"
			   command_check "rm -rf /var/lib/pgsql"
			   init "PGSQL Remove Finished."
		    ;;
			n|N|q|Q)
			   init
			;;
			*)
			   sw_color_out 3 "Enter agian."
			   sleep 0.5
			   menu_sw 2
			;;
		esac
	    ;;
	3)
	    clear
	    command_check "sudo su - postgres -c \"psql -c \\\"alter user postgres with password 'postgres';\\\"\""
		sleep 2.75
		init "Account postgres password reset Finished.(New password: postgres)"
	    ;;
	q|Q)
	    exit 0
	    ;;
	*)
	    init
	    ;;
	esac
}

function sw_color_out() {
    case $1 in
	 0)
	     echo -e "\e[32m "$2" \e[0m"
	     ;;
	 1)
	     echo -e "\e[31m "$2" \e[0m"
	     ;;
	 *)
	     echo -e "\e[33m "$2" \e[0m"
	     ;;
	esac
}

function Menu() {
echo "PGSQL10_14 Menu"
echo
echo "  1.Install"
echo "  2.Remove"
echo "  3.Reset Account [postgres] Password with postgres"
echo -e "\e[33m $1 \e[0m"
echo " Ctrl+C or q|Q with exit progress"
read -p "Enter num(1-3):" chw
}

function init() {
    clear
	Menu "$1"
	menu_sw $chw
}

#//-----------------------------------
#// MAIN PROCEDURE
#//-----------------------------------
clear
mkdir -p $LOG_FILE_DIR
touch $Log_Save_Path
log "========= pgsql10_14 START ========="

grep "CentOS Linux release 7" /etc/redhat-release
RC=$?
if [[ $RC -eq 1 ]];then
sw_color_out 1 "Only Allow In CentOS 7."
exit 1
fi

while true
do
init
done 
posted @ 2022-06-09 15:21  烽火三月  阅读(532)  评论(0编辑  收藏  举报