抽空写了一个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
1[root@db tmp]# more install_postgres.sh 2#!/bin/bash 3 4###################################################### 5## 6## Purpose:Install Postgresql Automatically 7## 8## Author :Kenyon 9## 10## Created:2014-02-08 11## 12## Version:1.0.0 13##################################################### 14 15echo "--------------------Check current OS datetime---------------------" 16 17DATE=`date +"%Y-%m-%d %H:%M:%S"` 18 19echo "------系统当前时间: $DATE------------------" 20echo "------确保系统时间不早于实际时间----------" 21echo "------请输入 Y or N 继续-----------------" 22read input 23if [ "$input" = "N" -o "$input" = "n" ]; then 24echo "-------------------Modify OS datetime correctly,exit install!-----" 25exit 1 26 27elif [ "$input" = "Y" -o "$input" = "y" ];then 28echo "---------------------start install PostgreSQL---------------------" 29yum 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 30mkdir -p /database/pgdata 31 32echo "---------------------create postgres user if not exist------------" 33if [ `grep "postgres" /etc/passwd | wc -l` -eq 0 ];then 34echo "adding user postgres" 35/usr/sbin/groupadd postgres 36/usr/sbin/useradd postgres -g postgres 37echo "postgres"|passwd --stdin postgres 38else 39echo "-------------- the user of Postgres is already exist,ignore useradd" 40fi 41 42cd /database 43chown -R postgres:postgres ./pgdata 44 45echo "---------------------download PostgreSQL source code----------------" 46cd /tmp 47chown postgres:postgres ./initdb_postgres.sh 48chmod u+x ./initdb_postgres.sh 49su - postgres -c /tmp/initdb_postgres.sh 50 51else 52echo "--------------------输入错误,请输入 Y or N-------------------" 53exit 1 54fi
2.initdb_postgres.sh
1#!/bin/bash 2 3###################################################### 4## 5## Purpose: Init Postgresql Automatically 6## 7## Author :Kenyon 8## 9## Created:2014-02-08 10## 11## Version:1.0.0 12##################################################### 13 14if [ `whoami` != "postgres" ] ; then 15echo "------------------Should be postgres user initdb!!----------------" 16exit 1 17 18else 19 20ecgo "-------------------init postgres's environment variables-----------" 21cat >> ~/.bash_profile << EOF 22export PGPORT=1949 23export PGHOME=/home/postgres 24export PGDATA=/database/pgdata 25export PATH=\$PGHOME/bin:\$PATH 26export MANPATH=\$PGHOME/share/man:\$MANPATH 27export LANG=en_US.utf8 28export LD_LIBRARY_PATH=\$PGHOME/lib:\$LD_LIBRARY_PATH 29 30alias pg_stop='pg_ctl -D \$PGDATA stop -m fast' 31alias pg_start='pg_ctl -D \$PGDATA start' 32alias pg_reload='pg_ctl -D \$PGDATA reload' 33EOF 34 35wget http://ftp.postgresql.org/pub/source/v9.3.2/postgresql-9.3.2.tar.gz 36echo "tar -zxvf postgresql-9.3.2.tar.gz" 37tar -zxvf postgresql-9.3.2.tar.gz 38cd postgresql-9.3.2 39 40echo "-------------------Configuring PostgreSQL,please wait---------------" 41./configure --prefix=/home/postgres --with-pgport=1949 --with-perl --with-python --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt --enable-thread-safety 42 43if [ $? -ne 0 ];then 44echo "Configure Postgresql失败,请检查config日志!" 45exit 1 46fi 47 48echo "-------------------Installing PostgreSQL,please wait----------------" 49gmake world 50if [ $? -ne 0 ];then 51echo "Gmake Postgresql失败 ,请检查日志!" 52exit 1 53fi 54 55gmake install-world 56if [ $? -ne 0 ];then 57echo "Gmake install Postgresql failed ,请检查日志!" 58exit 1 59fi 60 61echo "-----------------Initing Database----------------------------------" 62 63echo "Tc_Pgsql_ky">/tmp/postgres_pwd.txt 64initdb -D /database/pgdata -E UTF8 --locale=C -U postgres --pwfile /tmp/postgres_pwd.txt 65if [ $? -eq 0 ];then 66 echo "---------------安装PostgreSQL成功---" 67 rm -f /tmp/postgres_pwd.txt 68else 69 echo "--------------安装PostgreSQL失败,请检查日志------------" 70 exit 1 71fi 72 73 74fi
其他: 时间关系后期调整一下输入端口选择和密码动态输入