initdb.sh 13.1 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.39 1998/03/22 18:28:39 scrappy Exp $
30 31 32 33
#
#-------------------------------------------------------------------------

# ----------------
34 35
#       The _fUnKy_..._sTuFf_ gets set when the script is built (with make)
#       from parameters set in the make file.
36 37
#
# ----------------
38

39 40
NAMEDATALEN=_fUnKy_NAMEDATALEN_sTuFf_
OIDNAMELEN=_fUnKy_OIDNAMELEN_sTuFf_
41 42 43

CMDNAME=`basename $0`

44 45 46 47
# Find the default PGLIB directory (the directory that contains miscellaneous 
# files that are part of Postgres).  The user-written program postconfig
# outputs variable settings like "PGLIB=/usr/lib/whatever".  If it doesn't
# output a PGLIB value, then there is no default and the user must
48
# specify the pglib option.  Postconfig may validly not exist, in which case
49 50
# our invocation of it silently fails.

51
# The 2>/dev/null is to swallow the "postconfig: not found" message if there
52
# is no postconfig.
53

54 55
postconfig_result="`sh -c postconfig 2>/dev/null`"
if [ ! -z "$postconfig_result" ]; then
56
  set -a   # Make the following variable assignment exported to environment
57
  eval "$postconfig_result"
58
  set +a   # back to normal
59
fi
60

61
# Set defaults:
62 63
debug=0
noclean=0
64 65 66
template_only=0
POSTGRES_SUPERUSERNAME=$USER

67 68 69
while [ "$#" -gt 0 ]
do
# ${ARG#--username=} is not reliable or available on all platforms
70

71
    case "$1" in
72 73 74 75 76 77 78 79 80 81 82 83 84 85
        --debug|-d)
                debug=1
                echo "Running with debug mode on."
                ;;
        --noclean|-n)
                noclean=1
                echo "Running with noclean mode on. "
                     "Mistakes will not be cleaned up."
                ;;
        --template|-t)
                template_only=1
                echo "updating template1 database only."
                ;;
        --username=*)
86
                POSTGRES_SUPERUSERNAME="`echo $1 | sed 's/^--username=//'`"
87 88 89 90 91 92
                ;;
        -u)
                shift
                POSTGRES_SUPERUSERNAME="$1"
                ;;
        -u*)
93
                POSTGRES_SUPERUSERNAME="`echo $1 | sed 's/^-u//'`"
94 95
                ;;
        --pgdata=*)
96
                PGDATA="`echo $1 | sed 's/^--pgdata=//'`"
97 98 99 100 101 102
                ;;
        -r)
                shift
                PGDATA="$1"
                ;;
        -r*)
103
                PGDATA="`echo $1 | sed 's/^-r//'`"
104 105
                ;;
        --pglib=*)
106
                PGLIB="`echo $1 | sed 's/^--pglib=//'`"
107 108 109 110 111 112
                ;;
        -l)
                shift
                PGLIB="$1"
                ;;
        -l*)
113
                PGLIB="`echo $1 | sed 's/^-l//'`"
114 115 116 117 118 119 120 121 122 123 124 125
                ;;

        *)
                echo "Unrecognized option '$1'.  Syntax is:"
                echo "initdb [-t | --template] [-d | --debug]" \
                     "[-n | --noclean]" \
                     "[-u SUPERUSER | --username=SUPERUSER]" \
                     "[-r DATADIR | --pgdata=DATADIR]" \
                     "[-l LIBDIR | --pglib=LIBDIR]"
                exit 100
        esac
        shift
126 127
done

128 129 130
#-------------------------------------------------------------------------
# Make sure he told us where to find the Postgres files.
#-------------------------------------------------------------------------
131
if [ -z "$PGLIB" ]; then
132 133 134 135
    echo "$CMDNAME does not know where to find the files that make up "
    echo "Postgres (the PGLIB directory).  You must identify the PGLIB "
    echo "directory either with a --pglib invocation option, or by "
    echo "setting the PGLIB environment variable, or by having a program "
