initdb.sh 19.6 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
# To create the database cluster, we create the directory that contains
# all its data, create the files that hold the global tables, create
10 11
# a few other control files for it, and create two databases: the
# template0 and template1 databases.
M
 
Marc G. Fournier 已提交
12
#
13 14 15 16
# The template databases are ordinary PostgreSQL databases.  template0
# is never supposed to change after initdb, whereas template1 can be
# changed to add site-local standard data.  Either one can be copied
# to produce a new database.
M
 
Marc G. Fournier 已提交
17
#
18 19 20
# To create template1, we run the postgres (backend) program and
# feed it data from the bki files that were installed.  template0 is
# made just by copying the completed template1.
M
 
Marc G. Fournier 已提交
21 22
#
#
23
# Copyright (c) 1994, Regents of the University of California
M
 
Marc G. Fournier 已提交
24
#
25
# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.120 2001/01/20 22:09:24 tgl Exp $
B
Bruce Momjian 已提交
26 27 28
#
#-------------------------------------------------------------------------

29 30 31 32 33

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

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


CMDNAME=`basename $0`

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

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

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

# 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 已提交
81 82 83
#
# Find out where we're located
#
84 85
if echo "$0" | grep '/' > /dev/null 2>&1 
then
B
Bruce Momjian 已提交
86
        # explicit dir name given
P
Peter Eisentraut 已提交
87
        self_path=`echo $0 | sed 's,/[^/]*$,,'`       # (dirname command is not portable)
B
Bruce Momjian 已提交
88 89
else
        # look for it in PATH ('which' command is not portable)
B
Bruce Momjian 已提交
90 91
        for dir in `echo "$PATH" | sed 's/:/ /g'`
	do
B
Bruce Momjian 已提交
92 93
                # empty entry in path means current dir
                [ -z "$dir" ] && dir='.'
94
                if [ -f "$dir/$CMDNAME" ]
B
Bruce Momjian 已提交
95
		then
P
Peter Eisentraut 已提交
96
                        self_path="$dir"
B
Bruce Momjian 已提交
97 98 99 100 101
                        break
                fi
        done
fi

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

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

P
Peter Eisentraut 已提交
129 130 131 132

# 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
133 134
    echo "The program 'pg_id' is needed by $CMDNAME but was not found in" 1>&2
    echo "the directory '$PGPATH'.  Check your installation." 1>&2
P
Peter Eisentraut 已提交
135 136
    exit 1
fi
B
Bruce Momjian 已提交
137

138 139 140

EffectiveUser=`$PGPATH/pg_id -n -u`
if [ -z "$EffectiveUser" ]; then
141
    echo "$CMDNAME: could not determine current user name" 1>&2
142 143 144 145 146
    exit 1
fi

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

152

153 154
short_version=`echo $VERSION | sed -e 's!^\([0-9][0-9]*\.[0-9][0-9]*\).*!\1!'`
if [ x"$short_version" = x"" ] ; then
155
  echo "$CMDNAME: bug: version number has wrong format" 1>&2
156 157
  exit 1
fi
158

159 160 161 162 163

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

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

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

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

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

P
Peter Eisentraut 已提交
259
if [ "$usage" ]; then
260
    echo "$CMDNAME initializes a PostgreSQL database cluster."
261 262 263 264 265
    echo
    echo "Usage:"
    echo "  $CMDNAME [options] datadir"
    echo
    echo "Options:"
266
    echo " [-D, --pgdata] DATADIR       Location for this database cluster"
267 268
    echo "  -W, --pwprompt              Prompt for a password for the new superuser"
    if [ -n "$MULTIBYTE" ] ; then 
269
        echo "  -E, --encoding ENCODING     Set the default multibyte encoding for new databases"
270
    fi
271
    echo "  -i, --sysid SYSID           Database sysid for the superuser"
272
    echo "Less commonly used options: "
273
    echo "  -L DIRECTORY                Where to find the input files"
274 275 276 277 278
    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 已提交
279 280 281 282 283 284
fi

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

285 286
if [ "$MULTIBYTE" ]
then
P
Peter Eisentraut 已提交
287
	MULTIBYTEID=`$PGPATH/pg_encoding $MULTIBYTE`
288
        if [ "$?" -ne 0 ]
B
Bruce Momjian 已提交
289
	then
290
              (
P
Peter Eisentraut 已提交
291 292 293 294
                echo "$CMDNAME: pg_encoding failed"
                echo
                echo "Perhaps you did not configure PostgreSQL for multibyte support or"
                echo "the program was not successfully installed."
295
              ) 1>&2
B
Bruce Momjian 已提交
296 297
                exit 1
        fi
298 299
	if [ -z "$MULTIBYTEID" ]
	then
300
		echo "$CMDNAME: $MULTIBYTE is not a valid encoding name" 1>&2
B
Bruce Momjian 已提交
301 302 303 304 305 306 307 308 309
		exit 1
	fi
fi


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

310 311
if [ -z "$PGDATA" ]
then
312
  (
B
Bruce Momjian 已提交
313
    echo "$CMDNAME: You must identify where the the data for this database"
314
    echo "system will reside.  Do this with either a -D invocation"
B
Bruce Momjian 已提交
315
    echo "option or a PGDATA environment variable."
316
  ) 1>&2
B
Bruce Momjian 已提交
317 318 319 320 321 322 323 324
    exit 1
