initdb.sh 9.3 KB
Newer Older
1 2 3 4
#!/bin/sh
#-------------------------------------------------------------------------
#
# initdb.sh--
5 6 7 8
#     Create (initialize) a Postgres database system.  
# 
#     A database system is a collection of Postgres databases all managed
#     by the same postmaster.  
9
#
10 11 12 13 14 15 16 17 18 19 20 21 22 23
#     To create the database system, we create the directory that contains
#     all its data, create the files that hold the global classes, create
#     a few other control files for it, and create one database:  the
#     template database.
#
#     The template database is an ordinary Postgres database.  Its data
#     never changes, though.  It exists to make it easy for Postgres to 
#     create other databases -- it just copies.
#
#     Optionally, we can skip creating the database system and just create
#     (or replace) the template database.
#
#     To create all those classes, we run the postgres (backend) program and
#     feed it data from bki files that are in the Postgres library directory.
24 25 26 27 28
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
29
#    $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.9 1996/10/04 20:07:10 scrappy Exp $
30 31 32 33 34
#
#-------------------------------------------------------------------------

# ----------------
#       Set paths from environment or default values.
35 36 37 38 39
#       The _fUnKy_..._sTuFf_ gets set when the script is built (with make)
#       from parameters set in the make file.
#       Currently the only thing we look for from the environment is
#       PGDATA, PGHOST, and PGPORT.  However, we should have environment
#       variables for all the paths.  
40 41 42
#
# ----------------
[ -z "$PGDATA" ] && { PGDATA=_fUnKy_DATADIR_sTuFf_; export PGDATA; }
43
[ -z "$PGPORT" ] && { PGPORT=_fUnKy_POSTPORT_sTuFf_; export PGPORT; }
44 45
[ -z "$PGHOST" ] && { PGHOST=localhost; export PGHOST; }
BINDIR=_fUnKy_BINDIR_sTuFf_
46 47 48
LIBDIR=_fUnKy_LIBDIR_sTuFf_
NAMEDATALEN=_fUnKy_NAMEDATALEN_sTuFf_
OIDNAMELEN=_fUnKy_OIDNAMELEN_sTuFf_
49 50 51 52 53
PATH=$BINDIR:$PATH
export PATH

CMDNAME=`basename $0`

54
# Set defaults:
55 56
debug=0
noclean=0
57 58 59
template_only=0
POSTGRES_SUPERUSERNAME=$USER

60 61 62 63 64 65 66 67 68 69
for ARG ; do
# We would normally use e.g. ${ARG#--username=} to parse the options, but
# there is a bug in some shells that makes that not work (BSD4.4 sh,
# September 1996 -- supposed to be fixed in later release).  So we bypass
# the bug with this sed mess.

    username_sed=`echo $ARG | sed s/^--username=//`
    pgdata_sed=`echo $ARG | sed s/^--pgdata=//`

    if [ $ARG = "--debug" -o $ARG = "-d" ]; then
70 71
        debug=1
        echo "Running with debug mode on."
72
    elif [ $ARG = "--noclean" -o $ARG = "-n" ]; then
73 74
        noclean=1
        echo "Running with noclean mode on.  Mistakes will not be cleaned up."
75
    elif [ $ARG = "--template" ]; then
76 77
        template_only=1
        echo "updating template1 database only."
78 79 80 81
    elif [ $username_sed. != $ARG. ]; then
        POSTGRES_SUPERUSERNAME=$username_sed
    elif [ $pgdata_sed. != $ARG. ]; then
        PGDATA=$pgdata_sed
82
    else    
83 84 85 86
        echo "Unrecognized option '$ARG'.  Syntax is:"
        echo "initdb [--template] [--debug] [--noclean]" \
             "[--username=SUPERUSER] [--pgdata=DATADIR]"
        exit 100
87
    fi
88 89
done

