initdb.sh 20 KB
Newer Older
1
#!@SHELL@
2 3
#-------------------------------------------------------------------------
#
4
# initdb creates (initializes) a PostgreSQL database cluster (site,
5
# instance, installation, whatever).  A database cluster is a
6
# collection of PostgreSQL databases all managed by the same postmaster.
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.
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.
17
#
18 19 20 21 22 23 24
# To create template1, we run the postgres (backend) program in bootstrap
# mode and feed it data from the postgres.bki library file.  After this
# initial bootstrap phase, some additional stuff is created by normal
# SQL commands fed to a standalone backend.  Those commands are just
# embedded into this script (yeah, it's ugly).
#
# template0 is made just by copying the completed template1.
25 26
#
#
27 28
# Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
# Portions Copyright (c) 1994, Regents of the University of California
29
#
30
# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.126 2001/06/12 05:55:50 tgl Exp $
Bruce Momjian's avatar
Bruce Momjian committed
31 32 33
#
#-------------------------------------------------------------------------

34 35 36 37 38

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

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


CMDNAME=`basename $0`

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

68 69 70 71 72
if [ "$TMPDIR" ]; then
    TEMPFILE="$TMPDIR/initdb.$$"
else
    TEMPFILE="/tmp/initdb.$$"
fi
Bruce Momjian's avatar
Bruce Momjian committed
73

Peter Eisentraut's avatar
Peter Eisentraut committed
74 75 76 77 78 79 80 81 82 83 84 85

# 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


Bruce Momjian's avatar
Bruce Momjian committed
86 87 88
#
# Find out where we're located
#
89 90
if echo "$0" | grep '/' > /dev/null 2>&1 
then
Bruce Momjian's avatar
Bruce Momjian committed
91
        # explicit dir name given
92
        self_path=`echo $0 | sed 's,/[^/]*$,,'`       # (dirname command is not portable)
Bruce Momjian's avatar
Bruce Momjian committed
93 94
else
        # look for it in PATH ('which' command is not portable)
Bruce Momjian's avatar
Bruce Momjian committed
95 96
        for dir in `echo "$PATH" | sed 's/:/ /g'`
	do
Bruce Momjian's avatar
Bruce Momjian committed
97 98
                # empty entry in path means current dir
                [ -z "$dir" ] && dir='.'
99
                if [ -f "$dir/$CMDNAME" ]
Bruce Momjian's avatar
Bruce Momjian committed
100
		then
101
                        self_path="$dir"
Bruce Momjian's avatar
Bruce Momjian committed
102 103 104 105 106
                        break
                fi
        done
fi

107 108 109 110 111 112 113 114 115

# 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" ] \
116
  && [ x"`$self_path/postgres -V 2>/dev/null`" = x"postgres (PostgreSQL) $VERSION" ]
117 118 119
then
    PGPATH=$self_path
elif [ -x "$bindir/postgres" ]; then
120
    if [ x"`$bindir/postgres -V 2>/dev/null`" = x"postgres (PostgreSQL) $VERSION" ]
121 122 123
    then
        PGPATH=$bindir
    else
124 125 126 127 128 129 130 131 132 133 134 135 136
        # 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
137 138 139
        exit 1
    fi
else
140 141
    echo "The program 'postgres' is needed by $CMDNAME but was not found in" 1>&2
    echo "the directory '$bindir'.  Check your installation." 1>&2
142
    exit 1
143 144
fi

145 146 147 148

# 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
149 150
    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
151 152
    exit 1
fi
Bruce Momjian's avatar
Bruce Momjian committed
153

154 155 156

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

if [ `$PGPATH/pg_id -u` -eq 0 ]
then
163 164
    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
165 166 167
    exit 1
fi

168

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

175 176 177 178 179

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

Bruce Momjian's avatar
Bruce Momjian committed
180 181 182 183
# 0 is the default (non-)encoding
MULTIBYTEID=0

# Set defaults:
184 185 186
debug=
noclean=
show_setting=
Bruce Momjian's avatar
Bruce Momjian committed
187 188 189 190 191

# 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
192
#       a security measure.
193
POSTGRES_SUPERUSERNAME="$EffectiveUser"
194
POSTGRES_SUPERUSERID=`$PGPATH/pg_id -u`
Bruce Momjian's avatar
Bruce Momjian committed
195

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

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

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

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


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

330 331
if [ -z "$PGDATA" ]
then
332
  (
Bruce Momjian's avatar
Bruce Momjian committed
333
    echo "$CMDNAME: You must identify where the the data for this database"
334
    echo "system will reside.  Do this with either a -D invocation"
Bruce Momjian's avatar
Bruce Momjian committed
335
    echo "option or a PGDATA environment variable."
336
  ) 1>&2
Bruce Momjian's avatar
Bruce Momjian committed
337 338 339 340 341 342 343 344
    exit 1
fi


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

345 346
POSTGRES_BKI="$datadir"/postgres.bki
POSTGRES_DESCR="$datadir"/postgres.description
Bruce Momjian's avatar
Bruce Momjian committed
347

