initdb.sh 18.7 KB
Newer Older
1
#! /bin/sh
M
 
Marc G. Fournier 已提交
2 3
#-------------------------------------------------------------------------
#
4 5 6
# initdb creates (initializes) a Postgres database cluster (site,
# instance, installation, whatever).  A database cluster is a
# collection of Postgres databases all managed by the same postmaster.  
M
 
Marc G. Fournier 已提交
7
#
8 9 10 11
# To create the database cluster, we create the directory that contains
# all its data, create the files that hold the global tables, create
# a few other control files for it, and create one database:  the
# template database.
M
 
Marc G. Fournier 已提交
12
#
13 14 15
# 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.
M
 
Marc G. Fournier 已提交
16
#
17 18
# Optionally, we can skip creating the complete database cluster and
# just create (or replace) the template database.
M
 
Marc G. Fournier 已提交
19
#
20 21
# To create all those things, we run the postgres (backend) program and
# feed it data from the bki files that were installed.
M
 
Marc G. Fournier 已提交
22 23
#
#
24
# Copyright (c) 1994, Regents of the University of California
M
 
Marc G. Fournier 已提交
25
#
26
# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.106 2000/10/22 17:55:45 pjw Exp $
B
Bruce Momjian 已提交
27 28 29
#
#-------------------------------------------------------------------------

30 31 32 33 34

##########################################################################
#
# INITIALIZATION

35
exit_nicely(){
B
Bruce Momjian 已提交
36
    stty echo > /dev/null 2>&1
B
Bruce Momjian 已提交
37 38
    echo
    echo "$CMDNAME failed."
39
    if [ "$noclean" != yes ]; then
40 41 42 43
        if [ "$template_only" != yes ] && [ "$made_new_pgdata" = yes ]; then
            echo "Removing $PGDATA."
            rm -rf "$PGDATA" || echo "Failed."
        fi
44 45
        echo "Removing temp file $TEMPFILE."
        rm -rf "$TEMPFILE" || echo "Failed."
B
Bruce Momjian 已提交
46 47 48 49 50 51 52 53 54
    else
        echo "Data directory $PGDATA will not be removed at user's request."
    fi
    exit 1
}


CMDNAME=`basename $0`

55
# Placed here during build
56 57
VERSION='@VERSION@'
bindir='@bindir@'
58 59
# Note that "datadir" is not the directory we're initializing, it's
# merely how Autoconf names PREFIX/share.
60
datadir='@datadir@'
61
# as set by configure --enable-multibyte[=XXX].
62
MULTIBYTE='@MULTIBYTE@'
63

64 65 66 67 68
if [ "$TMPDIR" ]; then
    TEMPFILE="$TMPDIR/initdb.$$"
else
    TEMPFILE="/tmp/initdb.$$"
fi
B
Bruce Momjian 已提交
69

P
Peter Eisentraut 已提交
70 71 72 73 74 75 76 77 78 79 80 81

# 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


B
Bruce Momjian 已提交
82 83 84
#
# Find out where we're located
#
85 86
if echo "$0" | grep '/' > /dev/null 2>&1 
then
B
Bruce Momjian 已提交
87 88 89 90
        # 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)
B
Bruce Momjian 已提交
91 92
        for dir in `echo "$PATH" | sed 's/:/ /g'`
	do
B
Bruce Momjian 已提交
93 94
                # empty entry in path means current dir
                [ -z "$dir" ] && dir='.'
95
                if [ -f "$dir/$CMDNAME" ]
B
Bruce Momjian 已提交
96
		then
B
Bruce Momjian 已提交
97 98 99 100 101 102
                        PGPATH="$dir"
                        break
                fi
        done
fi

103 104 105 106
if [ x"$PGPATH" = x"" ] ; then
    PGPATH=$bindir
fi

B
Bruce Momjian 已提交
107
# Check if needed programs actually exist in path
108
for prog in postgres pg_id
B
Bruce Momjian 已提交
109
do
110
        if [ ! -x "$PGPATH/$prog" ]
B
Bruce Momjian 已提交
111
	then
112
                echo "The program \`$prog' needed by $CMDNAME could not be found. It was"
B
Bruce Momjian 已提交
113 114 115 116 117 118 119 120 121
                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

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

# 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

