initdb.sh 17.9 KB
Newer Older
M
 
Marc G. Fournier 已提交
1
#!/bin/sh
B
Bruce Momjian 已提交
2
set -x
M
 
Marc G. Fournier 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#-------------------------------------------------------------------------
#
# initdb.sh--
#     Create (initialize) a Postgres database system.  
# 
#     A database system is a collection of Postgres databases all managed
#     by the same postmaster.  
#
#     To create the database system, we create the directory that contains
#     all its data, create the files that hold the global classes, create
#     a few other control files for it, and create one database:  the
#     template database.
#
#     The template database is an ordinary Postgres database.  Its data
#     never changes, though.  It exists to make it easy for Postgres to 
#     create other databases -- it just copies.
#
#     Optionally, we can skip creating the database system and just create
#     (or replace) the template database.
#
#     To create all those classes, we run the postgres (backend) program and
#     feed it data from bki files that are in the Postgres library directory.
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
B
Bruce Momjian 已提交
30
#    $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.73 1999/12/18 03:21:21 momjian Exp $
B
Bruce Momjian 已提交
31 32 33
#
#-------------------------------------------------------------------------

34
exit_nicely(){
B
Bruce Momjian 已提交
35 36
    echo
    echo "$CMDNAME failed."
37 38
    if [ "$noclean" -eq 0 ]
	then
B
Bruce Momjian 已提交
39
        echo "Removing $PGDATA."
40
        rm -rf "$PGDATA" || echo "Failed."
B
Bruce Momjian 已提交
41 42 43 44 45 46 47 48
    else
        echo "Data directory $PGDATA will not be removed at user's request."
    fi
    exit 1
}


CMDNAME=`basename $0`
49 50
if [ "$USER" = 'root' -o "$LOGNAME" = 'root' ]
then
B
Bruce Momjian 已提交
51 52 53 54 55 56 57 58 59 60 61
    echo "You cannot run $CMDNAME as root. Please log in (using, e.g., 'su')"
    echo "as the (unprivileged) user that will own the server process."
    exit 1
fi

EffectiveUser=`id -n -u 2> /dev/null` || EffectiveUser=`whoami 2> /dev/null`
TEMPFILE="/tmp/initdb.$$"

#
# Find out where we're located
#
62 63
if echo "$0" | grep '/' > /dev/null 2>&1 
then
B
Bruce Momjian 已提交
64 65 66 67
        # explicit dir name given
        PGPATH=`echo $0 | sed 's,/[^/]*$,,'`       # (dirname command is not portable)
else
        # look for it in PATH ('which' command is not portable)
B
Bruce Momjian 已提交
68 69
        for dir in `echo "$PATH" | sed 's/:/ /g'`
	do
B
Bruce Momjian 已提交
70 71
                # empty entry in path means current dir
                [ -z "$dir" ] && dir='.'
72
                if [ -f "$dir/$CMDNAME" ]
B
Bruce Momjian 已提交
73
		then
B
Bruce Momjian 已提交
74 75 76 77 78 79 80
                        PGPATH="$dir"
                        break
                fi
        done
fi

# Check if needed programs actually exist in path
B
Bruce Momjian 已提交
81 82
for prog in postgres pg_version
do
83
        if [ ! -x "$PGPATH/$prog" ]
B
Bruce Momjian 已提交
84
	then
B
Bruce Momjian 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
                echo "The program $prog needed by $CMDNAME could not be found. It was"
                echo "expected at:"
                echo "    $PGPATH/$prog"
                echo "If this is not the correct directory, please start $CMDNAME"
                echo "with a full search path. Otherwise make sure that the program"
                echo "was installed successfully."
                exit 1
        fi
done

# 0 is the default (non-)encoding
MULTIBYTEID=0
# This is placed here by configure --with-mb=XXX.
MULTIBYTE=__MULTIBYTE__

# Set defaults:
debug=0
noclean=0
template_only=0


# 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
#       a security measure. It might change in the future (why?), but for
#       now the --username option is only a fallback if both id and whoami
#       fail, and in that case the argument _must_ be the name of the effective
#       user.
114
POSTGRES_SUPERUSERNAME="$EffectiveUser"
B
Bruce Momjian 已提交
115 116 117

