Shell操作PostgreSQL创建DataBase

转自:http://ywbanm.javaeye.com/blog/434572

 

config file:

postgres_home=/usr/local/pgsql_8.3 #admin_database=multi_dev_admin admin_database=testcreate admin_ip=172.25.73.252 admin_port=5432 admin_user=postgres admin_password=11111 contractor_database_0=multi_dev_contract contractor_ip_0=172.25.73.253 contractor_port_0=5432 contractor_user_0=postgres contractor_password_0=11111

 

shell:

source config.conf echo "postgres_home :" $postgres_home echo "========================================================================================" echo "admin_database :" $admin_database echo "admin_ip :" $admin_ip echo "admin_port :" $admin_port echo "admin_user :" $admin_user echo "admin_password :" $admin_password echo "contractor_database_0 :" $contractor_database_0 echo "contractor_ip_0 :" $contractor_ip_0 echo "contractor_port_0 :" $contractor_port_0 echo "contractor_user_0 :" $contractor_user_0 echo "contractor_password_0 :" $contractor_password_0 echo "========================================================================================" echo -n "Above are the configurations of database(s) , please confirm [y/n]: " read confirm if [ "$confirm" != "y" ]; then exit 0 fi echo $admin_ip:$admin_port:*:$admin_user:$admin_password >> ~/.pgpass echo $contractor_ip_0:$contractor_port_0:*:$contractor_user_0:$contractor_password_0 >> ~/.pgpass chmod 0600 ~/.pgpass #$postgres_home/bin/psql -h $contractor_ip_0 -p $contractor_port_0 -U $contractor_user_0 $contractor_database_0 -q -t -c "select com panyid,abbreviation from iwcontract ;" #function executeSQL #execute the sql statement #parameters: ip,port,user,sql executeSQL() { echo `$postgres_home/bin/psql -h $1 -p $2 -U $3 -q -t -c "$4";` } #function checkExistAndCreate #check whether the given database exists. #If it exists,then quit this method. #If it doesn't exist, then create one with the given name #parameters: ip,port,user,database checkExistAndCreate() { result=`executeSQL $1 $2 $3 "SELECT datname FROM pg_database WHERE datname='$4';"` echo "4:" $4 echo "result:" $result if [ "$result" == "$4" ]; then echo "The database has existed ! If keeping the following operations on a existent database ," echo " it will bring data insecurity. Please change a database name!" exit 0 else echo "------ when condition is false-----" executeSQL $1 $2 $3 "CREATE DATABASE $4 WITH OWNER = $3 ENCODING = 'UTF8';" echo "after create table... New Database is :" fi } echo "start checkExist()" checkExistAndCreate $admin_ip $admin_port $admin_user $admin_database

你可能感兴趣的:(Shell操作PostgreSQL创建DataBase)