initdb.sh 19.4 KB
Newer Older
1
#! /bin/sh
M
 
Marc G. Fournier 已提交
2 3
#-------------------------------------------------------------------------
#
P
Peter Eisentraut 已提交
4
# initdb creates (initializes) a PostgreSQL database cluster (site,
5
# instance, installation, whatever).  A database cluster is a
P
Peter Eisentraut 已提交
6
# collection of PostgreSQL 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
#
P
Peter Eisentraut 已提交
13 14
# The template database is an ordinary PostgreSQL database.  Its data
# never changes, though.  It exists to make it easy for PostgreSQL to 
15
# 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.110 2000/11/04 12:47:48 petere 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
        # explicit dir name given
P
Peter Eisentraut 已提交
88
        self_path=`echo $0 | sed 's,/[^/]*$,,'`       # (dirname command is not portable)
B
Bruce Momjian 已提交
89 90
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
P
Peter Eisentraut 已提交
97
                        self_path="$dir"
B
Bruce Momjian 已提交
98 99 100 101 102
                        break
                fi
        done
fi

P
Peter Eisentraut 已提交
103 104 105 106 107 108 109 110 111

# Check for right version of backend.  First we check for an
# executable in the same directory is this initdb script (presuming
# the above code worked).  Then we fall back to the hard-wired bindir.
# We do it in this order because during upgrades users might move
# their trees to backup places, so $bindir might be inaccurate.

if [ x"$self_path" != x"" ] \
  && [ -x "$self_path/postgres" ] \
112
  && [ x"`$self_path/postgres --version 2>/dev/null`" = x"postgres (PostgreSQL) $VERSION" ]
P
Peter Eisentraut 已提交
113 114 115
then
    PGPATH=$self_path
elif [ -x "$bindir/postgres" ]; then
116
    if [ x"`$bindir/postgres --version 2>/dev/null`" = x"postgres (PostgreSQL) $VERSION" ]
P
Peter Eisentraut 已提交
117 118 119 120 121 122 123 124 125 126 127
    then
        PGPATH=$bindir
    else
        echo "The program '$bindir/postgres' needed by $CMDNAME does not belong to"
        echo "PostgreSQL version $VERSION.  Check your installation."
        exit 1
    fi
else
    echo "The program 'postgres' is needed by $CMDNAME but was not found in"
    echo "the directory '$bindir'.  Check your installation."
    exit 1
128 129
fi

P
Peter Eisentraut 已提交
130 131 132 133 134 135 136 137

# Now we can assume that 'pg_id' belongs to the same version as the
# verified 'postgres' in the same directory.
if [ ! -x "$PGPATH/pg_id" ]; then
    echo "The program 'pg_id' is needed by $CMDNAME but was not found in"
    echo "the directory '$PGPATH'.  Check your installation."
    exit 1
fi
B
Bruce Momjian 已提交
138

139 140 141

EffectiveUser=`$PGPATH/pg_id -n -u`
if [ -z "$EffectiveUser" ]; then
P
Peter Eisentraut 已提交
142
    echo "$CMDNAME: could not determine current user name"
143 144 145 146 147 148 149 150 151 152
    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

153

154 155 156 157 158
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
159

160 161 162 163 164

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

B
Bruce Momjian 已提交
165 166 167 168
# 0 is the default (non-)encoding
MULTIBYTEID=0

# Set defaults:
169 170 171 172
debug=
noclean=
template_only=
show_setting=
B
Bruce Momjian 已提交
173 174 175 176 177

# 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
178
#       a security measure.
179
POSTGRES_SUPERUSERNAME="$EffectiveUser"
180
POSTGRES_SUPERUSERID=`$PGPATH/pg_id -u`
B
Bruce Momjian 已提交
181

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

P
Peter Eisentraut 已提交
265
if [ "$usage" ]; then
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    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 已提交
286 287 288 289 290 291
fi

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

292 293
if [ "$MULTIBYTE" ]
then
P
Peter Eisentraut 已提交
294
	MULTIBYTEID=`$PGPATH/pg_encoding $MULTIBYTE`
295
        if [ "$?" -ne 0 ]
B
Bruce Momjian 已提交
296
	then
P
Peter Eisentraut 已提交
297 298 299 300
                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 已提交
301 302
                exit 1
        fi