Password='_null_'

118
while [ "$#" -gt 0 ]
B
Bruce Momjian 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
do
    case "$1" in
        --help|-\?)
                usage=t
                break
                ;;
        --debug|-d)
                debug=1
                echo "Running with debug mode on."
                ;;
        --noclean|-n)
                noclean=1
                echo "Running with noclean mode on. Mistakes will not be cleaned up."
                ;;
        --template|-t)
                template_only=1
                echo "Updating template1 database only."
                ;;
# The database superuser. See comments above.
        --username|-u)
                POSTGRES_SUPERUSERNAME="$2"
                shift;;
        --username=*)
                POSTGRES_SUPERUSERNAME=`echo $1 | sed 's/^--username=//'`
                ;;
        -u*)
                POSTGRES_SUPERUSERNAME=`echo $1 | sed 's/^-u//'`
                ;;
# The sysid of the database superuser. See comments above.
        --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.
        --password|-W)
                Password="$2"
                shift;;
        --password=*)
                Password=`echo $1 | sed 's/^--password=//'`
                ;;
        -W*)
                Password=`echo $1 | sed 's/^-W//'`
                ;;
# 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//'`
                ;;
# The directory where the database templates are stored (traditionally in
# $prefix/lib). This is now autodetected for the most common layouts.
        --pglib|-L)
                PGLIB="$2"
                shift;;
        --pglib=*)
                PGLIB=`echo $1 | sed 's/^--pglib=//'`
                ;;
        -L*)
                PGLIB=`echo $1 | sed 's/^-L//'`
                ;;
# The encoding of the template1 database. Defaults to what you chose
# at configure time. (see above)
        --pgencoding|-e)
                MULTIBYTE="$2"
                shift;;
        --pgencoding=*)
                MULTIBYTE=`echo $1 | sed 's/^--pgencoding=//'`
                ;;
        -e*)
                MULTIBYTE=`echo $1 | sed 's/^-e//'`
                ;;
        *)
                echo "Unrecognized option '$1'. Try -? for help."
                exit 1
                ;;
    esac
    shift
done

208 209
if [ "$usage" ]
then
210 211 212 213 214 215 216 217 218 219 220 221
 	echo ""
 	echo "Usage: $CMDNAME [options]"
 	echo ""
        echo "    -t,           --template           "
        echo "    -d,           --debug              "
        echo "    -n,           --noclean            "
        echo "    -i SYSID,     --sysid=SYSID        "
        echo "    -W PASSWORD,  --password=PASSWORD  "
        echo "    -u SUPERUSER, --username=SUPERUSER " 
        echo "    -D DATADIR,   --pgdata=DATADIR     "
        echo "    -L LIBDIR,    --pglib=LIBDIR       "
 	
222 223
 	if [ -n "$MULTIBYTE" ]
	then 
224
 		echo "    -e ENCODING,  --pgencoding=ENCODING"
B
Bruce Momjian 已提交
225
	fi
226 227 228
 	echo "    -?,           --help               "           	
 	echo ""	 
 	exit 0
B
Bruce Momjian 已提交
229 230 231 232 233 234
fi

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

235 236
if [ "$MULTIBYTE" ]
then
B
Bruce Momjian 已提交
237
	MULTIBYTEID=`$PGPATH/pg_encoding $MULTIBYTE`
238
        if [ "$?" -ne 0 ]
B
Bruce Momjian 已提交
239
	then
B
Bruce Momjian 已提交
240 241 242 243 244
                echo "The program pg_encoding failed. Perhaps you did not configure"
                echo "PostgreSQL for multibyte support or the program was not success-"
                echo "fully installed."
                exit 1
        fi
245 246
	if [ -z "$MULTIBYTEID" ]
	then
B
Bruce Momjian 已提交
247 248 249 250 251 252 253 254 255 256
		echo "$CMDNAME: $MULTIBYTE is not a valid encoding name."
		exit 1
	fi
fi


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

257 258
if [ -z "$PGDATA" ]
then
B
Bruce Momjian 已提交
259 260 261 262 263 264 265 266 267 268
    echo "$CMDNAME: You must identify where the the data for this database"
    echo "system will reside.  Do this with either a --pgdata invocation"
    echo "option or a PGDATA environment variable."
    echo
    exit 1
