initdb.sh 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/bin/sh
#-------------------------------------------------------------------------
#
# initdb.sh--
#    create a postgres template database
#
#    this program feeds the proper input to the ``postgres'' program
#    to create a postgres database and register it in the
#    shared ``pg_database'' database.
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
M
Marc G. Fournier 已提交
15
#    $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.4 1996/07/23 03:03:19 scrappy Exp $
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#
#-------------------------------------------------------------------------

# ----------------
#       Set paths from environment or default values.
#       The _fUnKy_..._sTuFf_ gets set when the script is installed
#       from the default value for this build.
#       Currently the only thing wee look for from the environment is
#       PGDATA, PGHOST, and PGPORT
#
# ----------------
[ -z "$PGDATA" ] && { PGDATA=_fUnKy_DATADIR_sTuFf_; export PGDATA; }
[ -z "$PGPORT" ] && { PGPORT=5432; export PGPORT; }
[ -z "$PGHOST" ] && { PGHOST=localhost; export PGHOST; }
POSTGRESDIR=_fUnKy_POSTGRESDIR_sTuFf_
BINDIR=_fUnKy_BINDIR_sTuFf_
FILESDIR=$PGDATA/files
PATH=$BINDIR:$PATH
export PATH

CMDNAME=`basename $0`

# ----------------
# 	check arguments:
# 	    -d indicates debug mode.
#	    -n means don't clean up on error (so your cores don't go away)
# ----------------
debug=0
noclean=0
verbose=0

for ARG
do
	case "$ARG" in
	-d)	debug=1; echo "$CMDNAME: debug mode on";;
	-n)	noclean=1; echo "$CMDNAME: noclean mode on";;
	-v)	verbose=1; echo "$CMDNAME: verbose mode on";;
	*)	echo "initdb [-d][-n][-v]\n -d : debug mode\n -n : noclean mode, leaves temp files around \n -v : verbose mode";  exit 0;
	esac
done

# ----------------
# 	if the debug flag is set, then 
# ----------------
if test "$debug" -eq 1
then
M
Marc G. Fournier 已提交
62
    BACKENDARGS="-boot -C -F -d"
63
else
M
Marc G. Fournier 已提交
64
    BACKENDARGS="-boot -C -F -Q"
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
fi


TEMPLATE=$FILESDIR/local1_template1.bki
GLOBAL=$FILESDIR/global1.bki
if [ ! -f $TEMPLATE -o ! -f $GLOBAL ]
then
    echo "$CMDNAME: error: database initialization files not found."
    echo "$CMDNAME: either gmake install has not been run or you're trying to"
    echo "$CMDNAME: run this program on a machine that does not store the"
    echo "$CMDNAME: database (PGHOST doesn't work for this)."
    exit 1
fi

if test "$verbose" -eq 1
then
    echo "$CMDNAME: using $TEMPLATE"
    echo "$CMDNAME: using $GLOBAL"
fi

#
# Figure out who I am...
#

PG_UID=`pg_id`

if test $PG_UID -eq 0
then
    echo "$CMDNAME: do not install POSTGRES as root"
    exit 1
fi

# ----------------
# 	create the template database if necessary
#	the first we do is create data/base, so we'll check for that.
# ----------------

if test -d "$PGDATA/base"
then
	echo "$CMDNAME: error: it looks like initdb has already been run.  You must"
	echo "clean out the database directory first with the cleardbdir program"
	exit 1
fi

# umask must disallow access to group, other for files and dirs
umask 077

mkdir $PGDATA/base $PGDATA/base/template1

if test "$verbose" -eq 1
then
    echo "$CMDNAME: creating SHARED relations in $PGDATA"
    echo "$CMDNAME: creating template database in $PGDATA/base/template1"
    echo "postgres $BACKENDARGS template1 < $TEMPLATE "
fi

postgres $BACKENDARGS template1 < $TEMPLATE 


if test $? -ne 0
then
    echo "$CMDNAME: could not create template database"
    if test $noclean -eq 0
    then
	    echo "$CMDNAME: cleaning up."
	    cd $PGDATA
	    for i in *
	    do
		if [ $i != "files" -a $i != "pg_hba" ]
		then
			/bin/rm -rf $i
		fi
	    done
        else
	    echo "$CMDNAME: cleanup not done (noclean mode set)."
    fi
	exit 1;
fi

pg_version $PGDATA/base/template1

#
# Add the template database to pg_database
#

echo "open pg_database" > /tmp/create.$$
echo "insert (template1 $PG_UID template1)" >> /tmp/create.$$
#echo "show" >> /tmp/create.$$
echo "close pg_database" >> /tmp/create.$$

if test "$verbose" -eq 1
then
	echo "postgres $BACKENDARGS template1 < $GLOBAL"
fi

postgres $BACKENDARGS template1 < $GLOBAL 

if (test $? -ne 0)
then
    echo "$CMDNAME: could create shared relations"
    if (test $noclean -eq 0)
    then
	    echo "$CMDNAME: cleaning up."
	    cd $PGDATA
	    for i in *
	    do
		if [ $i != "files" ]
		then
			/bin/rm -rf $i
		fi
	    done
    else
	    echo "$CMDNAME: cleanup not done (noclean mode set)."
    fi
	exit 1;
fi

pg_version $PGDATA

if test "$verbose" -eq 1
then
	echo "postgres $BACKENDARGS template1 < /tmp/create.$$"
fi

postgres $BACKENDARGS template1 < /tmp/create.$$ 

if test $? -ne 0
then
    echo "$CMDNAME: could not log template database"
    if (test $noclean -eq 0)
    then
	    echo "$CMDNAME: cleaning up."
	    cd $PGDATA
	    for i in *
	    do
		if [ $i != "files" ]
		then
			/bin/rm -rf $i
		fi
	    done
    else
	    echo "$CMDNAME: cleanup not done (noclean mode set)."
    fi
	exit 1;
fi

if test $debug -eq 0
then

if test "$verbose" -eq 1
then
    echo "vacuuming template1"
fi

M
Marc G. Fournier 已提交
219
    echo "vacuum" | postgres -F -Q template1 > /dev/null
220 221 222
fi

rm -f /tmp/create.$$