348
PG_HBA_SAMPLE="$datadir"/pg_hba.conf.sample
349
PG_IDENT_SAMPLE="$datadir"/pg_ident.conf.sample
350
POSTGRESQL_CONF_SAMPLE="$datadir"/postgresql.conf.sample
Bruce Momjian's avatar
Bruce Momjian committed
351

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

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

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

381 382
for file in "$POSTGRES_BKI"
do
383
     if [ x"`sed 1q $file`" != x"# PostgreSQL $short_version" ]; then
384
       (
385 386 387
         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."
388
       ) 1>&2
389 390 391 392
         exit 1
     fi
done

Bruce Momjian's avatar
Bruce Momjian committed
393

Bruce Momjian's avatar
Bruce Momjian committed
394
trap 'echo "Caught signal." ; exit_nicely' 1 2 3 15
Bruce Momjian's avatar
Bruce Momjian committed
395

396 397 398 399
# 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
Bruce Momjian's avatar
Bruce Momjian committed
400

401 402 403 404

##########################################################################
#
# CREATE DATABASE DIRECTORY
Marc G. Fournier's avatar
Marc G. Fournier committed
405 406 407 408

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

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

430 431
    if [ ! -d "$PGDATA"/base ]
	then
432
        echo "Creating directory $PGDATA/base"
433
        mkdir "$PGDATA"/base || exit_nicely
Marc G. Fournier's avatar
Marc G. Fournier committed
434
    fi
435 436 437 438 439
    if [ ! -d "$PGDATA"/global ]
    then
        echo "Creating directory $PGDATA/global"
        mkdir "$PGDATA"/global || exit_nicely
    fi
440
    if [ ! -d "$PGDATA"/pg_xlog ]
Bruce Momjian's avatar
Bruce Momjian committed
441
    then
442
        echo "Creating directory $PGDATA/pg_xlog"
443
        mkdir "$PGDATA"/pg_xlog || exit_nicely
444
    fi
Marc G. Fournier's avatar
Marc G. Fournier committed
445 446
fi

447 448 449

##########################################################################
#
450
# RUN BKI SCRIPT IN BOOTSTRAP MODE TO CREATE TEMPLATE1
Marc G. Fournier's avatar
Marc G. Fournier committed
451

452 453
rm -rf "$PGDATA"/base/1 || exit_nicely
mkdir "$PGDATA"/base/1 || exit_nicely
Marc G. Fournier's avatar
Marc G. Fournier committed
454

455
if [ "$debug" = yes ]
456
then
Marc G. Fournier's avatar
Marc G. Fournier committed
457 458 459
    BACKEND_TALK_ARG="-d"
fi

460
FIRSTRUN="-boot -x1 -F -D$PGDATA $BACKEND_TALK_ARG"
Marc G. Fournier's avatar
Marc G. Fournier committed
461

462
echo "Creating template1 database in $PGDATA/base/1"
463
[ "$debug" = yes ] && echo "Running: $PGPATH/postgres $FIRSTRUN template1"
Marc G. Fournier's avatar
Marc G. Fournier committed
464

465 466 467 468
cat "$POSTGRES_BKI" \
| sed -e "s/POSTGRES/$POSTGRES_SUPERUSERNAME/g" \
      -e "s/PGUID/$POSTGRES_SUPERUSERID/g" \
      -e "s/ENCODING/$MULTIBYTEID/g" \
469
| "$PGPATH"/postgres $FIRSTRUN template1 \
470
|| exit_nicely
Marc G. Fournier's avatar
Marc G. Fournier committed
471

472
echo $short_version > "$PGDATA"/base/1/PG_VERSION || exit_nicely
Marc G. Fournier's avatar
Marc G. Fournier committed
473

474 475
##########################################################################
#
476
# CREATE CONFIG FILES
477 478 479 480 481 482 483 484

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
Marc G. Fournier's avatar
Marc G. Fournier committed
485

486 487 488 489
##########################################################################
#
# CREATE VIEWS and other things

490
echo "Initializing pg_shadow."
Marc G. Fournier's avatar
Marc G. Fournier committed
491

492
PGSQL_OPT="-o /dev/null -O -F -D$PGDATA"
Marc G. Fournier's avatar
Marc G. Fournier committed
493

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

Peter Eisentraut's avatar
Peter Eisentraut committed
500 501
# needs to be done before alter user
echo "REVOKE ALL on pg_shadow FROM public" \
502
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
503

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

529

530
echo "Enabling unlimited row width for system tables."
531

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


546 547
echo "Creating system views."

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

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

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

580 581
# XXX why does pg_tables include sequences?

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

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

echo "Loading pg_description."
605
echo "COPY pg_description FROM STDIN" > $TEMPFILE
606
cat "$POSTGRES_DESCR" >> $TEMPFILE
607 608

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

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

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

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

634 635 636 637 638

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

639
echo
640
echo "Success. You can now start the database server using:"
641
echo ""
642
echo "    $PGPATH/postmaster -D $PGDATA"
643
echo "or"
644 645 646
# (Advertise -l option here, otherwise we have a background
#  process writing to the terminal.)
echo "    $PGPATH/pg_ctl -D $PGDATA -l logfile start"
647 648 649
echo

exit 0