fi

# The data path must be absolute, because the backend doesn't like
# '.' and '..' stuff. (Should perhaps be fixed there.)

269 270
if ! echo "$PGDATA" | grep '^/' > /dev/null 2>&1
then
B
Bruce Momjian 已提交
271 272 273 274 275 276 277 278 279
    echo "$CMDNAME: The data path must be specified as an absolute path."
    exit 1
fi

#---------------------------------------------------------------------------
# Figure out who the Postgres superuser for the new database system will be.
#---------------------------------------------------------------------------

# This means they have neither 'id' nor 'whoami'!
280 281
if [ -z "$POSTGRES_SUPERUSERNAME" ]
then 
B
Bruce Momjian 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295
    echo "$CMDNAME: Could not determine what the name of the database"
    echo "superuser should be. Please use the --username option."
    exit 1
fi

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


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

296 297
if [ -z "$PGLIB" ]
then
B
Bruce Momjian 已提交
298 299
        for dir in "$PGPATH/../lib" "$PGPATH/../lib/pgsql"
	do
300
                if [ -f "$dir/global1.bki.source" ]
B
Bruce Momjian 已提交
301
		then
302
                        PGLIB="$dir"
B
Bruce Momjian 已提交
303 304 305 306 307
                        break
                fi
        done
fi

308 309
if [ -z "$PGLIB" ]
then
B
Bruce Momjian 已提交
310 311 312 313 314 315 316
        echo "$CMDNAME: Could not find the \"lib\" directory, that contains"
        echo "the files needed by initdb. Please specify it with the"
        echo "--pglib option."
        exit 1
fi


317 318 319
TEMPLATE="$PGLIB"/local1_template1.bki.source
GLOBAL="$PGLIB"/global1.bki.source
PG_HBA_SAMPLE="$PGLIB"/pg_hba.conf.sample
B
Bruce Momjian 已提交
320

321 322 323
TEMPLATE_DESCR="$PGLIB"/local1_template1.description
GLOBAL_DESCR="$PGLIB"/global1.description
PG_GEQO_SAMPLE="$PGLIB"/pg_geqo.sample
B
Bruce Momjian 已提交
324

B
Bruce Momjian 已提交
325 326
for PREREQ_FILE in "$TEMPLATE" "$GLOBAL" "$PG_HBA_SAMPLE"
do
327 328
    if [ ! -f "$PREREQ_FILE" ]
	then 
B
Bruce Momjian 已提交
329 330 331 332 333 334 335 336 337
        echo "$CMDNAME does not find the file '$PREREQ_FILE'."
        echo "This means you have a corrupted installation or identified the"
        echo "wrong directory with the --pglib invocation option."
        exit 1
    fi
done

[ "$debug" -ne 0 ] && echo "$CMDNAME: Using $TEMPLATE as input to create the template database."

338 339
if [ "$template_only" -eq 0 ]
then
B
Bruce Momjian 已提交
340 341
    [ "$debug" -ne 0 ] && echo "$CMDNAME: Using $GLOBAL as input to create the global classes."
    [ "$debug" -ne 0 ] && echo "$CMDNAME: Using $PG_HBA_SAMPLE as default authentication control file."
B
Bruce Momjian 已提交
342
fi
B
Bruce Momjian 已提交
343

B
Bruce Momjian 已提交
344
trap 'echo "Caught signal." ; exit_nicely' 1 2 3 15
B
Bruce Momjian 已提交
345 346


M
 
Marc G. Fournier 已提交
347 348 349 350 351 352 353
# -----------------------------------------------------------------------
# Create the data directory if necessary
# -----------------------------------------------------------------------

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

354 355 356
if [ -f "$PGDATA"/PG_VERSION ]
then
    if [ "$template_only" -eq 0 ]
B
Bruce Momjian 已提交
357
    then
358 359
        echo "$CMDNAME: The file $PGDATA/PG_VERSION already exists."
        echo "This probably means initdb has already been run and the"
M
 
Marc G. Fournier 已提交
360 361
        echo "database system already exists."
        echo 