303 304
	if [ -z "$MULTIBYTEID" ]
	then
P
Peter Eisentraut 已提交
305
		echo "$CMDNAME: $MULTIBYTE is not a valid encoding name"
B
Bruce Momjian 已提交
306 307 308 309 310 311 312 313 314
		exit 1
	fi
fi


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

315 316
if [ -z "$PGDATA" ]
then
B
Bruce Momjian 已提交
317
    echo "$CMDNAME: You must identify where the the data for this database"
318
    echo "system will reside.  Do this with either a -D invocation"
B
Bruce Momjian 已提交
319 320 321 322 323 324 325 326 327 328
    echo "option or a PGDATA environment variable."
    echo
    exit 1
fi


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

329 330
TEMPLATE1_BKI="$datadir"/template1.bki
GLOBAL_BKI="$datadir"/global.bki
B
Bruce Momjian 已提交
331

332 333
TEMPLATE1_DESCR="$datadir"/template1.description
GLOBAL_DESCR="$datadir"/global.description
B
Bruce Momjian 已提交
334

335 336
PG_HBA_SAMPLE="$datadir"/pg_hba.conf.sample
POSTGRESQL_CONF_SAMPLE="$datadir"/postgresql.conf.sample
B
Bruce Momjian 已提交
337

338
if [ "$show_setting" = yes ] || [ "$debug" = yes ]
339
then
340 341 342 343 344 345 346 347 348 349 350
    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
351 352
fi

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

P
Peter Eisentraut 已提交
363 364 365 366 367 368 369 370 371
for file in "$TEMPLATE1_BKI" "$GLOBAL_BKI"; do
     if [ x"`sed 1q $file`" != x"# PostgreSQL $short_version" ]; then
         echo "The input file '$file' needed by $CMDNAME does not"
         echo "belong to PostgreSQL $VERSION.  Check your installation or specify the"
         echo "correct path using the -L option."
         exit 1
     fi
done

B
Bruce Momjian 已提交
372

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

375 376 377 378
# 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 已提交
379

380 381 382 383

##########################################################################
#
# CREATE DATABASE DIRECTORY
M
 
Marc G. Fournier 已提交
384 385 386 387

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

388 389 390
# find out if directory is empty
pgdata_contents=`ls -A "$PGDATA" 2>/dev/null`
if [ x"$pgdata_contents" != x ]
391
then
392
    if [ "$template_only" != yes ]
B
Bruce Momjian 已提交
393
    then
394 395 396
        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 已提交
397 398 399 400
        echo "other than $PGDATA."
        exit 1
    fi
else
401
    if [ ! -d "$PGDATA" ]; then
402
        echo "Creating directory $PGDATA"
403
        mkdir -p "$PGDATA" >/dev/null 2>&1 || mkdir "$PGDATA" || exit_nicely
404
        made_new_pgdata=yes
405
    else
406
        echo "Fixing permissions on existing directory $PGDATA"
407
	chmod go-rwx "$PGDATA" || exit_nicely
M
 
Marc G. Fournier 已提交
408
    fi
409

410 411
    if [ ! -d "$PGDATA"/base ]
	then
412
        echo "Creating directory $PGDATA/base"
413
        mkdir "$PGDATA"/base || exit_nicely
M
 
Marc G. Fournier 已提交
414
    fi
415 416 417 418 419
    if [ ! -d "$PGDATA"/global ]
    then
        echo "Creating directory $PGDATA/global"
        mkdir "$PGDATA"/global || exit_nicely
    fi
420
    if [ ! -d "$PGDATA"/pg_xlog ]
B
Bruce Momjian 已提交
421
    then
422
        echo "Creating directory $PGDATA/pg_xlog"
423
        mkdir "$PGDATA"/pg_xlog || exit_nicely
424
    fi
M
 
Marc G. Fournier 已提交
425 426
fi

427 428 429 430

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

432 433
rm -rf "$PGDATA"/base/1 || exit_nicely
mkdir "$PGDATA"/base/1 || exit_nicely
M
 
Marc G. Fournier 已提交
434

435
if [ "$debug" = yes ]
436
then
M
 
Marc G. Fournier 已提交
437 438 439 440 441 442
    BACKEND_TALK_ARG="-d"
else
    BACKEND_TALK_ARG="-Q"
