环境:
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
其他: 时间关系后期调整一下输入端口选择和密码动态输入