362 363
        echo "If you want to create a new database system, either remove"
        echo "the directory $PGDATA or run initdb with a --pgdata argument"
M
 
Marc G. Fournier 已提交
364 365 366 367
        echo "other than $PGDATA."
        exit 1
    fi
else
368 369
    if [ ! -d "$PGDATA" ]
	then
370
        echo "Creating database system directory $PGDATA"
371
        mkdir "$PGDATA" || exit_nicely
372 373
    else
        echo "Fixing permissions on pre-existing data directory $PGDATA"
374
	chmod go-rwx "$PGDATA" || exit_nicely
M
 
Marc G. Fournier 已提交
375
    fi
376

377 378
    if [ ! -d "$PGDATA"/base ]
	then
379
        echo "Creating database system directory $PGDATA/base"
380
        mkdir "$PGDATA"/base || exit_nicely
M
 
Marc G. Fournier 已提交
381
    fi
382
    if [ ! -d "$PGDATA"/pg_xlog ]
B
Bruce Momjian 已提交
383
    then
384
        echo "Creating database XLOG directory $PGDATA/pg_xlog"
385
        mkdir "$PGDATA"/pg_xlog || exit_nicely
386
    fi
M
 
Marc G. Fournier 已提交
387 388 389 390 391 392
fi

#----------------------------------------------------------------------------
# Create the template1 database
#----------------------------------------------------------------------------

393 394
rm -rf "$PGDATA"/base/template1 || exit_nicely
mkdir "$PGDATA"/base/template1 || exit_nicely
M
 
Marc G. Fournier 已提交
395

396 397
if [ "$debug" -eq 1 ]
then
M
 
Marc G. Fournier 已提交
398 399 400 401 402 403
    BACKEND_TALK_ARG="-d"
else
    BACKEND_TALK_ARG="-Q"
fi

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

B
Bruce Momjian 已提交
406
echo "Creating template database in $PGDATA/base/template1"
407
[ "$debug" -ne 0 ] && echo "Running: $PGPATH/postgres $FIRSTRUN template1"
M
 
Marc G. Fournier 已提交
408

409
cat "$TEMPLATE" \
410
| sed -e "s/PGUID/$POSTGRES_SUPERUSERID/g" \
411
| "$PGPATH"/postgres $FIRSTRUN template1 \
412
|| exit_nicely
M
 
Marc G. Fournier 已提交
413

414
"$PGPATH"/pg_version "$PGDATA"/base/template1 || exit_nicely
M
 
Marc G. Fournier 已提交
415 416 417 418 419

#----------------------------------------------------------------------------
# Create the global classes, if requested.
#----------------------------------------------------------------------------

420 421
if [ "$template_only" -eq 0 ]
then
422 423
    echo "Creating global relations in $PGDATA/base"
    [ "$debug" -ne 0 ] && echo "Running: $PGPATH/postgres $BACKENDARGS template1"
M
 
Marc G. Fournier 已提交
424

425
    cat "$GLOBAL" \
426 427 428
    | sed -e "s/POSTGRES/$POSTGRES_SUPERUSERNAME/g" \
          -e "s/PGUID/$POSTGRES_SUPERUSERID/g" \
          -e "s/PASSWORD/$Password/g" \
429
    | "$PGPATH"/postgres $BACKENDARGS template1 \
430
    || exit_nicely
M
 
Marc G. Fournier 已提交
431

432
    "$PGPATH"/pg_version "$PGDATA" || exit_nicely
M
 
Marc G. Fournier 已提交
433

434 435
    cp "$PG_HBA_SAMPLE" "$PGDATA"/pg_hba.conf     || exit_nicely
    cp "$PG_GEQO_SAMPLE" "$PGDATA"/pg_geqo.sample || exit_nicely
M
 
Marc G. Fournier 已提交
436

437
    echo "Adding template1 database to pg_database"
M
 
Marc G. Fournier 已提交
438

439
    echo "open pg_database" > "$TEMPFILE"
440
    echo "insert (template1 $POSTGRES_SUPERUSERID $MULTIBYTEID template1)" >> $TEMPFILE
441 442
    #echo "show" >> "$TEMPFILE"
    echo "close pg_database" >> "$TEMPFILE"
M
 
Marc G. Fournier 已提交
443