136 137
    echo "called 'postconfig' in your search path that outputs an asignment "
    echo "for PGLIB."
138 139 140 141 142 143 144
    exit 20
fi

#-------------------------------------------------------------------------
# Make sure he told us where to build the database system
#-------------------------------------------------------------------------

145
if [ -z "$PGDATA" ]; then
146 147 148 149 150 151
    echo "$CMDNAME: You must identify the PGDATA directory, where the data"
    echo "for this database system will reside.  Do this with either a"
    echo "--pgdata invocation option or a PGDATA environment variable."
    echo
    exit 20
fi
152

153 154
TEMPLATE=$PGLIB/local1_template1.bki.source
GLOBAL=$PGLIB/global1.bki.source
155 156
TEMPLATE_DESCR=$PGLIB/local1_template1.description
GLOBAL_DESCR=$PGLIB/global1.description
157
PG_HBA_SAMPLE=$PGLIB/pg_hba.conf.sample
158
PG_GEQO_SAMPLE=$PGLIB/pg_geqo.sample
159 160


161 162 163
#-------------------------------------------------------------------------
# Find the input files
#-------------------------------------------------------------------------
164

165 166 167
for PREREQ_FILE in $TEMPLATE $GLOBAL $PG_HBA_SAMPLE; do
    if [ ! -f $PREREQ_FILE ]; then 
        echo "$CMDNAME does not find the file '$PREREQ_FILE'."
168 169 170
        echo "This means you have identified an invalid PGLIB directory."
        echo "You specify a PGLIB directory with a --pglib invocation "
        echo "option, a PGLIB environment variable, or a postconfig program."
171 172 173 174 175 176 177 178 179
        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."
180
    echo
181 182 183 184 185 186
fi  

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

187
if [ -z "$POSTGRES_SUPERUSERNAME" ]; then 
188 189 190 191
    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
192 193
fi

194
POSTGRES_SUPERUID=`pg_id $POSTGRES_SUPERUSERNAME`
195

196 197 198 199 200 201 202
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
203

204 205 206
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"
207
    echo "to the specified unix user)."
208
    exit 2
209 210
fi

211 212
echo "We are initializing the database system with username" \
  "$POSTGRES_SUPERUSERNAME (uid=$POSTGRES_SUPERUID)."   
213
echo "This user will own all the files and must also own the server process."
214
echo
215

216 217 218
# -----------------------------------------------------------------------
# Create the data directory if necessary
# -----------------------------------------------------------------------
219 220 221 222

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

223
if [ -f "$PGDATA/PG_VERSION" ]; then
224
    if [ $template_only -eq 0 ]; then
225
        echo "$CMDNAME: error: File $PGDATA/PG_VERSION already exists."
226 227 228 229
        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 "
230 231
        echo "the $PGDATA directory or run initdb with a --pgdata option "
        echo "other than $PGDATA."
232 233 234 235 236
        exit 1
    fi
else
    if [ ! -d $PGDATA ]; then
        echo "Creating Postgres database system directory $PGDATA"
237
        echo
238 239 240 241 242
        mkdir $PGDATA
        if [ $? -ne 0 ]; then exit 5; fi
    fi
    if [ ! -d $PGDATA/base ]; then
        echo "Creating Postgres database system directory $PGDATA/base"
243
        echo
244 245 246
        mkdir $PGDATA/base
        if [ $? -ne 0 ]; then exit 5; fi
    fi
247 248
fi

249 250 251
#----------------------------------------------------------------------------
# Create the template1 database
#----------------------------------------------------------------------------
252

253 254
rm -rf $PGDATA/base/template1
mkdir $PGDATA/base/template1
255

256 257 258 259 260 261 262 263
if [ "$debug" -eq 1 ]; then
    BACKEND_TALK_ARG="-d"
else
    BACKEND_TALK_ARG="-Q"
fi