fi

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

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

448
cat "$TEMPLATE1_BKI" \
449
| sed -e "s/PGUID/$POSTGRES_SUPERUSERID/g" \
450
| "$PGPATH"/postgres $FIRSTRUN template1 \
451
|| exit_nicely
M
 
Marc G. Fournier 已提交
452

453
echo $short_version > "$PGDATA"/base/1/PG_VERSION || exit_nicely
M
 
Marc G. Fournier 已提交
454 455


456 457 458 459 460
##########################################################################
#
# CREATE GLOBAL TABLES

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

465
    cat "$GLOBAL_BKI" \
466 467
    | sed -e "s/POSTGRES/$POSTGRES_SUPERUSERNAME/g" \
          -e "s/PGUID/$POSTGRES_SUPERUSERID/g" \
468
          -e "s/ENCODING/$MULTIBYTEID/g" \
469
    | "$PGPATH"/postgres $BACKENDARGS template1 \
470
    || exit_nicely
M
 
Marc G. Fournier 已提交
471

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

474
    cp "$PG_HBA_SAMPLE" "$PGDATA"/pg_hba.conf     || exit_nicely
475
    cp "$POSTGRESQL_CONF_SAMPLE" "$PGDATA"/postgresql.conf || exit_nicely
476
    chmod 0600 "$PGDATA"/pg_hba.conf "$PGDATA"/postgresql.conf
M
 
Marc G. Fournier 已提交
477 478 479

fi

480 481 482 483 484

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

M
 
Marc G. Fournier 已提交
485 486
echo

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

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

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

P
Peter Eisentraut 已提交
499 500 501
# set up password
if [ "$PwPrompt" ]; then
    $ECHO_N "Enter new superuser password: "$ECHO_C
B
Bruce Momjian 已提交
502
    stty -echo > /dev/null 2>&1
P
Peter Eisentraut 已提交
503
    read FirstPw
B
Bruce Momjian 已提交
504
    stty echo > /dev/null 2>&1
P
Peter Eisentraut 已提交
505 506
    echo
    $ECHO_N "Enter it again: "$ECHO_C
B
Bruce Momjian 已提交
507
    stty -echo > /dev/null 2>&1
P
Peter Eisentraut 已提交
508
    read SecondPw
B
Bruce Momjian 已提交
509
    stty echo > /dev/null 2>&1
P
Peter Eisentraut 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523
    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

524

525 526 527 528 529 530 531 532 533
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
534 535
echo "ALTER TABLE pg_rewrite CREATE TOAST TABLE" \
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
536 537
echo "ALTER TABLE pg_statistic CREATE TOAST TABLE" \
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
538 539


540
echo "Creating view pg_user."
541 542 543 544 545 546 547 548 549 550
echo "CREATE VIEW pg_user AS \
        SELECT \
            usename, \
            usesysid, \
            usecreatedb, \
            usetrace, \
            usesuper, \
            usecatupd, \
            '********'::text as passwd, \
            valuntil \
551
        FROM pg_shadow" \
552
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
553 554

echo "Creating view pg_rules."
555 556 557 558 559 560 561
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' \
562
            AND C.oid = R.ev_class;" \
563
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
564 565

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

echo "Creating view pg_tables."
580 581 582 583 584 585 586 587 588 589 590 591
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' \
592
            )" \
593
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
594 595

echo "Creating view pg_indexes."
596 597 598 599 600 601 602
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 \
603
            AND I.oid = X.indexrelid" \
604
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
605 606

echo "Loading pg_description."
607
echo "COPY pg_description FROM STDIN" > $TEMPFILE
608
cat "$TEMPLATE1_DESCR" >> $TEMPFILE
609 610 611
cat "$GLOBAL_DESCR" >> $TEMPFILE

cat $TEMPFILE \
612
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
613
rm -f "$TEMPFILE" || exit_nicely
614

615 616 617 618 619
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

620 621
echo "Vacuuming database."
echo "VACUUM ANALYZE" \
622
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
623

624 625 626 627 628

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

629
echo
630
echo "Success. You can now start the database server using:"
631 632 633 634
echo ""
echo "	$PGPATH/postmaster -D $PGDATA"
echo "or"
echo "	$PGPATH/pg_ctl -D $PGDATA start"
635 636 637
echo

exit 0