PostgreSQL自动安装脚本

抽空写了一个postgresql的自动安装部署脚本,方便集群化安装

环境:
OS:CentOS 6.3
DB:PostgreSQL 9.3.2

使用步骤:
分两个脚本,一个是执行文件,另一个是初始化脚本,实际只要点击执行文件install_postgres.sh即可。

1.上传这两个文件到/tmp目录下
2.root用户执行sh install_postgres.sh 内容:

1.install_postgres.sh
[root@db tmp]# more install_postgres.sh
#!/bin/bash

######################################################
##
##  Purpose:Install Postgresql Automatically
## 
##  Author :Kenyon
##  
##  Created:2014-02-08
## 
##  Version:1.0.0
#####################################################

echo "--------------------Check current OS datetime---------------------"

DATE=`date +"%Y-%m-%d %H:%M:%S"`

echo "------系统当前时间: $DATE------------------"
echo "------确保系统时间不早于实际时间----------"
echo "------请输入 Y or N 继续-----------------"
read input
if [ "$input" = "N" -o "$input" = "n" ]; then
echo "-------------------Modify OS datetime correctly,exit install!-----"
exit 1

elif [ "$input" = "Y" -o "$input" = "y" ];then
echo "---------------------start install PostgreSQL---------------------"
yum install -y wget perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake
mkdir -p /database/pgdata

echo "---------------------create postgres user if not exist------------"
if [ `grep "postgres" /etc/passwd | wc -l` -eq 0 ];then
echo "adding user postgres"
/usr/sbin/groupadd postgres
/usr/sbin/useradd postgres -g postgres
echo "postgres"|passwd --stdin  postgres
else
echo "--------------  the user of Postgres is already exist,ignore useradd"
fi

cd /database
chown -R postgres:postgres  ./pgdata

echo "---------------------download PostgreSQL source code----------------"
cd /tmp
chown postgres:postgres ./initdb_postgres.sh
chmod u+x ./initdb_postgres.sh
su - postgres -c /tmp/initdb_postgres.sh

else
echo "--------------------输入错误,请输入 Y or N-------------------"
exit 1
fi


2.initdb_postgres.sh
#!/bin/bash

######################################################
##
##  Purpose: Init Postgresql Automatically
## 
##  Author :Kenyon
##  
##  Created:2014-02-08
## 
##  Version:1.0.0
#####################################################

if [ `whoami` != "postgres" ] ; then
echo "------------------Should be postgres user initdb!!----------------"
exit 1

else

ecgo "-------------------init postgres's environment variables-----------"
cat >> ~/.bash_profile << EOF
export PGPORT=1949
export PGHOME=/home/postgres
export PGDATA=/database/pgdata
export PATH=\$PGHOME/bin:\$PATH
export MANPATH=\$PGHOME/share/man:\$MANPATH
export LANG=en_US.utf8
export LD_LIBRARY_PATH=\$PGHOME/lib:\$LD_LIBRARY_PATH

alias pg_stop='pg_ctl -D \$PGDATA stop -m fast'
alias pg_start='pg_ctl -D \$PGDATA start'
alias pg_reload='pg_ctl -D \$PGDATA reload'
EOF

wget http://ftp.postgresql.org/pub/source/v9.3.2/postgresql-9.3.2.tar.gz
echo "tar -zxvf postgresql-9.3.2.tar.gz"
tar -zxvf postgresql-9.3.2.tar.gz
cd postgresql-9.3.2

echo "-------------------Configuring PostgreSQL,please wait---------------"
./configure --prefix=/home/postgres --with-pgport=1949 --with-perl --with-python --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt --enable-thread-safety

if [ $? -ne 0 ];then
echo "Configure Postgresql失败,请检查config日志!"
exit 1
fi

echo "-------------------Installing PostgreSQL,please wait----------------"
gmake world
if [ $? -ne 0 ];then
echo "Gmake Postgresql失败 ,请检查日志!"
exit 1
fi

gmake install-world
if [ $? -ne 0 ];then
echo "Gmake install Postgresql failed ,请检查日志!"
exit 1
fi

echo "-----------------Initing Database----------------------------------"

echo "Tc_Pgsql_ky">/tmp/postgres_pwd.txt
initdb -D /database/pgdata -E UTF8 --locale=C -U postgres --pwfile /tmp/postgres_pwd.txt
if [ $? -eq 0 ];then
  echo "---------------安装PostgreSQL成功---"
  rm -f /tmp/postgres_pwd.txt
else
  echo "--------------安装PostgreSQL失败,请检查日志------------"
  exit 1
fi


fi
其他: 时间关系后期调整一下输入端口选择和密码动态输入

转载于:https://my.oschina.net/Kenyon/blog/197961

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值