ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

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

2022-06-09 15:32:51  阅读:187  来源: 互联网

标签:10 PostgreSQL 14 sudo echo command out check postgres


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 

标签:10,PostgreSQL,14,sudo,echo,command,out,check,postgres
来源: https://www.cnblogs.com/syfl/p/16359454.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有