initdb.sh 18.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#!/bin/sh
#-------------------------------------------------------------------------
#
# initdb.sh--
#     Create (initialize) a Postgres database system.  
# 
#     A database system is a collection of Postgres databases all managed
#     by the same postmaster.  
#
#     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.
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
29
#    $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.84 2000/02/08 15:58:54 momjian Exp $
Bruce Momjian's avatar
Bruce Momjian committed
30 31 32
#
#-------------------------------------------------------------------------

33
exit_nicely(){
Peter Eisentraut's avatar
Peter Eisentraut committed
34
    stty echo >& /dev/null
Bruce Momjian's avatar
Bruce Momjian committed
35 36
    echo
    echo "$CMDNAME failed."
37
    if [ "$noclean" -eq 0 ]; then
Bruce Momjian's avatar
Bruce Momjian committed
38
        echo "Removing $PGDATA."
39
        rm -rf "$PGDATA" || echo "Failed."
40 41
        echo "Removing temp file $TEMPFILE."
        rm -rf "$TEMPFILE" || echo "Failed."
Bruce Momjian's avatar
Bruce Momjian committed
42 43 44 45 46 47 48 49 50
    else
        echo "Data directory $PGDATA will not be removed at user's request."
    fi
    exit 1
}


CMDNAME=`basename $0`

51 52 53 54 55
if [ "$TMPDIR" ]; then
    TEMPFILE="$TMPDIR/initdb.$$"
else
    TEMPFILE="/tmp/initdb.$$"
fi
Bruce Momjian's avatar
Bruce Momjian committed
56

Peter Eisentraut's avatar
Peter Eisentraut committed
57 58 59 60 61 62 63 64 65 66 67 68

# Check for echo -n vs echo \c
if echo '\c' | grep -s c >/dev/null 2>&1
then
    ECHO_N="echo -n"
    ECHO_C=""
else
    ECHO_N="echo"
    ECHO_C='\c'
fi


Bruce Momjian's avatar
Bruce Momjian committed
69 70 71
#
# Find out where we're located
#
72 73
if echo "$0" | grep '/' > /dev/null 2>&1 
then
Bruce Momjian's avatar
Bruce Momjian committed
74 75 76 77
        # explicit dir name given
        PGPATH=`echo $0 | sed 's,/[^/]*$,,'`       # (dirname command is not portable)
else
        # look for it in PATH ('which' command is not portable)
Bruce Momjian's avatar
Bruce Momjian committed
78 79
        for dir in `echo "$PATH" | sed 's/:/ /g'`
	do
Bruce Momjian's avatar
Bruce Momjian committed
80 81
                # empty entry in path means current dir
                [ -z "$dir" ] && dir='.'
82
                if [ -f "$dir/$CMDNAME" ]
Bruce Momjian's avatar
Bruce Momjian committed
83
		then
Bruce Momjian's avatar
Bruce Momjian committed
84 85 86 87 88 89 90
                        PGPATH="$dir"
                        break
                fi
        done
fi

# Check if needed programs actually exist in path
91
for prog in postgres pg_version pg_id
Bruce Momjian's avatar
Bruce Momjian committed
92
do
93
        if [ ! -x "$PGPATH/$prog" ]
Bruce Momjian's avatar
Bruce Momjian committed
94
	then
Bruce Momjian's avatar
Bruce Momjian committed
95 96 97 98 99 100 101 102 103 104
                echo "The program $prog needed by $CMDNAME could not be found. It was"
                echo "expected at:"
                echo "    $PGPATH/$prog"
                echo "If this is not the correct directory, please start $CMDNAME"
                echo "with a full search path. Otherwise make sure that the program"
                echo "was installed successfully."
                exit 1
        fi
done

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

# Gotta wait for pg_id existence check above
EffectiveUser=`$PGPATH/pg_id -n -u`
if [ -z "$EffectiveUser" ]; then
    echo "Could not determine current user name. You are really hosed."
    exit 1
fi

if [ `$PGPATH/pg_id -u` -eq 0 ]
then
    echo "You cannot run $CMDNAME as root. Please log in (using, e.g., 'su')"
    echo "as the (unprivileged) user that will own the server process."
    exit 1