137

138 139 140 141 142
short_version=`echo $VERSION | sed -e 's!^\([0-9][0-9]*\.[0-9][0-9]*\).*!\1!'`
if [ x"$short_version" = x"" ] ; then
  echo "$CMDNAME: bug: version number is out of format"
  exit 1
fi
143

144 145 146 147 148

##########################################################################
#
# COMMAND LINE OPTIONS

B
Bruce Momjian 已提交
149 150 151 152
# 0 is the default (non-)encoding
MULTIBYTEID=0

# Set defaults:
153 154 155 156
debug=
noclean=
template_only=
show_setting=
B
Bruce Momjian 已提交
157 158 159 160 161

# 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
162
#       a security measure.
163
POSTGRES_SUPERUSERNAME="$EffectiveUser"
164
POSTGRES_SUPERUSERID=`$PGPATH/pg_id -u`
B
Bruce Momjian 已提交
165

166
while [ "$#" -gt 0 ]
B
Bruce Momjian 已提交
167 168 169 170 171 172
do
    case "$1" in
        --help|-\?)
                usage=t
                break
                ;;
173 174 175 176
        --version)
                echo "initdb (PostgreSQL) $VERSION"
                exit 0
                ;;
B
Bruce Momjian 已提交
177
        --debug|-d)
178
                debug=yes
B
Bruce Momjian 已提交
179 180
                echo "Running with debug mode on."
                ;;
181
        --show|-s)
182
        	show_setting=yes
183
        	;;        
B
Bruce Momjian 已提交
184
        --noclean|-n)
185
                noclean=yes
B
Bruce Momjian 已提交
186 187 188
                echo "Running with noclean mode on. Mistakes will not be cleaned up."
                ;;
        --template|-t)
189
                template_only=yes
B
Bruce Momjian 已提交
190 191
                echo "Updating template1 database only."
                ;;
192
# The sysid of the database superuser. Can be freely changed.
B
Bruce Momjian 已提交
193 194 195 196 197 198 199 200 201 202
        --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.
P
Peter Eisentraut 已提交
203 204 205
# Make initdb prompt for the default password of the database superuser.
        --pwprompt|-W)
                PwPrompt=1
B
Bruce Momjian 已提交
206 207 208 209 210 211 212 213 214 215 216 217
                ;;
# 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//'`
                ;;
218 219 220 221
# The directory where the database templates are stored. Normally
# they are in PREFIX/share and this option should be unnecessary.
        -L)
                datadir="$2"
B
Bruce Momjian 已提交
222 223
                shift;;
        -L*)
224
                datadir=`echo $1 | sed 's/^-L//'`
B
Bruce Momjian 已提交
225 226 227
                ;;
# The encoding of the template1 database. Defaults to what you chose
# at configure time. (see above)
228
        --encoding|-E)
B
Bruce Momjian 已提交
229 230
                MULTIBYTE="$2"
                shift;;
P
Peter Eisentraut 已提交
231 232
        --encoding=*)
                MULTIBYTE=`echo $1 | sed 's/^--encoding=//'`
B
Bruce Momjian 已提交
233
                ;;
234 235
        -E*)
                MULTIBYTE=`echo $1 | sed 's/^-E//'`
B
Bruce Momjian 已提交
236
                ;;
P
Peter Eisentraut 已提交
237 238 239 240 241
	-*)
		echo "$CMDNAME: invalid option: $1"
		echo "Try -? for help."
		exit 1
		;;
B
Bruce Momjian 已提交
242
        *)
P
Peter Eisentraut 已提交
243
                PGDATA=$1
B
Bruce Momjian 已提交
244 245 246 247 248
                ;;
    esac
    shift
done

P
Peter Eisentraut 已提交
249
if [ "$usage" ]; then
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    echo "$CMDNAME initialized a PostgreSQL database cluster."
    echo
    echo "Usage:"
    echo "  $CMDNAME [options] datadir"
    echo
    echo "Options:"
    echo " [-D, --pgdata] <datadir>     Location for this database cluster"
    echo "  -W, --pwprompt              Prompt for a password for the new superuser"
    if [ -n "$MULTIBYTE" ] ; then 
        echo "  -E, --encoding <encoding>   Set the default multibyte encoding for new databases"
    fi
    echo "  -i, --sysid <sysid>         Database sysid for the superuser"
    echo "Less commonly used options: "
    echo "  -L <directory>              Where to find the input files"
    echo "  -t, --template              Re-initialize template database only"
    echo "  -d, --debug                 Generate lots of debugging output"
    echo "  -n, --noclean               Do not clean up after errors"
    echo
    echo "Report bugs to <pgsql-bugs@postgresql.org>."
    exit 0
