createdb.sh
上传用户:blenddy
上传日期:2007-01-07
资源大小:6495k
文件大小:2k
源码类别:

数据库系统

开发平台:

Unix_Linux

  1. #!/bin/sh
  2. #-------------------------------------------------------------------------
  3. #
  4. # createdb.sh--
  5. #    create a postgres database
  6. #
  7. #    this program runs the monitor with the "-c" option to create
  8. #    the requested database.
  9. #
  10. # Copyright (c) 1994, Regents of the University of California
  11. #
  12. #
  13. # IDENTIFICATION
  14. #    $Header: /usr/local/cvsroot/pgsql/src/bin/createdb/createdb.sh,v 1.11 1998/09/03 02:12:14 momjian Exp $
  15. #
  16. #-------------------------------------------------------------------------
  17. CMDNAME=`basename $0`
  18. MBENABLED=__MULTIBYTE__
  19. MB=
  20. if [ -z "$USER" ]; then
  21. if [ -z "$LOGNAME" ]; then
  22. if [ -z "`whoami`" ]; then
  23. echo "$CMDNAME: cannot determine user name"
  24. exit 1
  25. fi
  26. else
  27. USER=$LOGNAME
  28. export USER
  29. fi
  30. fi
  31. dbname=$USER
  32. PASSWDOPT="";
  33. while test -n "$1"
  34. do
  35. case $1 in
  36. --help) usage=1;;
  37. -a) AUTHSYS=$2; shift;;
  38. -h) PGHOST=$2; shift;;
  39. -p) PGPORT=$2; shift;;
  40. -u) PASSWDOPT=$1;;
  41. -D) dbpath=$2; shift;;
  42. -E)
  43. if [ -z "$MBENABLED" ];then
  44. echo "$CMDNAME: you need to turn on MB compile time option"
  45. exit 1
  46. fi
  47. MB=$2
  48. MBID=`pg_encoding $MB`
  49. if [ -z "$MBID" ];then
  50. echo "$CMDNAME: $MB is not a valid encoding name"
  51. exit 1
  52. fi
  53. shift;;
  54. -*) echo "$CMDNAME: unrecognized parameter $1"; usage=1;;
  55.  *) dbname=$1;;
  56. esac
  57. shift;
  58. done
  59. if [ "$usage" ]; then
  60. if [ -z "$MBENABLED" ];then
  61. echo "Usage: $CMDNAME -a <authtype> -h <server> -p <portnumber> -D <location> [dbname]"
  62. exit 1
  63. else
  64. echo "Usage: $CMDNAME -a <authtype> -h <server> -p <portnumber> -D <location> -E <encoding> [dbname]"
  65. exit 1
  66. fi
  67. fi
  68. if [ -z "$AUTHSYS" ]; then
  69. AUTHOPT=""
  70. else
  71. AUTHOPT="-a $AUTHSYS"
  72. fi
  73. if [ -z "$PGHOST" ]; then
  74. PGHOSTOPT=""
  75. else
  76. PGHOSTOPT="-h $PGHOST"
  77. fi
  78. if [ -z "$PGPORT" ]; then
  79. PGPORTOPT=""
  80. else
  81. PGPORTOPT="-p $PGPORT"
  82. fi
  83. if [ -z "$dbpath" ]; then
  84. location=""
  85. else
  86. # if [ ! -d "$dbpath"/base ]; then
  87. # echo "$CMDNAME: database creation failed on $dbname."
  88. # echo "directory $dbpath/base not found."
  89. # exit 1
  90. # fi
  91. location="with location = '$dbpath'"
  92. fi
  93. if [ -z "$MBENABLED" -o -z "$MB" ]; then
  94. encoding=""
  95. else
  96. encoding="encoding = '$MB'"
  97. if [ -z "$location" ];then
  98. encoding="with $encoding"
  99. fi
  100. fi
  101. psql $PASSWDOPT -tq $AUTHOPT $PGHOSTOPT $PGPORTOPT -c "create database $dbname $location $encoding" template1
  102. if [ $? -ne 0 ]; then
  103. echo "$CMDNAME: database creation failed on $dbname."
  104. exit 1
  105. fi
  106. exit 0