90
if [ "$debug" -eq 1 ]; then
M
Marc G. Fournier 已提交
91
    BACKENDARGS="-boot -C -F -d"
92
else
M
Marc G. Fournier 已提交
93
    BACKENDARGS="-boot -C -F -Q"
94 95
fi

96 97 98
TEMPLATE=$LIBDIR/local1_template1.bki.source
GLOBAL=$LIBDIR/global1.bki.source
PG_HBA_SAMPLE=$LIBDIR/pg_hba.sample
99

100 101 102
#-------------------------------------------------------------------------
# Find the input files
#-------------------------------------------------------------------------
103

104 105 106 107 108 109 110 111 112 113 114 115 116
for PREREQ_FILE in $TEMPLATE $GLOBAL $PG_HBA_SAMPLE; do
    if [ ! -f $PREREQ_FILE ]; then 
        echo "$CMDNAME does not find the file '$PREREQ_FILE'."
        echo "This means Postgres95 is incorrectly installed."
        exit 1
    fi
done

echo "$CMDNAME: using $TEMPLATE as input to create the template database."
if [ $template_only -eq 0 ]; then
    echo "$CMDNAME: using $GLOBAL as input to create the global classes."
    echo "$CMDNAME: using $PG_HBA_SAMPLE as the host-based authentication" \
         "control file."
117
    echo
118 119 120 121 122 123 124 125 126 127 128
fi  

#---------------------------------------------------------------------------
# Figure out who the Postgres superuser for the new database system will be.
#---------------------------------------------------------------------------

if [ -z $POSTGRES_SUPERUSERNAME ]; then 
    echo "Can't tell what username to use.  You don't have the USER"
    echo "environment variable set to your username and didn't specify the "
    echo "--username option"
    exit 1
129 130
fi

131
POSTGRES_SUPERUID=`pg_id $POSTGRES_SUPERUSERNAME`
132

133 134 135 136 137 138 139
if [ $POSTGRES_SUPERUID = NOUSER ]; then
    echo "Valid username not given.  You must specify the username for "
    echo "the Postgres superuser for the database system you are "
    echo "initializing, either with the --username option or by default "
    echo "to the USER environment variable."
    exit 10
fi
140

141 142 143 144 145
if [ $POSTGRES_SUPERUID -ne `pg_id` -a `pg_id` -ne 0 ]; then 
    echo "Only the unix superuser may initialize a database with a different"
    echo "Postgres superuser.  (You must be able to create files that belong"
    echo "to the specified Postgres userid)."
    exit 2
146 147
fi

148 149 150
echo "We are initializing the database system with username" \
  "$POSTGRES_SUPERUSERNAME (uid=$POSTGRES_SUPERUID)."   
echo "Please be aware that Postgres is not secure.  Anyone who can connect"
B
Bruce Momjian 已提交
151
echo "to the database can act as user $POSTGRES_SUPERUSERNAME" \
152
     "with very little effort."
153
echo
154

155 156 157
# -----------------------------------------------------------------------
# Create the data directory if necessary
# -----------------------------------------------------------------------
158 159 160 161

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

162 163 164 165 166 167 168 169 170 171 172 173 174 175
if [ -d "$PGDATA" ]; then
    if [ $template_only -eq 0 ]; then
        echo "$CMDNAME: error: Directory $PGDATA already exists."
        echo "This probably means initdb has already been run and the "
        echo "database system already exists."
        echo 
        echo "If you want to create a new database system, either remove "
        echo "the $PGDATA directory or run initdb with environment variable"
        echo "PGDATA set to something other than $PGDATA."
        exit 1
    fi
else
    if [ ! -d $PGDATA ]; then
        echo "Creating Postgres database system directory $PGDATA"
176
        echo
177 178 179 180 181
        mkdir $PGDATA
        if [ $? -ne 0 ]; then exit 5; fi
    fi
    if [ ! -d $PGDATA/base ]; then
        echo "Creating Postgres database system directory $PGDATA/base"