B
Bruce Momjian 已提交
270 271 272 273 274 275
fi

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

276 277
if [ "$MULTIBYTE" ]
then
P
Peter Eisentraut 已提交
278
	MULTIBYTEID=`$PGPATH/pg_encoding $MULTIBYTE 2> /dev/null`
279
        if [ "$?" -ne 0 ]
B
Bruce Momjian 已提交
280
	then
P
Peter Eisentraut 已提交
281 282 283 284
                echo "$CMDNAME: pg_encoding failed"
                echo
                echo "Perhaps you did not configure PostgreSQL for multibyte support or"
                echo "the program was not successfully installed."
B
Bruce Momjian 已提交
285 286
                exit 1
        fi
287 288
	if [ -z "$MULTIBYTEID" ]
	then
P
Peter Eisentraut 已提交
289
		echo "$CMDNAME: $MULTIBYTE is not a valid encoding name"
B
Bruce Momjian 已提交
290 291 292 293 294 295 296 297 298
		exit 1
	fi
fi


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

299 300
if [ -z "$PGDATA" ]
then
B
Bruce Momjian 已提交
301
    echo "$CMDNAME: You must identify where the the data for this database"
302
    echo "system will reside.  Do this with either a -D invocation"
B
Bruce Momjian 已提交
303 304 305 306 307 308 309 310
    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.)

311 312
echo "$PGDATA" | grep '^/' > /dev/null 2>&1
if [ "$?" -ne 0 ]
313
then
P
Peter Eisentraut 已提交
314
    echo "$CMDNAME: data path must be specified as an absolute path"
B
Bruce Momjian 已提交
315 316 317 318 319 320 321 322
    exit 1
fi


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

323 324
TEMPLATE1_BKI="$datadir"/template1.bki
GLOBAL_BKI="$datadir"/global.bki
B
Bruce Momjian 已提交
325

326 327
TEMPLATE1_DESCR="$datadir"/template1.description
GLOBAL_DESCR="$datadir"/global.description
B
Bruce Momjian 已提交
328

329 330
PG_HBA_SAMPLE="$datadir"/pg_hba.conf.sample
POSTGRESQL_CONF_SAMPLE="$datadir"/postgresql.conf.sample
B
Bruce Momjian 已提交
331

332
if [ "$show_setting" = yes ] || [ "$debug" = yes ]
333
then
334 335 336 337 338 339 340 341 342 343 344
    echo
    echo "Initdb variables:"
    for var in PGDATA datadir PGPATH TEMPFILE MULTIBYTE MULTIBYTEID \
        POSTGRES_SUPERUSERNAME POSTGRES_SUPERUSERID TEMPLATE1_BKI GLOBAL_BKI \
        TEMPLATE1_DESCR GLOBAL_DESCR POSTGRESQL_CONF_SAMPLE PG_HBA_SAMPLE ; do
        eval "echo '  '$var=\$$var"
    done
fi

if [ "$show_setting" = yes ] ; then
    exit 0
345 346
fi

347
for PREREQ_FILE in "$TEMPLATE1_BKI" "$GLOBAL_BKI" "$PG_HBA_SAMPLE"
B
Bruce Momjian 已提交
348
do
349
    if [ ! -f "$PREREQ_FILE" ] ; then
B
Bruce Momjian 已提交
350 351
        echo "$CMDNAME does not find the file '$PREREQ_FILE'."
        echo "This means you have a corrupted installation or identified the"
352
        echo "wrong directory with the -L invocation option."
B
Bruce Momjian 已提交
353 354 355 356 357
        exit 1
    fi
done


B
Bruce Momjian 已提交
358
trap 'echo "Caught signal." ; exit_nicely' 1 2 3 15
B
Bruce Momjian 已提交
359

360 361 362 363
# 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
B
Bruce Momjian 已提交
364