fi


Bruce Momjian's avatar
Bruce Momjian committed
121 122
# 0 is the default (non-)encoding
MULTIBYTEID=0
Peter Eisentraut's avatar
Peter Eisentraut committed
123
# This is placed here by configure --enable-multibyte[=XXX].
Bruce Momjian's avatar
Bruce Momjian committed
124 125 126 127 128 129
MULTIBYTE=__MULTIBYTE__

# Set defaults:
debug=0
noclean=0
template_only=0
130
show_setting=0
Bruce Momjian's avatar
Bruce Momjian committed
131 132 133 134 135

# Note: There is a single compelling reason that the name of the database
#       superuser be the same as the Unix user owning the server process:
#       The single user postgres backend will only connect as the database
#       user with the same name as the Unix user running it. That's
136
#       a security measure.
137
POSTGRES_SUPERUSERNAME="$EffectiveUser"
138
POSTGRES_SUPERUSERID=`$PGPATH/pg_id -u`
Bruce Momjian's avatar
Bruce Momjian committed
139

140
while [ "$#" -gt 0 ]
Bruce Momjian's avatar
Bruce Momjian committed
141 142 143 144 145 146 147 148 149 150
do
    case "$1" in
        --help|-\?)
                usage=t
                break
                ;;
        --debug|-d)
                debug=1
                echo "Running with debug mode on."
                ;;
151 152 153
        --show|-s)
        	show_setting=1
        	;;        
Bruce Momjian's avatar
Bruce Momjian committed
154 155 156 157 158 159 160 161
        --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."
                ;;
162
# The sysid of the database superuser. Can be freely changed.
Bruce Momjian's avatar
Bruce Momjian committed
163 164 165 166 167 168 169 170 171 172
        --sysid|-i)
                POSTGRES_SUPERUSERID="$2"
                shift;;
        --sysid=*)
                POSTGRES_SUPERUSERID=`echo $1 | sed 's/^--sysid=//'`
                ;;
        -i*)
                POSTGRES_SUPERUSERID=`echo $1 | sed 's/^-i//'`
                ;;
# The default password of the database superuser.
Peter Eisentraut's avatar
Peter Eisentraut committed
173 174 175
# Make initdb prompt for the default password of the database superuser.
        --pwprompt|-W)
                PwPrompt=1
Bruce Momjian's avatar
Bruce Momjian committed
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
                ;;
# Directory where to install the data. No default, unless the environment
# variable PGDATA is set.
        --pgdata|-D)
                PGDATA="$2"
                shift;;
        --pgdata=*)
                PGDATA=`echo $1 | sed 's/^--pgdata=//'`
                ;;
        -D*)
                PGDATA=`echo $1 | sed 's/^-D//'`
                ;;
# The directory where the database templates are stored (traditionally in
# $prefix/lib). This is now autodetected for the most common layouts.
        --pglib|-L)
                PGLIB="$2"
                shift;;
        --pglib=*)
                PGLIB=`echo $1 | sed 's/^--pglib=//'`
                ;;
        -L*)
                PGLIB=`echo $1 | sed 's/^-L//'`
                ;;
# The encoding of the template1 database. Defaults to what you chose
# at configure time. (see above)
201
        --encoding|-E)
Bruce Momjian's avatar
Bruce Momjian committed
202 203
                MULTIBYTE="$2"
                shift;;
Peter Eisentraut's avatar
Peter Eisentraut committed
204 205
        --encoding=*)
                MULTIBYTE=`echo $1 | sed 's/^--encoding=//'`
Bruce Momjian's avatar
Bruce Momjian committed
206
                ;;
207 208
        -E*)
                MULTIBYTE=`echo $1 | sed 's/^-E//'`
Bruce Momjian's avatar
Bruce Momjian committed
209
                ;;
210 211 212 213 214
	-*)
		echo "$CMDNAME: invalid option: $1"
		echo "Try -? for help."
		exit 1
		;;
