PostgreSQL10 自动选择源码安装或者yum安装脚本,并实现开机自启动
#!/bin/bash
#########################################################################
# File Name: auto_install_postgresql.sh
# Author: dx
# Created Time: Sun 26 Jul 2020 07:09:28 PM CST
# Description: The test script
# Copyright(C): 2020 All rights reserved
# Version: v1.0.0
#########################################################################
LOCAL_SRC="/usr/src"
CHK_SYSVER=$(cat /etc/redhat-release | sed -r "s#.* ([0-9]+)..*#\1#")
YUM_INSTALL="yum install -y"
LOCAL_PACKAGE="wget make tar gzip net-tools cmake"
LOCAL_PACKAGE_CHECK=$(rpm -qa | grep -awciE "wget|make|tar|gzip|net-tools")
#nginx define variables
PGSQL_VER="10.13"
PGSQL_SRC="postgresql-${PGSQL_VER}"
PGSQL_DIR="/usr/local/postgresql"
PGSQL_SOFT="postgresql-${PGSQL_VER}.tar.gz"
PGSQL_URL="https://ftp.postgresql.org/pub/source/v10.13"
PGSQL_PACKAGE="readline readline-devel openssl openssl-devel zlib zlib-devel bison flex gcc gcc-c++ perl perl-devel perl-ExtUtils-Embed pam pam-devel libxml2 libxml2-devel libxslt libxslt-devel openldap openldap-devel python-devel "
PGSQL_DATA_PATH="/data/pgdata"
###只有root才可以运行该脚本
if [ $(id -u) -ne 0 ]; then
echo -e "\033[32myou must run scripts as root Sorry exit!!"
exit 1
fi
#安装必要软件以及依赖
function local_install() {
echo "安装wget make tar gzip net-tools cmake"
if [ $LOCAL_PACKAGE_CHECK -lt 5 ]; then
$YUM_INSTALL $LOCAL_PACKAGE
fi
echo "安装依赖"
$YUM_INSTALL $INSTALL_PACKAGE
}
#关闭slinux以及配置防火墙
function firewall_set() {
echo "关闭sliunx,配置防火墙"
setenforce 0
if [ $CHK_SYSVER = "6" ]; then
iptables -t filter -A INPUT -m tcp -p tcp --dport ${FIREWALL_PORT} -j ACCEPT
service iptables save
else
firewall-cmd --add-port=${FIREWALL_PORT}/tcp --permanent
systemctl reload firewalld.service
fi
ps -ef | grep ${FIREWALL_NAME}
netstat -tnlp | grep -w ${FIREWALL_PORT}
#chkconfig --add ${FIREWALL_NAME}
#chkconfig ${FIREWALL_NAME} on
cat > /usr/lib/systemd/system/postgresql.service <<-EOF
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
# Port number for server to listen on
Environment=PGPORT=5432
# Location of database directory
Environment=PGDATA=${PGSQL_DATA_PATH}
# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog
# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
#ExecStartPre=${PGSQL_DIR}/bin/postgresql-check-db-dir \${PGDATA}
ExecStart=${PGSQL_DIR}/bin/pg_ctl start -D \${PGDATA} -s -o "-p \${PGPORT}" -w -t 300
ExecStop=${PGSQL_DIR}/bin/pg_ctl stop -D \${PGDATA} -s -m fast
ExecReload=${PGSQL_DIR}/bin/pg_ctl reload -D \${PGDATA} -s
# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300
[Install]
WantedBy=multi-user.target
EOF
chmod +x /usr/lib/systemd/system/postgresql.service
systemctl daemon-reload
systemctl enable postgresql.service
systemctl restart postgresql.service
}
#文件检查
function check_file() {
echo "检查文件是否存在,不存在则下载该文件。"
if [ -f $SOFT ]; then
echo "文件已存在,复制文件到$LOCAL_SRC"
\cp -a $SOFT $LOCAL_SRC
else
echo "文件不存在,下载$SOFT文件到$LOCAL_SRC"
wget -c $SOFT_URL -P $LOCAL_SRC
fi
#test -f '$SOFT' && \cp -a '$SOFT' '$LOCAL_SRC' || wget -c $SOFT_URL -P $LOCAL_SRC
}
#编译文件
function make_file() {
cd $LOCAL_SRC
echo "解压$SOFT"
ls -l $SOFT
tar -xf $SOFT
cd $SOFT_SRC
echo "预编译$SOFT"
#./configure --prefix=$SOFT_DIR --datadir=${PGSQL_DATA_PATH} --with-perl --with-python --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt --enable-thread-safety
./configure --prefix=$SOFT_DIR --with-perl --with-python --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt --enable-thread-safety
echo "编译,安装$SOFT"
make -j && make install -j
}
#用户添加
function add_new_user() {
id -u ${NEW_USER}
if [ $? -ne 0 ]; then
echo "创建$NEW_USER用户"
#useradd -s /sbin/nologin ${NEW_USER} -M
useradd ${NEW_USER}
fi
echo "${NEW_USER}" | passwd --stdin ${NEW_USER}
}
function make_install_postgresql() {
#安装依赖
INSTALL_PACKAGE=$PGSQL_PACKAGE
local_install
#用户添加
NEW_USER="postgres"
add_new_user ${NEW_USER}
#文件检查
SOFT=$PGSQL_SOFT
echo "文件名称$SOFT"
SOFT_URL=$PGSQL_URL/$PGSQL_SOFT
echo "文件下载路径$SOFT_URL"
check_file
#编译文件
SOFT_DIR=$PGSQL_DIR
SOFT_ARGS=$PGSQL_ARGS
SOFT_SRC=$PGSQL_SRC
make_file
echo "添加环境变量。"
cat >/etc/profile.d/postgresql.sh <<-EOF
export PATH=${PGSQL_DIR}/bin:\$PATH
export LD_LIBRARY_PATH=${PGSQL_DIR}/lib:\$LD_LIBRARY_PATH
export PGDATA=${PGSQL_DATA_PATH}
export PGHOST=/tmp
EOF
source /etc/profile
mkdir -p ${PGSQL_DATA_PATH}
chown -R ${NEW_USER}. ${PGSQL_DATA_PATH}/../
echo "初始化postgresql数据库"
#ls -l initdb_postgresql.sh
#if [ $? ne 0 ];then
# \cp initdb_postgresql.sh ${LOCAL_SRC}
#fi
#su - postgres -s /bin/bash ${LOCAL_SRC}/initdb_postgresql.sh
su - postgres -c "${PGSQL_DIR}/bin/initdb"
sleep 1
echo "启动postgresql数据库"
su - postgres -c "${PGSQL_DIR}/bin/pg_ctl -D ${PGSQL_DATA_PATH} -l logfile start"
echo "设置postgresql网络"
sed -i "/listen_addresses/a\listen_addresses = '*'" /data/pgdata/postgresql.conf
echo "host all all 0.0.0.0/0 md5" >>/data/pgdata/pg_hba.conf
su - postgres -c "${PGSQL_DIR}/bin/psql -U postgres -d postgres <<EOF
alter role postgres with password 'postgres';
EOF"
chown -R ${NEW_USER}. ${PGSQL_DATA_PATH}/../
su - postgres -c "${PGSQL_DIR}/bin/pg_ctl restart"
#关闭slinux以及配置防火墙:w
FIREWALL_NAME="postgresql"
FIREWALL_PORT="5432"
firewall_set ${FIREWALL_NAME} ${FIREWALL_PORT}
}
function yum_install_postgresql() {
# Install the repository RPM:
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# Install PostgreSQL:
yum install -y postgresql10-server
# Optionally initialize the database and enable automatic start:
/usr/pgsql-10/bin/postgresql-10-setup initdb
sed -i "/listen_addresses/a\listen_addresses = '*'" /var/lib/pgsql/10/data/postgresql.conf
egrep "listen_addresses" /var/lib/pgsql/10/data/postgresql.conf
echo "host all all 0.0.0.0/0 md5" >>/var/lib/pgsql/10/data/pg_hba.conf
systemctl enable postgresql-10
systemctl start postgresql-10
#关闭slinux以及配置防火墙
FIREWALL_NAME="postgresql"
FIREWALL_PORT="5432"
firewall_set ${FIREWALL_NAME} ${FIREWALL_PORT}
}
PS3="Please select install Menu:"
select i in yum_install_postgresql make_install_postgresql quit; do
case $i in
yum_install_postgresql)
echo -e "\033[32mstart to yum install postgresql, please wait.................\033[0m"
sleep 1
echo "###########################################"
echo "开始安装postgresql"
echo "###########################################"
echo "开始安装基础应用"
local_install
echo "基础应用安装完成,开始安装ningx"
yum_install_postgresql
break
;;
make_install_postgresql)
echo -e "\033[32mstart to make install postgresql, please wait.................\033[0m"
sleep 1
echo "###########################################"
echo "开始安装postgresql"
echo "###########################################"
make_install_postgresql
break
;;
*)
echo -e "\033[32mPlease input right agrs exit!!\033[0m"
break
;;
esac
done
本脚本提供PostgreSQL10的自动化安装流程,包括源码安装和yum安装两种方式,支持开机自启动配置及防火墙设置,适用于不同Linux环境。
1266

被折叠的 条评论
为什么被折叠?