365 366 367 368

##########################################################################
#
# CREATE DATABASE DIRECTORY
M
 
Marc G. Fournier 已提交
369 370 371 372

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

373 374 375
# find out if directory is empty
pgdata_contents=`ls -A "$PGDATA" 2>/dev/null`
if [ x"$pgdata_contents" != x ]
376
then
377
    if [ "$template_only" != yes ]
B
Bruce Momjian 已提交
378
    then
379 380 381
        echo "$CMDNAME: The directory $PGDATA is exists but is not empty."
        echo "If you want to create a new database system, either remove or empty"
        echo "the directory $PGDATA or run initdb with an argument"
M
 
Marc G. Fournier 已提交
382 383 384 385
        echo "other than $PGDATA."
        exit 1
    fi
else
386
    if [ ! -d "$PGDATA" ]; then
387
        echo "Creating directory $PGDATA"
388
        mkdir -p "$PGDATA" >/dev/null 2>&1 || mkdir "$PGDATA" || exit_nicely
389
        made_new_pgdata=yes
390
    else
391
        echo "Fixing permissions on existing directory $PGDATA"
392
	chmod go-rwx "$PGDATA" || exit_nicely
M
 
Marc G. Fournier 已提交
393
    fi
394

395 396
    if [ ! -d "$PGDATA"/base ]
	then
397
        echo "Creating directory $PGDATA/base"
398
        mkdir "$PGDATA"/base || exit_nicely
M
 
Marc G. Fournier 已提交
399
    fi
400 401 402 403 404
    if [ ! -d "$PGDATA"/global ]
    then
        echo "Creating directory $PGDATA/global"
        mkdir "$PGDATA"/global || exit_nicely
    fi
405
    if [ ! -d "$PGDATA"/pg_xlog ]
B
Bruce Momjian 已提交
406
    then
407
        echo "Creating directory $PGDATA/pg_xlog"
408
        mkdir "$PGDATA"/pg_xlog || exit_nicely
409
    fi
M
 
Marc G. Fournier 已提交
410 411
fi

412 413 414 415

##########################################################################
#
# CREATE TEMPLATE1 DATABASE
M
 
Marc G. Fournier 已提交
416

417 418
rm -rf "$PGDATA"/base/1 || exit_nicely
mkdir "$PGDATA"/base/1 || exit_nicely
M
 
Marc G. Fournier 已提交
419

420
if [ "$debug" = yes ]
421
then
M
 
Marc G. Fournier 已提交
422 423 424 425 426 427
    BACKEND_TALK_ARG="-d"
else
    BACKEND_TALK_ARG="-Q"
fi

BACKENDARGS="-boot -C -F -D$PGDATA $BACKEND_TALK_ARG"
428
FIRSTRUN="-boot -x -C -F -D$PGDATA $BACKEND_TALK_ARG"
M
 
Marc G. Fournier 已提交
429

430
echo "Creating template database in $PGDATA/base/1"
431
[ "$debug" = yes ] && echo "Running: $PGPATH/postgres $FIRSTRUN template1"
M
 
Marc G. Fournier 已提交
432

433
cat "$TEMPLATE1_BKI" \
434
| sed -e "s/PGUID/$POSTGRES_SUPERUSERID/g" \
435
| "$PGPATH"/postgres $FIRSTRUN template1 \
436
|| exit_nicely
M
 
Marc G. Fournier 已提交
437

438
echo $short_version > "$PGDATA"/base/1/PG_VERSION || exit_nicely
M
 
Marc G. Fournier 已提交
439 440


441 442 443 444 445
##########################################################################
#
# CREATE GLOBAL TABLES

if [ "$template_only" != yes ]
446
then
447
    echo "Creating global relations in $PGDATA/global"
448
    [ "$debug" = yes ] && echo "Running: $PGPATH/postgres $BACKENDARGS template1"
M
 
Marc G. Fournier 已提交
449

450
    cat "$GLOBAL_BKI" \
451 452
    | sed -e "s/POSTGRES/$POSTGRES_SUPERUSERNAME/g" \
          -e "s/PGUID/$POSTGRES_SUPERUSERID/g" \
453
          -e "s/ENCODING/$MULTIBYTEID/g" \
454
    | "$PGPATH"/postgres $BACKENDARGS template1 \