Bruce Momjian's avatar
Bruce Momjian committed
215
        *)
Peter Eisentraut's avatar
Peter Eisentraut committed
216
                PGDATA=$1
Bruce Momjian's avatar
Bruce Momjian committed
217 218 219 220 221
                ;;
    esac
    shift
done

Peter Eisentraut's avatar
Peter Eisentraut committed
222 223 224 225 226 227 228
if [ "$usage" ]; then
        echo "initdb initialized a PostgreSQL database."
 	echo
 	echo "Usage:"
        echo "  $CMDNAME [options] datadir"
 	echo
        echo "Options:"
229
        echo " [-D, --pgdata] <datadir>     Location for this database"
Peter Eisentraut's avatar
Peter Eisentraut committed
230
        echo "  -W, --pwprompt              Prompt for a password for the new superuser's"
231 232
 	if [ -n "$MULTIBYTE" ]
	then 
233
 		echo "  -E, --encoding <encoding>   Set the default multibyte encoding for new databases"
Bruce Momjian's avatar
Bruce Momjian committed
234
	fi
Peter Eisentraut's avatar
Peter Eisentraut committed
235 236
        echo "  -i, --sysid <sysid>         Database sysid for the superuser"
        echo "Less commonly used options: "
237
        echo "  -L, --pglib <libdir>        Where to find the input files"
Peter Eisentraut's avatar
Peter Eisentraut committed
238 239
        echo "  -t, --template              Re-initialize template database only"
        echo "  -d, --debug                 Generate lots of debugging output"
240
        echo "  -s, --show                  Do not action, show the initdb setting only" 
Peter Eisentraut's avatar
Peter Eisentraut committed
241
        echo "  -n, --noclean               Do not clean up after errors"
242
 	echo
243
        echo "Report bugs to <pgsql-bugs@postgresql.org>."
244
 	exit 0
Bruce Momjian's avatar
Bruce Momjian committed
245 246 247 248 249 250
fi

#-------------------------------------------------------------------------
# Resolve the multibyte encoding name
#-------------------------------------------------------------------------

251 252
if [ "$MULTIBYTE" ]
then
Peter Eisentraut's avatar
Peter Eisentraut committed
253
	MULTIBYTEID=`$PGPATH/pg_encoding $MULTIBYTE 2> /dev/null`
254
        if [ "$?" -ne 0 ]
Bruce Momjian's avatar
Bruce Momjian committed
255
	then
Peter Eisentraut's avatar
Peter Eisentraut committed
256 257 258 259
                echo "$CMDNAME: pg_encoding failed"
                echo
                echo "Perhaps you did not configure PostgreSQL for multibyte support or"
                echo "the program was not successfully installed."
Bruce Momjian's avatar
Bruce Momjian committed
260 261
                exit 1
        fi
262 263
	if [ -z "$MULTIBYTEID" ]
	then
Peter Eisentraut's avatar
Peter Eisentraut committed
264
		echo "$CMDNAME: $MULTIBYTE is not a valid encoding name"
Bruce Momjian's avatar
Bruce Momjian committed
265 266 267 268 269 270 271 272 273
		exit 1
	fi
fi


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

274 275
if [ -z "$PGDATA" ]
then
Bruce Momjian's avatar
Bruce Momjian committed
276 277 278 279 280 281 282 283 284 285
    echo "$CMDNAME: You must identify where the the data for this database"
    echo "system will reside.  Do this with either a --pgdata invocation"
    echo "option or a PGDATA environment variable."
    echo
    exit 1
fi

# The data path must be absolute, because the backend doesn't like
# '.' and '..' stuff. (Should perhaps be fixed there.)

286 287
if ! echo "$PGDATA" | grep '^/' > /dev/null 2>&1
then
Peter Eisentraut's avatar
Peter Eisentraut committed
288
    echo "$CMDNAME: data path must be specified as an absolute path"
Bruce Momjian's avatar
Bruce Momjian committed
289 290 291 292 293 294 295 296
    exit 1
fi


#-------------------------------------------------------------------------
# Find the input files
#-------------------------------------------------------------------------

