home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / scripts / initdb < prev    next >
Encoding:
Text File  |  1992-08-27  |  5.0 KB  |  246 lines

  1. #!/bin/sh
  2. # ----------------------------------------------------------------
  3. #   FILE
  4. #    initdb    create a postgres template database
  5. #
  6. #   DESCRIPTION
  7. #    this program feeds the proper input to the ``postgres'' program
  8. #    to create a postgres database and register it in the
  9. #    shared ``pg_database'' database.
  10. #
  11. #   IDENTIFICATION
  12. #     $Header: /private/postgres/src/scripts/RCS/initdb,v 1.2 1991/05/13 01:22:28 kemnitz Exp $
  13. # ----------------------------------------------------------------
  14.  
  15. CMDNAME=`basename $0`
  16.  
  17. # ----------------
  18. #     check arguments:
  19. #         -d indicates debug mode.
  20. #        -n means don't clean up on error (so your cores don't go away)
  21. # ----------------
  22. debug=0
  23. noclean=0
  24. verbose=0
  25.  
  26. for ARG
  27. do
  28.     case "$ARG" in
  29.     -d)    debug=1; echo "$CMDNAME: debug mode on";;
  30.     -n)    noclean=1; echo "$CMDNAME: noclean mode on";;
  31.     -v) verbose=1; echo "$CMDNAME: verbose mode on";;
  32.     esac
  33. done
  34.  
  35. # ----------------
  36. #     if the debug flag is set, then 
  37. # ----------------
  38. if (test "$debug" -eq 1)
  39. then
  40.     BACKENDARGS="-boot -COd -ami"
  41. else
  42.     BACKENDARGS="-boot -COQ -ami"
  43. fi
  44.  
  45. # ----------------
  46. #     check POSTGRESHOME
  47. # ----------------
  48. if (test -z "$POSTGRESHOME")
  49. then
  50.     echo "$CMDNAME: POSTGRESHOME not set."
  51.     exit 1
  52. else
  53.     PG=`echo $POSTGRESHOME | sed -e 's/\([^:]*\):.*/\1/'`
  54.     PGS=`echo $POSTGRESHOME | sed -e 's/:/ /g'`
  55. fi
  56.  
  57. # ----------------
  58. #     check POSTGRESTREE
  59. # ----------------
  60. if (test -z "$POSTGRESTREE")
  61. then
  62.     TREE=$PG
  63. else
  64.     TREE=$POSTGRESTREE
  65. fi
  66.  
  67. # ----------------
  68. #     check POSTGRESBIN
  69. # ----------------
  70. if (test -z "$POSTGRESBIN")
  71. then
  72.     PGBIN=$PG/bin
  73. else
  74.     PGBIN=$POSTGRESBIN
  75. fi
  76.  
  77. # ----------------
  78. #     check POSTGRESTEMP
  79. # ----------------
  80. if (test -z "$POSTGRESTEMP")
  81. then
  82.     PGTEMP=$PG/files
  83. else
  84.     PGTEMP=$POSTGRESTEMP
  85. fi
  86.  
  87. # ----------------
  88. #     find the paths to the postgres, pg_version, and pg_id programs
  89. # ----------------
  90. if (test "$verbose" -eq 1)
  91. then
  92.     echo "$CMDNAME: looking for postgres..."
  93. fi
  94.  
  95. if (test -f $PGBIN/postgres)
  96. then
  97.     BACKEND=$PGBIN/postgres
  98.     PG_VERSION=$PGBIN/pg_version
  99.     PG_ID=$PGBIN/pg_id
  100.     if (test "$verbose" -eq 1)
  101.     then
  102.         echo "$CMDNAME: found $BACKEND"
  103.     fi
  104. elif (test -f $TREE/*/support/postgres)
  105. then
  106.     BACKEND=$TREE/*/support/postgres
  107.     PG_VERSION=$TREE/*/support/pg_version
  108.     PG_ID=$TREE/*/support/pg_id
  109.     if (test "$verbose" -eq 1)
  110.     then
  111.         echo "$CMDNAME: found $BACKEND"
  112.     fi
  113. else
  114.     echo "$CMDNAME: could not find postgres program"
  115.     echo "$CMDNAME: set POSTGRESHOME to the proper directory and rerun."
  116.     exit 1
  117. fi
  118.  
  119. # ----------------
  120. #     find the template files
  121. # ----------------
  122.  
  123. if (test "$verbose" -eq 1)
  124. then
  125.     echo "$CMDNAME: looking for template files..."
  126. fi
  127.  
  128. if (test -f $PGTEMP/local1_template1.bki)
  129. then
  130.     TEMPLATE=$PGTEMP/local1_template1.bki
  131.     GLOBAL=$PGTEMP/global1.bki
  132. else
  133.     echo "$CMDNAME: warning: Make install has not been run"
  134.     TEMPLATE=`echo $TREE/*/support/local.bki`
  135.     GLOBAL=`echo $TREE/*/support/dbdb.bki`
  136.  
  137.     if (test ! -f $TEMPLATE)
  138.     then
  139.         echo "$CMDNAME: could not find template files"
  140.         echo "$CMDNAME: set POSTGRESTEMP to the proper directory and rerun."
  141.         exit 1
  142.     fi
  143. fi
  144.  
  145. if (test "$verbose" -eq 1)
  146. then
  147.     echo "$CMDNAME: using $TEMPLATE"
  148.     echo "$CMDNAME: using $GLOBAL"
  149. fi
  150.  
  151. #
  152. # Figure out who I am...
  153. #
  154.  
  155. UID=`$PG_ID`
  156.  
  157. # ----------------
  158. #     create the template database if necessary
  159. # ----------------
  160.  
  161. if (test -f "$pg/data")
  162. then
  163.     echo "$CMDNAME: $pg/data exists - delete it if necessary"
  164.     exit 1
  165. fi
  166.  
  167. for pg in $PGS
  168. do
  169.     mkdir $pg/data $pg/data/base $pg/data/base/template1
  170. done
  171.  
  172. if (test "$verbose" -eq 1)
  173. then
  174.     echo "$CMDNAME: creating SHARED relations in $PG/data"
  175.     echo "$CMDNAME: creating template database in $PG/data/base/template1"
  176. fi
  177.  
  178. $BACKEND $BACKENDARGS template1 < $TEMPLATE 
  179.  
  180. if (test $? -ne 0)
  181. then
  182.     echo "$CMDNAME: could not create template database"
  183.     if (test $noclean -eq 0)
  184.     then
  185.         echo "$CMDNAME: cleaning up."
  186.         for pg in $PGS
  187.         do
  188.                 rm -rf $pg/data
  189.         done
  190.         else
  191.         echo "$CMDNAME: cleanup not done (noclean mode set)."
  192.     fi
  193.     exit 1;
  194. fi
  195.  
  196. $PG_VERSION $PG/data/base/template1
  197.  
  198. #
  199. # Add the template database to pg_database
  200. #
  201.  
  202. echo "open pg_database" > /tmp/create.$$
  203. echo "insert (template1 $UID template1)" >> /tmp/create.$$
  204. echo "show" >> /tmp/create.$$
  205. echo "close pg_database" >> /tmp/create.$$
  206.  
  207. $BACKEND $BACKENDARGS template1 < $GLOBAL 
  208.  
  209. if (test $? -ne 0)
  210. then
  211.     echo "$CMDNAME: could create shared relations"
  212.     if (test $noclean -eq 0)
  213.     then
  214.         echo "$CMDNAME: cleaning up."
  215.         for pg in $PGS
  216.         do
  217.             rm -rf $pg/data
  218.         done
  219.     else
  220.         echo "$CMDNAME: cleanup not done (noclean mode set)."
  221.     fi
  222.     exit 1;
  223. fi
  224.  
  225. $PG_VERSION $PG/data
  226.  
  227. $BACKEND $BACKENDARGS template1 < /tmp/create.$$ 
  228.  
  229. if (test $? -ne 0)
  230. then
  231.     echo "$CMDNAME: could not log template database"
  232.     if (test $noclean -eq 0)
  233.     then
  234.         echo "$CMDNAME: cleaning up."
  235.         for pg in $PGS
  236.         do
  237.             rm -rf $pg/data
  238.         done
  239.     else
  240.         echo "$CMDNAME: cleanup not done (noclean mode set)."
  241.     fi
  242.     exit 1;
  243. fi
  244.  
  245. rm -f /tmp/create.$$
  246.