initdb.sh 20.2 KB
Newer Older
1
#!@SHELL@
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 24
# Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
# Portions 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.125 2001/05/12 01:48:49 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
37 38
    echo 1>&2
    echo "$CMDNAME failed." 1>&2
39
    if [ "$noclean" != yes ]; then
40
        if [ "$made_new_pgdata" = yes ]; then
41 42
            echo "Removing $PGDATA." 1>&2
            rm -rf "$PGDATA" || echo "Failed." 1>&2
43
        fi
44 45
        echo "Removing temp file $TEMPFILE." 1>&2
        rm -rf "$TEMPFILE" || echo "Failed." 1>&2
B
Bruce Momjian 已提交
46
    else
47
        echo "Data directory $PGDATA will not be removed at user's request." 1>&2
B
Bruce Momjian 已提交
48 49 50 51 52 53 54
    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 -V 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 -V 2>/dev/null`" = x"postgres (PostgreSQL) $VERSION" ]
P
Peter Eisentraut 已提交
117 118 119
    then
        PGPATH=$bindir
    else
120 121 122 123 124 125 126 127 128 129 130 131 132
        # Maybe there was an error message?
        errormsg=`$bindir/postgres -V 2>&1 >/dev/null`
      (
        echo "The program "
        echo "    '$bindir/postgres'"
        echo "needed by $CMDNAME does not belong to PostgreSQL version $VERSION, or"
        echo "there may be a configuration problem."
        if test x"$errormsg" != x""; then
            echo
            echo "This was the error message issued by that program:"
            echo "$errormsg"
        fi
      ) 1>&2
P
Peter Eisentraut 已提交
133 134 135
        exit 1
    fi
else
136 137
    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 已提交
138
    exit 1
139 140
fi

P
Peter Eisentraut 已提交
141 142 143 144

# 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
145 146
    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 已提交
147 148
    exit 1
fi
B
Bruce Momjian 已提交
149

150 151 152

EffectiveUser=`$PGPATH/pg_id -n -u`
if [ -z "$EffectiveUser" ]; then
153
    echo "$CMDNAME: could not determine current user name" 1>&2
154 155 156 157 158
    exit 1
fi

if [ `$PGPATH/pg_id -u` -eq 0 ]
then
159 160
    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
161 162 163
    exit 1
fi

164

165 166
short_version=`echo $VERSION | sed -e 's!^\([0-9][0-9]*\.[0-9][0-9]*\).*!\1!'`
if [ x"$short_version" = x"" ] ; then
167
  echo "$CMDNAME: bug: version number has wrong format" 1>&2
168 169
  exit 1
fi
170

171 172 173 174 175

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

B
Bruce Momjian 已提交
176 177 178 179
# 0 is the default (non-)encoding
MULTIBYTEID=0

# Set defaults:
180 181 182
debug=
noclean=
show_setting=
B
Bruce Momjian 已提交
183 184 185 186 187

# 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
188
#       a security measure.
189
POSTGRES_SUPERUSERNAME="$EffectiveUser"
190
POSTGRES_SUPERUSERID=`$PGPATH/pg_id -u`
B
Bruce Momjian 已提交
191

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

P
Peter Eisentraut 已提交
271
if [ "$usage" ]; then
272
    echo "$CMDNAME initializes a PostgreSQL database cluster."
273 274 275 276 277
    echo
    echo "Usage:"
    echo "  $CMDNAME [options] datadir"
    echo
    echo "Options:"
278
    echo " [-D, --pgdata] DATADIR       Location for this database cluster"
279 280
    echo "  -W, --pwprompt              Prompt for a password for the new superuser"
    if [ -n "$MULTIBYTE" ] ; then 
281
        echo "  -E, --encoding ENCODING     Set the default multibyte encoding for new databases"
282
    fi
283
    echo "  -i, --sysid SYSID           Database sysid for the superuser"
284
    echo "Less commonly used options: "
285
    echo "  -L DIRECTORY                Where to find the input files"
286 287 288 289 290
    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 已提交
291 292 293 294 295 296
fi

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

297 298
if [ "$MULTIBYTE" ]
then
P
Peter Eisentraut 已提交
299
	MULTIBYTEID=`$PGPATH/pg_encoding $MULTIBYTE`
300
        if [ "$?" -ne 0 ]
B
Bruce Momjian 已提交
301
	then
302
              (
P
Peter Eisentraut 已提交
303 304 305 306
                echo "$CMDNAME: pg_encoding failed"
                echo
                echo "Perhaps you did not configure PostgreSQL for multibyte support or"
                echo "the program was not successfully installed."
307
              ) 1>&2
B
Bruce Momjian 已提交
308 309
                exit 1
        fi
310 311
	if [ -z "$MULTIBYTEID" ]
	then
312
		echo "$CMDNAME: $MULTIBYTE is not a valid encoding name" 1>&2
B
Bruce Momjian 已提交
313
		exit 1
314 315 316 317
	elif [ $MULTIBYTEID -gt 31 ]
	then
		echo "$CMDNAME: $MULTIBYTE cannot be used as a database encoding" 1>&2
		exit 1
B
Bruce Momjian 已提交
318 319 320 321 322 323 324 325
	fi
fi


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

326 327
if [ -z "$PGDATA" ]
then
328
  (
B
Bruce Momjian 已提交
329
    echo "$CMDNAME: You must identify where the the data for this database"
330
    echo "system will reside.  Do this with either a -D invocation"
B
Bruce Momjian 已提交
331
    echo "option or a PGDATA environment variable."
332
  ) 1>&2
B
Bruce Momjian 已提交
333 334 335 336 337 338 339 340
    exit 1
fi


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

341 342
TEMPLATE1_BKI="$datadir"/template1.bki
GLOBAL_BKI="$datadir"/global.bki
B
Bruce Momjian 已提交
343

344 345
TEMPLATE1_DESCR="$datadir"/template1.description
GLOBAL_DESCR="$datadir"/global.description
B
Bruce Momjian 已提交
346

347
PG_HBA_SAMPLE="$datadir"/pg_hba.conf.sample
348
PG_IDENT_SAMPLE="$datadir"/pg_ident.conf.sample
349
POSTGRESQL_CONF_SAMPLE="$datadir"/postgresql.conf.sample
B
Bruce Momjian 已提交
350

351
if [ "$show_setting" = yes ] || [ "$debug" = yes ]
352
then
353 354 355 356
    echo
    echo "Initdb variables:"
    for var in PGDATA datadir PGPATH TEMPFILE MULTIBYTE MULTIBYTEID \
        POSTGRES_SUPERUSERNAME POSTGRES_SUPERUSERID TEMPLATE1_BKI GLOBAL_BKI \
357 358
        TEMPLATE1_DESCR GLOBAL_DESCR POSTGRESQL_CONF_SAMPLE \
	PG_HBA_SAMPLE PG_IDENT_SAMPLE ; do
359 360 361 362 363 364
        eval "echo '  '$var=\$$var"
    done
fi

if [ "$show_setting" = yes ] ; then
    exit 0
365 366
fi

367 368
for PREREQ_FILE in "$TEMPLATE1_BKI" "$GLOBAL_BKI" "$PG_HBA_SAMPLE" \
    "$PG_IDENT_SAMPLE"
B
Bruce Momjian 已提交
369
do
370
    if [ ! -f "$PREREQ_FILE" ] ; then
371
      (
B
Bruce Momjian 已提交
372 373
        echo "$CMDNAME does not find the file '$PREREQ_FILE'."
        echo "This means you have a corrupted installation or identified the"
374
        echo "wrong directory with the -L invocation option."
375
      ) 1>&2
B
Bruce Momjian 已提交
376 377 378 379
        exit 1
    fi
done

P
Peter Eisentraut 已提交
380 381
for file in "$TEMPLATE1_BKI" "$GLOBAL_BKI"; do
     if [ x"`sed 1q $file`" != x"# PostgreSQL $short_version" ]; then
382
       (
P
Peter Eisentraut 已提交
383 384 385
         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."
386
       ) 1>&2
P
Peter Eisentraut 已提交
387 388 389 390
         exit 1
     fi
done

B
Bruce Momjian 已提交
391

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

394 395 396 397
# 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 已提交
398

399 400 401 402

##########################################################################
#
# CREATE DATABASE DIRECTORY
M
 
Marc G. Fournier 已提交
403 404 405 406

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

407 408 409
# find out if directory is empty
pgdata_contents=`ls -A "$PGDATA" 2>/dev/null`
if [ x"$pgdata_contents" != x ]
410
then
411 412 413 414 415 416 417
    (
      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 已提交
418
else
419
    if [ ! -d "$PGDATA" ]; then
420
        echo "Creating directory $PGDATA"
421
        mkdir -p "$PGDATA" >/dev/null 2>&1 || mkdir "$PGDATA" || exit_nicely
422
        made_new_pgdata=yes
423
    else
424
        echo "Fixing permissions on existing directory $PGDATA"
425
	chmod go-rwx "$PGDATA" || exit_nicely
M
 
Marc G. Fournier 已提交
426
    fi
427

428 429
    if [ ! -d "$PGDATA"/base ]
	then
430
        echo "Creating directory $PGDATA/base"
431
        mkdir "$PGDATA"/base || exit_nicely
M
 
Marc G. Fournier 已提交
432
    fi
433 434 435 436 437
    if [ ! -d "$PGDATA"/global ]
    then
        echo "Creating directory $PGDATA/global"
        mkdir "$PGDATA"/global || exit_nicely
    fi
438
    if [ ! -d "$PGDATA"/pg_xlog ]
B
Bruce Momjian 已提交
439
    then
440
        echo "Creating directory $PGDATA/pg_xlog"
441
        mkdir "$PGDATA"/pg_xlog || exit_nicely
442
    fi
M
 
Marc G. Fournier 已提交
443 444
fi

445 446 447 448

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

450 451
rm -rf "$PGDATA"/base/1 || exit_nicely
mkdir "$PGDATA"/base/1 || exit_nicely
M
 
Marc G. Fournier 已提交
452

453
if [ "$debug" = yes ]
454
then
M
 
Marc G. Fournier 已提交
455 456 457
    BACKEND_TALK_ARG="-d"
fi

458 459
BACKENDARGS="-boot -F -D$PGDATA $BACKEND_TALK_ARG"
FIRSTRUN="-boot -x1 -F -D$PGDATA $BACKEND_TALK_ARG"
M
 
Marc G. Fournier 已提交
460

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

464
cat "$TEMPLATE1_BKI" \
465
| sed -e "s/PGUID/$POSTGRES_SUPERUSERID/g" \
466
| "$PGPATH"/postgres $FIRSTRUN template1 \
467
|| exit_nicely
M
 
Marc G. Fournier 已提交
468

469
echo $short_version > "$PGDATA"/base/1/PG_VERSION || exit_nicely
M
 
Marc G. Fournier 已提交
470 471


472 473 474
##########################################################################
#
# CREATE GLOBAL TABLES
475
#
476

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

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
[ "$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 已提交
495

496 497 498 499 500

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

501
echo "Initializing pg_shadow."
M
 
Marc G. Fournier 已提交
502

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

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

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

P
Peter Eisentraut 已提交
515 516 517
# set up password
if [ "$PwPrompt" ]; then
    $ECHO_N "Enter new superuser password: "$ECHO_C
B
Bruce Momjian 已提交
518
    stty -echo > /dev/null 2>&1
P
Peter Eisentraut 已提交
519
    read FirstPw
B
Bruce Momjian 已提交
520
    stty echo > /dev/null 2>&1
P
Peter Eisentraut 已提交
521 522
    echo
    $ECHO_N "Enter it again: "$ECHO_C
B
Bruce Momjian 已提交
523
    stty -echo > /dev/null 2>&1
P
Peter Eisentraut 已提交
524
    read SecondPw
B
Bruce Momjian 已提交
525
    stty echo > /dev/null 2>&1
P
Peter Eisentraut 已提交
526 527
    echo
    if [ "$FirstPw" != "$SecondPw" ]; then
528
        echo "Passwords didn't match." 1>&2
P
Peter Eisentraut 已提交
529 530 531 532
        exit_nicely
    fi
    echo "ALTER USER \"$POSTGRES_SUPERUSERNAME\" WITH PASSWORD '$FirstPw'" \
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
533
    if [ ! -f $PGDATA/global/pg_pwd ]; then
534
        echo "The password file wasn't generated. Please report this problem." 1>&2
P
Peter Eisentraut 已提交
535 536 537 538 539
        exit_nicely
    fi
    echo "Setting password"
fi

540

541
echo "Enabling unlimited row width for system tables."
542

543 544 545 546 547 548 549 550
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
551 552
echo "ALTER TABLE pg_rewrite CREATE TOAST TABLE" \
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
553 554
echo "ALTER TABLE pg_statistic CREATE TOAST TABLE" \
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
555 556


557 558
echo "Creating system views."

559 560 561 562 563 564 565 566 567 568
echo "CREATE VIEW pg_user AS \
        SELECT \
            usename, \
            usesysid, \
            usecreatedb, \
            usetrace, \
            usesuper, \
            usecatupd, \
            '********'::text as passwd, \
            valuntil \
569
        FROM pg_shadow" \
570
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
571

572 573 574 575 576 577 578
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' \
579
            AND C.oid = R.ev_class;" \
580
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
581

582 583 584 585 586 587
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 \
588
        WHERE C.relkind = 'v';" \
589
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
590

591 592
# XXX why does pg_tables include sequences?

593 594 595 596 597 598 599 600
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 \
601
        WHERE C.relkind IN ('r', 's');" \
602
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
603

604 605 606 607 608 609
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 \
610 611 612
	WHERE C.relkind = 'r' AND I.relkind = 'i' \
	    AND C.oid = X.indrelid \
            AND I.oid = X.indexrelid;" \
613
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
614 615

echo "Loading pg_description."
616
echo "COPY pg_description FROM STDIN" > $TEMPFILE
617
cat "$TEMPLATE1_DESCR" >> $TEMPFILE
618 619 620
cat "$GLOBAL_DESCR" >> $TEMPFILE

cat $TEMPFILE \
621
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
622
rm -f "$TEMPFILE" || exit_nicely
623

624
echo "Setting lastsysoid."
625 626 627 628
echo "UPDATE pg_database SET \
	datistemplate = 't', \
	datlastsysoid = (SELECT max(oid) FROM pg_description) \
        WHERE datname = 'template1'" \
629 630
		| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely

631 632
echo "Vacuuming database."
echo "VACUUM ANALYZE" \
633
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
634

635 636 637 638 639 640 641 642 643 644 645
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

646 647 648 649 650

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

651
echo
652
echo "Success. You can now start the database server using:"
653
echo ""
654
echo "    $PGPATH/postmaster -D $PGDATA"
655
echo "or"
656 657 658
# (Advertise -l option here, otherwise we have a background
#  process writing to the terminal.)
echo "    $PGPATH/pg_ctl -D $PGDATA -l logfile start"
659 660 661
echo

exit 0