297 298
if [ -z "$PGLIB" ]
then
Bruce Momjian's avatar
Bruce Momjian committed
299 300
        for dir in "$PGPATH/../lib" "$PGPATH/../lib/pgsql"
	do
301
                if [ -f "$dir/global1.bki.source" ]
Bruce Momjian's avatar
Bruce Momjian committed
302
		then
303
                        PGLIB="$dir"
Bruce Momjian's avatar
Bruce Momjian committed
304 305 306 307 308
                        break
                fi
        done
fi

309 310
if [ -z "$PGLIB" ]
then
Bruce Momjian's avatar
Bruce Momjian committed
311 312 313 314 315 316 317
        echo "$CMDNAME: Could not find the \"lib\" directory, that contains"
        echo "the files needed by initdb. Please specify it with the"
        echo "--pglib option."
        exit 1
fi


318 319 320
TEMPLATE="$PGLIB"/local1_template1.bki.source
GLOBAL="$PGLIB"/global1.bki.source
PG_HBA_SAMPLE="$PGLIB"/pg_hba.conf.sample
Bruce Momjian's avatar
Bruce Momjian committed
321

322 323 324
TEMPLATE_DESCR="$PGLIB"/local1_template1.description
GLOBAL_DESCR="$PGLIB"/global1.description
PG_GEQO_SAMPLE="$PGLIB"/pg_geqo.sample
325
PG_POSTMASTER_OPTS_DEFAULT_SAMPLE="$PGLIB"/postmaster.opts.default.sample
Bruce Momjian's avatar
Bruce Momjian committed
326

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
if [ "$show_setting" ]
then
	echo
	echo "The initdb setting:"
 	echo
 	echo "  DATADIR:        $PGDATA"
 	echo "  PGLIB:          $PGLIB"
 	echo "  PGPATH:         $PGPATH"
 	echo "  TEMPFILE:       $TEMPFILE"
 	echo "  MULTIBYTE:      $MULTIBYTE"
 	echo "  MULTIBYTEID:    $MULTIBYTEID"
 	echo "  SUPERUSERNAME:  $POSTGRES_SUPERUSERNAME"
 	echo "  SUPERUSERID:    $POSTGRES_SUPERUSERID"
 	echo "  TEMPLATE:       $TEMPLATE"	 
	echo "  GLOBAL:         $GLOBAL"
	echo "  PG_HBA_SAMPLE:  $PG_HBA_SAMPLE"
	echo "  TEMPLATE_DESCR: $TEMPLATE_DESCR"
	echo "  GLOBAL_DESCR:   $GLOBAL_DESCR"
	echo "  PG_GEQO_SAMPLE: $PG_GEQO_SAMPLE"
	echo "  PG_POSTMASTER_OPTS_DEFAULT_SAMPLE: $PG_POSTMASTER_OPTS_DEFAULT_SAMPLE"
	echo 
	exit 0
fi

Bruce Momjian's avatar
Bruce Momjian committed
351 352
for PREREQ_FILE in "$TEMPLATE" "$GLOBAL" "$PG_HBA_SAMPLE"
do
353 354
    if [ ! -f "$PREREQ_FILE" ]
	then 
Bruce Momjian's avatar
Bruce Momjian committed
355 356 357 358 359 360 361 362 363
        echo "$CMDNAME does not find the file '$PREREQ_FILE'."
        echo "This means you have a corrupted installation or identified the"
        echo "wrong directory with the --pglib invocation option."
        exit 1
    fi
done

[ "$debug" -ne 0 ] && echo "$CMDNAME: Using $TEMPLATE as input to create the template database."

364 365
if [ "$template_only" -eq 0 ]
then
Bruce Momjian's avatar
Bruce Momjian committed
366 367
    [ "$debug" -ne 0 ] && echo "$CMDNAME: Using $GLOBAL as input to create the global classes."
    [ "$debug" -ne 0 ] && echo "$CMDNAME: Using $PG_HBA_SAMPLE as default authentication control file."
Bruce Momjian's avatar
Bruce Momjian committed
368
fi
Bruce Momjian's avatar
Bruce Momjian committed
369

