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 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.121 2001/02/18 18:33:59 momjian 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
        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 已提交
122 123 124
        exit 1
    fi
else
125 126
    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 已提交
127
    exit 1
128 129
fi

P
Peter Eisentraut 已提交
130 131 132 133

# 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
134 135
    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 已提交
136 137
    exit 1
fi
B
Bruce Momjian 已提交
138

139 140 141

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

if [ `$PGPATH/pg_id -u` -eq 0 ]
then
148 149
    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
150 151 152
    exit 1
fi

153

154 155
short_version=`echo $VERSION | sed -e 's!^\([0-9][0-9]*\.[0-9][0-9]*\).*!\1!'`
if [ x"$short_version" = x"" ] ; then
156
  echo "$CMDNAME: bug: version number has wrong format" 1>&2
157 158
  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
debug=
noclean=
show_setting=
B
Bruce Momjian 已提交
172 173 174 175 176

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

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

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

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

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


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

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


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

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

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

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

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

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

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

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

B
Bruce Momjian 已提交
376

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

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

384 385 386 387

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

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

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

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

430 431 432 433

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

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

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

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

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

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

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


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

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

466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
[ "$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 已提交
482

483 484 485 486 487

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

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

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

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

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

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

527

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

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


544 545
echo "Creating system views."

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

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

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

578 579
# XXX why does pg_tables include sequences?

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

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

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

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

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

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

622 623 624 625 626 627 628 629 630 631 632
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

633 634 635 636 637

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

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

exit 0