ywbanm 发表于 2013-1-30 01:37:35

Shell操作PostgreSQL创建DataBase

config file:

postgres_home=/usr/local/pgsql_8.3#admin_database=multi_dev_adminadmin_database=testcreateadmin_ip=172.25.73.252admin_port=5432admin_user=postgresadmin_password=11111contractor_database_0=multi_dev_contractcontractor_ip_0=172.25.73.253contractor_port_0=5432contractor_user_0=postgrescontractor_password_0=11111

shell:

source config.confecho "postgres_home :" $postgres_homeecho "========================================================================================"echo "admin_database :" $admin_databaseecho "admin_ip :" $admin_ipecho "admin_port :" $admin_portecho "admin_user :" $admin_userecho "admin_password :" $admin_passwordecho "contractor_database_0 :" $contractor_database_0echo "contractor_ip_0 :" $contractor_ip_0echo "contractor_port_0 :" $contractor_port_0echo "contractor_user_0 :" $contractor_user_0echo "contractor_password_0 :" $contractor_password_0echo "========================================================================================"echo -n "Above are the configurations of database(s) , please confirm : "read confirmif [ "$confirm" != "y" ]; thenexit 0fiecho $admin_ip:$admin_port:*:$admin_user:$admin_password >> ~/.pgpassecho $contractor_ip_0:$contractor_port_0:*:$contractor_user_0:$contractor_password_0 >> ~/.pgpasschmod 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 companyid,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
页: [1]
查看完整版本: Shell操作PostgreSQL创建DataBase