182
        echo
183 184 185
        mkdir $PGDATA/base
        if [ $? -ne 0 ]; then exit 5; fi
    fi
186 187
fi

188 189 190
#----------------------------------------------------------------------------
# Create the template1 database
#----------------------------------------------------------------------------
191

192 193
rm -rf $PGDATA/base/template1
mkdir $PGDATA/base/template1
194

195 196 197 198 199 200 201 202 203 204 205
echo "$CMDNAME: creating template database in $PGDATA/base/template1"
echo "Running: postgres $BACKENDARGS template1"

cat $TEMPLATE \
| sed -e "s/postgres PGUID/$POSTGRES_SUPERUSERNAME $POSTGRES_SUPERUID/" \
      -e "s/NAMEDATALEN/$NAMEDATALEN/g" \
      -e "s/OIDNAMELEN/$OIDNAMELEN/g" \
      -e "s/PGUID/$POSTGRES_SUPERUID/" \
| postgres $BACKENDARGS template1

if [ $? -ne 0 ]; then
206
    echo "$CMDNAME: could not create template database"
207 208 209 210 211
    if [ $noclean -eq 0 ]; then
        echo "$CMDNAME: cleaning up by wiping out $PGDATA/base/template1"
        rm -rf $PGDATA/base/template1
    else
        echo "$CMDNAME: cleanup not done because noclean options was used."
212
    fi
213
    exit 1;
214 215
fi

216 217
echo

218 219
pg_version $PGDATA/base/template1

220 221 222
#----------------------------------------------------------------------------
# Create the global classes, if requested.
#----------------------------------------------------------------------------
223

224 225 226
if [ $template_only -eq 0 ]; then
    echo "Creating global classes in $PG_DATA/base"
    echo "Running: postgres $BACKENDARGS template1"
227

228 229 230 231 232 233
    cat $GLOBAL \
    | sed -e "s/postgres PGUID/$POSTGRES_SUPERUSERNAME $POSTGRES_SUPERUID/" \
        -e "s/NAMEDATALEN/$NAMEDATALEN/g" \
        -e "s/OIDNAMELEN/$OIDNAMELEN/g" \
        -e "s/PGUID/$POSTGRES_SUPERUID/" \
    | postgres $BACKENDARGS template1
234

235
    if (test $? -ne 0)
236
    then
237 238 239 240 241 242 243 244
        echo "$CMDNAME: could not create global classes."
        if (test $noclean -eq 0); then
            echo "$CMDNAME: cleaning up."
            rm -rf $PGDATA
        else
            echo "$CMDNAME: cleanup not done (noclean mode set)."
        fi
        exit 1;
245 246
    fi

247 248
    echo

249
    pg_version $PGDATA
250

251
    cp $PG_HBA_SAMPLE $PGDATA/pg_hba
252

253
    echo "Adding template1 database to pg_database..."
254

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    echo "open pg_database" > /tmp/create.$$
    echo "insert (template1 $POSTGRES_SUPERUID template1)" >> /tmp/create.$$
    #echo "show" >> /tmp/create.$$
    echo "close pg_database" >> /tmp/create.$$

    echo "Running: postgres $BACKENDARGS template1 < /tmp/create.$$"

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

    if [ $? -ne 0 ]; then
        echo "$CMDNAME: could not log template database"
        if [ $noclean -eq 0 ]; then
            echo "$CMDNAME: cleaning up."
            rm -rf $PGDATA
        else
            echo "$CMDNAME: cleanup not done (noclean mode set)."
        fi
        exit 1;
273
    fi
274
    rm -f /tmp/create.$$
275 276
fi

277 278
echo

279
if [ $debug -eq 0 ]; then
280 281
    echo "vacuuming template1"

M
Marc G. Fournier 已提交
282
    echo "vacuum" | postgres -F -Q template1 > /dev/null
283
fi