455
    || exit_nicely
M
 
Marc G. Fournier 已提交
456

457
    echo $short_version > "$PGDATA/PG_VERSION" || exit_nicely
M
 
Marc G. Fournier 已提交
458

459
    cp "$PG_HBA_SAMPLE" "$PGDATA"/pg_hba.conf     || exit_nicely
460
    cp "$POSTGRESQL_CONF_SAMPLE" "$PGDATA"/postgresql.conf || exit_nicely
461
    chmod 0600 "$PGDATA"/pg_hba.conf "$PGDATA"/postgresql.conf
M
 
Marc G. Fournier 已提交
462 463 464

fi

465 466 467 468 469

##########################################################################
#
# CREATE VIEWS and other things

M
 
Marc G. Fournier 已提交
470 471
echo

472
PGSQL_OPT="-o /dev/null -O -F -D$PGDATA"
M
 
Marc G. Fournier 已提交
473

B
Bruce Momjian 已提交
474 475
# Create a trigger so that direct updates to pg_shadow will be written
# to the flat password file pg_pwd
476 477
echo "CREATE TRIGGER pg_sync_pg_pwd AFTER INSERT OR UPDATE OR DELETE ON pg_shadow" \
     "FOR EACH ROW EXECUTE PROCEDURE update_pg_pwd()" \
478
     | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
B
Bruce Momjian 已提交
479

P
Peter Eisentraut 已提交
480 481
# needs to be done before alter user
echo "REVOKE ALL on pg_shadow FROM public" \
482
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
483

P
Peter Eisentraut 已提交
484 485 486
# set up password
if [ "$PwPrompt" ]; then
    $ECHO_N "Enter new superuser password: "$ECHO_C
B
Bruce Momjian 已提交
487
    stty -echo > /dev/null 2>&1
P
Peter Eisentraut 已提交
488
    read FirstPw
B
Bruce Momjian 已提交
489
    stty echo > /dev/null 2>&1
P
Peter Eisentraut 已提交
490 491
    echo
    $ECHO_N "Enter it again: "$ECHO_C
B
Bruce Momjian 已提交
492
    stty -echo > /dev/null 2>&1
P
Peter Eisentraut 已提交
493
    read SecondPw
B
Bruce Momjian 已提交
494
    stty echo > /dev/null 2>&1
P
Peter Eisentraut 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508
    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

509

510 511 512 513 514 515 516 517 518
echo "Enabling unlimited row width for system tables."
echo "ALTER TABLE pg_attrdef CREATE TOAST TABLE" \
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
echo "ALTER TABLE pg_description CREATE TOAST TABLE" \
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
echo "ALTER TABLE pg_proc CREATE TOAST TABLE" \
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
echo "ALTER TABLE pg_relcheck CREATE TOAST TABLE" \
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
519 520
echo "ALTER TABLE pg_rewrite CREATE TOAST TABLE" \
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
521 522
echo "ALTER TABLE pg_statistic CREATE TOAST TABLE" \
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
523 524


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

echo "Creating view pg_rules."
540 541 542 543 544 545 546
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' \
547
            AND C.oid = R.ev_class;" \
548
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
549 550

echo "Creating view pg_views."
551 552 553 554 555 556 557 558 559 560
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' \
561
            )" \
562
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
563 564

echo "Creating view pg_tables."
565 566 567 568 569 570 571 572 573 574 575 576
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' \
577
            )" \
578
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
579 580

echo "Creating view pg_indexes."
581 582 583 584 585 586 587
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 \
588
            AND I.oid = X.indexrelid" \
589
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
590 591

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

cat $TEMPFILE \
597
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
598
rm -f "$TEMPFILE" || exit_nicely
599

600 601 602 603 604
echo "Setting lastsysoid."
echo "Update pg_database Set datlastsysoid = (Select max(oid) From pg_description) \
        Where datname = 'template1'" \
		| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely

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

609 610 611 612 613

##########################################################################
#
# FINISHED

614
echo
615
echo "Success. You can now start the database server using:"
616 617 618 619
echo ""
echo "	$PGPATH/postmaster -D $PGDATA"
echo "or"
echo "	$PGPATH/pg_ctl -D $PGDATA start"
620 621 622
echo

exit 0