Bruce Momjian's avatar
Bruce Momjian committed
370
trap 'echo "Caught signal." ; exit_nicely' 1 2 3 15
Bruce Momjian's avatar
Bruce Momjian committed
371

372 373 374 375
# Let's go
echo "This database system will be initialized with username \"$POSTGRES_SUPERUSERNAME\"."
echo "This user will own all the data files and must also own the server process."
echo
Bruce Momjian's avatar
Bruce Momjian committed
376

Marc G. Fournier's avatar
Marc G. Fournier committed
377 378 379 380 381 382 383
# -----------------------------------------------------------------------
# Create the data directory if necessary
# -----------------------------------------------------------------------

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

384 385 386
if [ -f "$PGDATA"/PG_VERSION ]
then
    if [ "$template_only" -eq 0 ]
Bruce Momjian's avatar
Bruce Momjian committed
387
    then
388 389
        echo "$CMDNAME: The file $PGDATA/PG_VERSION already exists."
        echo "This probably means initdb has already been run and the"
Marc G. Fournier's avatar
Marc G. Fournier committed
390 391
        echo "database system already exists."
        echo 
392 393
        echo "If you want to create a new database system, either remove"
        echo "the directory $PGDATA or run initdb with a --pgdata argument"
Marc G. Fournier's avatar
Marc G. Fournier committed
394 395 396 397
        echo "other than $PGDATA."
        exit 1
    fi
else
398 399
    if [ ! -d "$PGDATA" ]
	then
400
        echo "Creating database system directory $PGDATA"
401
        mkdir "$PGDATA" || exit_nicely
402 403
    else
        echo "Fixing permissions on pre-existing data directory $PGDATA"
404
	chmod go-rwx "$PGDATA" || exit_nicely
Marc G. Fournier's avatar
Marc G. Fournier committed
405
    fi
406

407 408
    if [ ! -d "$PGDATA"/base ]
	then
409
        echo "Creating database system directory $PGDATA/base"
410
        mkdir "$PGDATA"/base || exit_nicely
Marc G. Fournier's avatar
Marc G. Fournier committed
411
    fi
412
    if [ ! -d "$PGDATA"/pg_xlog ]
Bruce Momjian's avatar
Bruce Momjian committed
413
    then
414
        echo "Creating database XLOG directory $PGDATA/pg_xlog"
415
        mkdir "$PGDATA"/pg_xlog || exit_nicely
416
    fi
Marc G. Fournier's avatar
Marc G. Fournier committed
417 418 419 420 421 422
fi

#----------------------------------------------------------------------------
# Create the template1 database
#----------------------------------------------------------------------------

423 424
rm -rf "$PGDATA"/base/template1 || exit_nicely
mkdir "$PGDATA"/base/template1 || exit_nicely
Marc G. Fournier's avatar
Marc G. Fournier committed
425

426 427
if [ "$debug" -eq 1 ]
then
Marc G. Fournier's avatar
Marc G. Fournier committed
428 429 430 431 432 433
    BACKEND_TALK_ARG="-d"
else
    BACKEND_TALK_ARG="-Q"
fi

BACKENDARGS="-boot -C -F -D$PGDATA $BACKEND_TALK_ARG"
434
FIRSTRUN="-boot -x -C -F -D$PGDATA $BACKEND_TALK_ARG"
Marc G. Fournier's avatar
Marc G. Fournier committed
435

Bruce Momjian's avatar
Bruce Momjian committed
436
echo "Creating template database in $PGDATA/base/template1"
437
[ "$debug" -ne 0 ] && echo "Running: $PGPATH/postgres $FIRSTRUN template1"
Marc G. Fournier's avatar
Marc G. Fournier committed
438

439
cat "$TEMPLATE" \
440
| sed -e "s/PGUID/$POSTGRES_SUPERUSERID/g" \
441
| "$PGPATH"/postgres $FIRSTRUN template1 \
442
|| exit_nicely
Marc G. Fournier's avatar
Marc G. Fournier committed
443