fi


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

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

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

331
PG_HBA_SAMPLE="$datadir"/pg_hba.conf.sample
332
PG_IDENT_SAMPLE="$datadir"/pg_ident.conf.sample
333
POSTGRESQL_CONF_SAMPLE="$datadir"/postgresql.conf.sample
B
Bruce Momjian 已提交
334

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

if [ "$show_setting" = yes ] ; then
    exit 0
349 350
fi

351 352
for PREREQ_FILE in "$TEMPLATE1_BKI" "$GLOBAL_BKI" "$PG_HBA_SAMPLE" \
    "$PG_IDENT_SAMPLE"
B
Bruce Momjian 已提交
353
do
354
    if [ ! -f "$PREREQ_FILE" ] ; then
355
      (
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."
359
      ) 1>&2
B
Bruce Momjian 已提交
360 361 362 363
        exit 1
    fi
done

P
Peter Eisentraut 已提交
364 365
for file in "$TEMPLATE1_BKI" "$GLOBAL_BKI"; do
     if [ x"`sed 1q $file`" != x"# PostgreSQL $short_version" ]; then
366
       (
P
Peter Eisentraut 已提交
367 368 369
         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."
370
       ) 1>&2
P
Peter Eisentraut 已提交
371 372 373 374
         exit 1
     fi
done

B
Bruce Momjian 已提交
375

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

378 379 380 381
# 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 已提交
382

383 384 385 386

##########################################################################
#
# CREATE DATABASE DIRECTORY
M
 
Marc G. Fournier 已提交
387 388 389 390

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

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

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

429 430 431 432

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

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

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

BACKENDARGS="-boot -C -F -D$PGDATA $BACKEND_TALK_ARG"
V
Vadim B. Mikheev 已提交
445
FIRSTRUN="-boot -x1 -C -F -D$PGDATA $BACKEND_TALK_ARG"
M
 
Marc G. Fournier 已提交
446

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

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

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


458 459 460
##########################################################################
#
# CREATE GLOBAL TABLES
461
#
462

463
echo "Creating global relations in $PGDATA/global"
M
 
Marc G. Fournier 已提交
464

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
[ "$debug" = yes ] && echo "Running: $PGPATH/postgres $BACKENDARGS template1"

cat "$GLOBAL_BKI" \
| sed -e "s/POSTGRES/$POSTGRES_SUPERUSERNAME/g" \
      -e "s/PGUID/$POSTGRES_SUPERUSERID/g" \
      -e "s/ENCODING/$MULTIBYTEID/g" \
| "$PGPATH"/postgres $BACKENDARGS template1 \
|| exit_nicely

echo $short_version > "$PGDATA/PG_VERSION" || exit_nicely

cp "$PG_HBA_SAMPLE" "$PGDATA"/pg_hba.conf              || exit_nicely
cp "$PG_IDENT_SAMPLE" "$PGDATA"/pg_ident.conf          || exit_nicely
cp "$POSTGRESQL_CONF_SAMPLE" "$PGDATA"/postgresql.conf || exit_nicely
chmod 0600 "$PGDATA"/pg_hba.conf "$PGDATA"/pg_ident.conf \
	"$PGDATA"/postgresql.conf
M
 
Marc G. Fournier 已提交
481

482 483 484 485 486

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

487
echo "Initializing pg_shadow."
M
 
Marc G. Fournier 已提交
488

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

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

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

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

526

527
echo "Enabling unlimited row width for system tables."
528

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


543 544
echo "Creating system views."

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

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

568 569 570 571 572 573
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 \
574
        WHERE C.relkind = 'v';" \
575
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
576

577 578
# XXX why does pg_tables include sequences?

579 580 581 582 583 584 585 586
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 \
587
        WHERE C.relkind IN ('r', 's');" \
588
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
589

590 591 592 593 594 595
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 \
596 597 598
	WHERE C.relkind = 'r' AND I.relkind = 'i' \
	    AND C.oid = X.indrelid \
            AND I.oid = X.indexrelid;" \
599
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
600 601

echo "Loading pg_description."
602
echo "COPY pg_description FROM STDIN" > $TEMPFILE
603
cat "$TEMPLATE1_DESCR" >> $TEMPFILE
604 605 606
cat "$GLOBAL_DESCR" >> $TEMPFILE

cat $TEMPFILE \
607
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
608
rm -f "$TEMPFILE" || exit_nicely
609

610
echo "Setting lastsysoid."
611 612 613 614
echo "UPDATE pg_database SET \
	datistemplate = 't', \
	datlastsysoid = (SELECT max(oid) FROM pg_description) \
        WHERE datname = 'template1'" \
615 616
		| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely

617 618
echo "Vacuuming database."
echo "VACUUM ANALYZE" \
619
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
620

621 622 623 624 625 626 627 628 629 630 631
echo "Copying template1 to template0."
echo "CREATE DATABASE template0" \
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
echo "UPDATE pg_database SET \
	datistemplate = 't', \
	datallowconn = 'f' \
        WHERE datname = 'template0'" \
		| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
echo "VACUUM pg_database" \
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely

632 633 634 635 636

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

637
echo
638
echo "Success. You can now start the database server using:"
639 640 641 642
echo ""
echo "	$PGPATH/postmaster -D $PGDATA"
echo "or"
echo "	$PGPATH/pg_ctl -D $PGDATA start"
643 644 645
echo

exit 0