BACKENDARGS="-boot -C -F -D$PGDATA $BACKEND_TALK_ARG"

264 265 266 267 268 269 270 271 272 273 274
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
275
    echo "$CMDNAME: could not create template database"
276 277 278 279 280
    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."
281
    fi
282
    exit 1;
283 284
fi

285 286
echo

287 288
pg_version $PGDATA/base/template1

289 290 291
#----------------------------------------------------------------------------
# Create the global classes, if requested.
#----------------------------------------------------------------------------
292

293 294 295
if [ $template_only -eq 0 ]; then
    echo "Creating global classes in $PG_DATA/base"
    echo "Running: postgres $BACKENDARGS template1"
296

297 298 299 300 301 302
    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
303

304
    if (test $? -ne 0)
305
    then
306 307 308 309 310 311 312 313
        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;
314 315
    fi

316 317
    echo

318
    pg_version $PGDATA
319

320
    cp $PG_HBA_SAMPLE $PGDATA/pg_hba.conf
321
    cp $PG_GEQO_SAMPLE $PGDATA/pg_geqo.sample
322

323
    echo "Adding template1 database to pg_database..."
324

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    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;
343
    fi
344
    rm -f /tmp/create.$$
345 346
fi

347 348
echo

349 350
# If the COPY is first, the VACUUM generates an error, so we vacuum first
echo "vacuuming template1"
351
echo "vacuum" | postgres -o /dev/null -F -Q -D$PGDATA template1 
352

353
echo "COPY pg_shadow TO '$PGDATA/pg_pwd' USING DELIMITERS '\\t'" |\
354 355 356
	postgres -F -Q -D$PGDATA template1 2>&1 > /dev/null |\
	grep -v "'DEBUG:"

357 358 359 360 361 362 363 364 365 366
echo "creating public pg_user view"
echo "CREATE TABLE xpg_user (		\
	    usename	name,		\
	    usesysid	int4,		\
	    usecreatedb	bool,		\
	    usetrace	bool,		\
	    usesuper	bool,		\
	    usecatupd	bool,		\
	    passwd		text,		\
	    valuntil	abstime);" |\
367 368
	postgres -F -Q -D$PGDATA template1 2>&1 > /dev/null |\
	grep -v "'DEBUG:"
369 370

#move it into pg_user
371 372 373
echo "UPDATE pg_class SET relname = 'pg_user' WHERE relname = 'xpg_user';" |\
	postgres -F -Q -D$PGDATA template1 2>&1 > /dev/null |\
	grep -v "'DEBUG:"
374 375 376 377 378
echo "UPDATE pg_type SET typname = 'pg_user' WHERE typname = 'xpg_user';" |\
	postgres -F -Q -D$PGDATA template1 2>&1 > /dev/null |\
	grep -v "'DEBUG:"
mv $PGDATA/base/template1/xpg_user $PGDATA/base/template1/pg_user

379 380 381 382
echo "CREATE RULE _RETpg_user AS ON SELECT TO pg_user DO INSTEAD	\
	    SELECT usename, usesysid, usecreatedb, usetrace,		\
	           usesuper, usecatupd, '********'::text as passwd,	\
		   valuntil FROM pg_shadow;" |\
383 384
	postgres -F -Q -D$PGDATA template1 2>&1 > /dev/null |\
	grep -v "'DEBUG:"
385
echo "REVOKE ALL on pg_shadow FROM public" |\
386 387
	postgres -F -Q -D$PGDATA template1 2>&1 > /dev/null |\
	grep -v "'DEBUG:"
388

389 390 391
echo "loading pg_description"
echo "copy pg_description from '$TEMPLATE_DESCR'" | postgres -F -Q -D$PGDATA template1 > /dev/null
echo "copy pg_description from '$GLOBAL_DESCR'" | postgres -F -Q -D$PGDATA template1 > /dev/null
392
echo "vacuum analyze" | postgres -o /dev/null -F -Q -D$PGDATA template1