444
"$PGPATH"/pg_version "$PGDATA"/base/template1 || exit_nicely
Marc G. Fournier's avatar
Marc G. Fournier committed
445 446 447 448 449

#----------------------------------------------------------------------------
# Create the global classes, if requested.
#----------------------------------------------------------------------------

450 451
if [ "$template_only" -eq 0 ]
then
452 453
    echo "Creating global relations in $PGDATA/base"
    [ "$debug" -ne 0 ] && echo "Running: $PGPATH/postgres $BACKENDARGS template1"
Marc G. Fournier's avatar
Marc G. Fournier committed
454

455
    cat "$GLOBAL" \
456 457
    | sed -e "s/POSTGRES/$POSTGRES_SUPERUSERNAME/g" \
          -e "s/PGUID/$POSTGRES_SUPERUSERID/g" \
458
    | "$PGPATH"/postgres $BACKENDARGS template1 \
459
    || exit_nicely
Marc G. Fournier's avatar
Marc G. Fournier committed
460

461
    "$PGPATH"/pg_version "$PGDATA" || exit_nicely
Marc G. Fournier's avatar
Marc G. Fournier committed
462

463 464
    cp "$PG_HBA_SAMPLE" "$PGDATA"/pg_hba.conf     || exit_nicely
    cp "$PG_GEQO_SAMPLE" "$PGDATA"/pg_geqo.sample || exit_nicely
465
    cp "$PG_POSTMASTER_OPTS_DEFAULT_SAMPLE" "$PGDATA"/postmaster.opts.default || exit_nicely
Marc G. Fournier's avatar
Marc G. Fournier committed
466

467
    echo "Adding template1 database to pg_database"
Marc G. Fournier's avatar
Marc G. Fournier committed
468

469
    echo "open pg_database" > "$TEMPFILE"
470
    echo "insert (template1 $POSTGRES_SUPERUSERID $MULTIBYTEID template1)" >> $TEMPFILE
471 472
    #echo "show" >> "$TEMPFILE"
    echo "close pg_database" >> "$TEMPFILE"
Marc G. Fournier's avatar
Marc G. Fournier committed
473

474
    [ "$debug" -ne 0 ] && echo "Running: $PGPATH/postgres $BACKENDARGS template1 < $TEMPFILE"
Marc G. Fournier's avatar
Marc G. Fournier committed
475

476
    "$PGPATH"/postgres $BACKENDARGS template1 < "$TEMPFILE"
477
    # Gotta remove that temp file before exiting on error.
478 479
    retval="$?"
    if [ "$noclean" -eq 0 ]
Bruce Momjian's avatar
Bruce Momjian committed
480
    then
481
            rm -f "$TEMPFILE" || exit_nicely
Marc G. Fournier's avatar
Marc G. Fournier committed
482
    fi
483
    [ "$retval" -ne 0 ] && exit_nicely
Marc G. Fournier's avatar
Marc G. Fournier committed
484 485 486 487
fi

echo

488
PGSQL_OPT="-o /dev/null -O -F -Q -D$PGDATA"
Marc G. Fournier's avatar
Marc G. Fournier committed
489

Bruce Momjian's avatar
Bruce Momjian committed
490 491
# Create a trigger so that direct updates to pg_shadow will be written
# to the flat password file pg_pwd
492 493
echo "CREATE TRIGGER pg_sync_pg_pwd AFTER INSERT OR UPDATE OR DELETE ON pg_shadow" \
     "FOR EACH ROW EXECUTE PROCEDURE update_pg_pwd()" \
494
     | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
Bruce Momjian's avatar
Bruce Momjian committed
495

Peter Eisentraut's avatar
Peter Eisentraut committed
496 497
# needs to be done before alter user
echo "REVOKE ALL on pg_shadow FROM public" \
498
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
499