444
    [ "$debug" -ne 0 ] && echo "Running: $PGPATH/postgres $BACKENDARGS template1 < $TEMPFILE"
M
 
Marc G. Fournier 已提交
445

446
    "$PGPATH"/postgres $BACKENDARGS template1 < "$TEMPFILE"
447
    # Gotta remove that temp file before exiting on error.
448 449
    retval="$?"
    if [ "$noclean" -eq 0 ]
B
Bruce Momjian 已提交
450
    then
451
            rm -f "$TEMPFILE" || exit_nicely
M
 
Marc G. Fournier 已提交
452
    fi
453
    [ "$retval" -ne 0 ] && exit_nicely
M
 
Marc G. Fournier 已提交
454 455 456 457
fi

echo

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

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

466
# Create the initial pg_pwd (flat-file copy of pg_shadow)
467 468
echo "Writing password file."
echo "COPY pg_shadow TO '$PGDATA/pg_pwd' USING DELIMITERS '\\t'" \
469
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
470

471
# An ordinary COPY will leave the file too loosely protected.
472 473 474 475
# Note: If you lied above and specified a --username different from the one
# you really are, this will manifest itself in this command failing because
# of a missing file, since the COPY command above failed. It would perhaps
# be better if postgres returned an error code.
476
chmod go-rw "$PGDATA"/pg_pwd || exit_nicely
477 478

echo "Creating view pg_user."
479 480 481 482 483 484 485 486 487 488
echo "CREATE VIEW pg_user AS \
        SELECT \
            usename, \
            usesysid, \
            usecreatedb, \
            usetrace, \
            usesuper, \
            usecatupd, \
            '********'::text as passwd, \
            valuntil \
489
        FROM pg_shadow" \
490
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
491 492

echo "REVOKE ALL on pg_shadow FROM public" \
493
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
494 495

echo "Creating view pg_rules."
496 497 498 499 500 501 502
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' \
503
            AND C.oid = R.ev_class;" \
504
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
505 506

echo "Creating view pg_views."
507 508 509 510 511 512 513 514 515 516
echo "CREATE VIEW pg_views AS \
        SELECT \
            C.relname AS viewname, \
            pg_get_userbyid(C.relowner) AS viewowner, \
            pg_get_viewdef(C.relname) AS definition \
        FROM pg_class C \
        WHERE C.relhasrules \
            AND	EXISTS ( \
                SELECT rulename FROM pg_rewrite R \
                    WHERE ev_class = C.oid AND ev_type = '1' \
517
            )" \
518
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
519 520

echo "Creating view pg_tables."
521 522 523 524 525 526 527 528 529 530 531 532
echo "CREATE VIEW pg_tables AS \
        SELECT \
            C.relname AS tablename, \
	    pg_get_userbyid(C.relowner) AS tableowner, \
	    C.relhasindex AS hasindexes, \
	    C.relhasrules AS hasrules, \
	    (C.reltriggers > 0) AS hastriggers \
        FROM pg_class C \
        WHERE C.relkind IN ('r', 's') \
            AND NOT EXISTS ( \
                SELECT rulename FROM pg_rewrite \
                    WHERE ev_class = C.oid AND ev_type = '1' \
533
            )" \
534
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
535 536

echo "Creating view pg_indexes."
537 538 539 540 541 542 543
echo "CREATE VIEW pg_indexes AS \
        SELECT \
            C.relname AS tablename, \
	    I.relname AS indexname, \
            pg_get_indexdef(X.indexrelid) AS indexdef \
        FROM pg_index X, pg_class C, pg_class I \
	WHERE C.oid = X.indrelid \
544
            AND I.oid = X.indexrelid" \
545
        | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
546 547 548

echo "Loading pg_description."
echo "COPY pg_description FROM '$TEMPLATE_DESCR'" \
549
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
550
echo "COPY pg_description FROM '$GLOBAL_DESCR'" \
551
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
552 553
echo "Vacuuming database."
echo "VACUUM ANALYZE" \
554
	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
555 556 557 558 559 560 561

echo
echo "$CMDNAME completed successfully. You can now start the database server."
echo "($PGPATH/postmaster -D $PGDATA)"
echo

exit 0