Peter Eisentraut's avatar
Peter Eisentraut committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
# set up password
if [ "$PwPrompt" ]; then
    $ECHO_N "Enter new superuser password: "$ECHO_C
    stty -echo >& /dev/null
    read FirstPw
    stty echo >& /dev/null
    echo
    $ECHO_N "Enter it again: "$ECHO_C
    stty -echo >& /dev/null
    read SecondPw
    stty echo >& /dev/null
    echo
    if [ "$FirstPw" != "$SecondPw" ]; then
        echo "Passwords didn't match."
        exit_nicely
    fi
    echo "ALTER USER \"$POSTGRES_SUPERUSERNAME\" WITH PASSWORD '$FirstPw'" \
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
    if [ ! -f $PGDATA/pg_pwd ]; then
        echo "The password file wasn't generated. Please report this problem."
        exit_nicely
    fi
    echo "Setting password"
fi

525 526

echo "Creating view pg_user."
527 528 529 530 531 532 533 534 535 536
echo "CREATE VIEW pg_user AS \
        SELECT \
            usename, \
            usesysid, \
            usecreatedb, \
            usetrace, \
            usesuper, \
            usecatupd, \
            '********'::text as passwd, \
            valuntil \
537
        FROM pg_shadow" \
538
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
539 540

echo "Creating view pg_rules."
541 542 543 544 545 546 547
echo "CREATE VIEW pg_rules AS \
        SELECT \
            C.relname AS tablename, \
            R.rulename AS rulename, \
	    pg_get_ruledef(R.rulename) AS definition \
	FROM pg_rewrite R, pg_class C \
	WHERE R.rulename !~ '^_RET' \
548
            AND C.oid = R.ev_class;" \
549
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
550 551

echo "Creating view pg_views."
552 553 554 555 556 557 558 559 560 561
echo "CREATE VIEW pg_views AS \
        SELECT \
            C.relname AS viewname, \
            pg_get_userbyid(C.relowner) AS viewowner, \
            pg_get_viewdef(C.relname) AS definition \
        FROM pg_class C \
        WHERE C.relhasrules \
            AND	EXISTS ( \
                SELECT rulename FROM pg_rewrite R \
                    WHERE ev_class = C.oid AND ev_type = '1' \
562
            )" \
563
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
564 565

echo "Creating view pg_tables."
566 567 568 569 570 571 572 573 574 575 576 577
echo "CREATE VIEW pg_tables AS \
        SELECT \
            C.relname AS tablename, \
	    pg_get_userbyid(C.relowner) AS tableowner, \
	    C.relhasindex AS hasindexes, \
	    C.relhasrules AS hasrules, \
	    (C.reltriggers > 0) AS hastriggers \
        FROM pg_class C \
        WHERE C.relkind IN ('r', 's') \
            AND NOT EXISTS ( \
                SELECT rulename FROM pg_rewrite \
                    WHERE ev_class = C.oid AND ev_type = '1' \
578
            )" \
579
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
580 581

echo "Creating view pg_indexes."
582 583 584 585 586 587 588
echo "CREATE VIEW pg_indexes AS \
        SELECT \
            C.relname AS tablename, \
	    I.relname AS indexname, \
            pg_get_indexdef(X.indexrelid) AS indexdef \
        FROM pg_index X, pg_class C, pg_class I \
	WHERE C.oid = X.indrelid \
589
            AND I.oid = X.indexrelid" \
590
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
591 592

echo "Loading pg_description."
593 594 595 596 597
echo "COPY pg_description FROM STDIN" > $TEMPFILE
cat "$TEMPLATE_DESCR" >> $TEMPFILE
cat "$GLOBAL_DESCR" >> $TEMPFILE

cat $TEMPFILE \
598
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
599 600 601 602 603
if [ "$noclean" -eq 0 ]
then
    rm -f "$TEMPFILE" || exit_nicely
fi

604 605
echo "Vacuuming database."
echo "VACUUM ANALYZE" \
606
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
607 608 609

echo
echo "$CMDNAME completed successfully. You can now start the database server."
Peter Eisentraut's avatar
Peter Eisentraut committed
610
echo "  $PGPATH/postmaster -D $PGDATA"
611
echo "or"
Peter Eisentraut's avatar
Peter Eisentraut committed
612
echo "  $PGPATH/pg_ctl -D $PGDATA start"
613 614 615
echo

exit 0