initdb.sh 16.1 KB
Newer Older
M
 
Marc G. Fournier 已提交
1 2 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
#!/bin/sh
#-------------------------------------------------------------------------
#
# 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
29
#    $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.66 1999/12/17 01:05:30 momjian Exp $
M
 
Marc G. Fournier 已提交
30 31 32 33
#
#-------------------------------------------------------------------------

# ----------------
34
#       The OPT_..._PARAM gets set when the script is built (with make)
M
 
Marc G. Fournier 已提交
35 36 37 38 39
#       from parameters set in the make file.
#
# ----------------

CMDNAME=`basename $0`
40
MULTIBYTEID=0
M
 
Marc G. Fournier 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 114 115 116 117 118 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

MULTIBYTE=__MULTIBYTE__
if [  -n "$MULTIBYTE" ];then
	MULTIBYTEID=`pg_encoding $MULTIBYTE`
fi

# Find the default PGLIB directory (the directory that contains miscellaneous 
# files that are part of Postgres).  The user-written program postconfig
# outputs variable settings like "PGLIB=/usr/lib/whatever".  If it doesn't
# output a PGLIB value, then there is no default and the user must
# specify the pglib option.  Postconfig may validly not exist, in which case
# our invocation of it silently fails.

# The 2>/dev/null is to swallow the "postconfig: not found" message if there
# is no postconfig.

postconfig_result="`sh -c postconfig 2>/dev/null`"
if [ ! -z "$postconfig_result" ]; then
  set -a   # Make the following variable assignment exported to environment
  eval "$postconfig_result"
  set +a   # back to normal
fi

# Set defaults:
debug=0
noclean=0
template_only=0
POSTGRES_SUPERUSERNAME=$USER

while [ "$#" -gt 0 ]
do
# ${ARG#--username=} is not reliable or available on all platforms

    case "$1" in
        --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."
                ;;
        --username=*)
                POSTGRES_SUPERUSERNAME="`echo $1 | sed 's/^--username=//'`"
                ;;
        -u)
                shift
                POSTGRES_SUPERUSERNAME="$1"
                ;;
        -u*)
                POSTGRES_SUPERUSERNAME="`echo $1 | sed 's/^-u//'`"
                ;;
        --pgdata=*)
                PGDATA="`echo $1 | sed 's/^--pgdata=//'`"
                ;;
        -r)
                shift
                PGDATA="$1"
                ;;
        -r*)
                PGDATA="`echo $1 | sed 's/^-r//'`"
                ;;
        --pglib=*)
                PGLIB="`echo $1 | sed 's/^--pglib=//'`"
                ;;
        -l)
                shift
                PGLIB="$1"
                ;;
        -l*)
                PGLIB="`echo $1 | sed 's/^-l//'`"
                ;;

        --pgencoding=*)
		if [ -z "$MULTIBYTE" ];then
			echo "MULTIBYTE support seems to be disabled"
			exit 100
		fi
                mb="`echo $1 | sed 's/^--pgencoding=//'`"
		MULTIBYTEID=`pg_encoding $mb`
		if [ -z "$MULTIBYTEID" ];then
			echo "$mb is not a valid encoding name"
			exit 100
		fi
                ;;
        -e)
		if [ -z "$MULTIBYTE" ];then
			echo "MULTIBYTE support seems to be disabled"
			exit 100
		fi
                shift
		MULTIBYTEID=`pg_encoding $1`
		if [ -z "$MULTIBYTEID" ];then
			echo "$1 is not a valid encoding name"
			exit 100
		fi
                ;;
        -e*)
		if [ -z "$MULTIBYTE" ];then
			echo "MULTIBYTE support seems to be disabled"
			exit 100
		fi
                mb="`echo $1 | sed 's/^-e//'`"
		MULTIBYTEID=`pg_encoding $mb`
		if [ -z "$MULTIBYTEID" ];then
			echo "$mb is not a valid encoding name"
			exit 100
		fi
                ;;
B
Hi,  
Bruce Momjian 已提交
155 156 157 158 159 160
	--help)
		usage=t
		;;
	-\?)
		usage=t
		;;
M
 
Marc G. Fournier 已提交
161
        *)
B
Hi,  
Bruce Momjian 已提交
162 163
                echo "Unrecognized option '$1'. Try -? for help."
		exit 100
M
 
Marc G. Fournier 已提交
164 165 166 167
        esac
        shift
done

B
Hi,  
Bruce Momjian 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
if [ "$usage" ]; then
	echo ""
	echo "Usage: $CMDNAME [options]"
	echo ""
        echo "    -t,           --template           "
        echo "    -d,           --debug              "
        echo "    -n,           --noclean            "
        echo "    -u SUPERUSER, --username=SUPERUSER " 
        echo "    -r DATADIR,   --pgdata=DATADIR     "
        echo "    -l LIBDIR,    --pglib=LIBDIR       "
	
	if [ -n "$MULTIBYTE" ]; then 
		echo "    -e ENCODING,  --pgencoding=ENCODING"
        fi
	
	echo "    -?,           --help               "           	
	echo ""	

	exit 100
fi

M
 
Marc G. Fournier 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
#-------------------------------------------------------------------------
# Make sure he told us where to find the Postgres files.
#-------------------------------------------------------------------------
if [ -z "$PGLIB" ]; then
    echo "$CMDNAME does not know where to find the files that make up "
    echo "Postgres (the PGLIB directory).  You must identify the PGLIB "
    echo "directory either with a --pglib invocation option, or by "
    echo "setting the PGLIB environment variable, or by having a program "
    echo "called 'postconfig' in your search path that outputs an asignment "
    echo "for PGLIB."
    exit 20
fi

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

if [ -z "$PGDATA" ]; then
    echo "$CMDNAME: You must identify the PGDATA directory, where the data"
    echo "for this database system will reside.  Do this with either a"
    echo "--pgdata invocation option or a PGDATA environment variable."
    echo
    exit 20
fi

TEMPLATE=$PGLIB/local1_template1.bki.source
GLOBAL=$PGLIB/global1.bki.source
TEMPLATE_DESCR=$PGLIB/local1_template1.description
GLOBAL_DESCR=$PGLIB/global1.description
PG_HBA_SAMPLE=$PGLIB/pg_hba.conf.sample
PG_GEQO_SAMPLE=$PGLIB/pg_geqo.sample


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

for PREREQ_FILE in $TEMPLATE $GLOBAL $PG_HBA_SAMPLE; do
    if [ ! -f $PREREQ_FILE ]; then 
        echo "$CMDNAME does not find the file '$PREREQ_FILE'."
        echo "This means you have identified an invalid PGLIB directory."
        echo "You specify a PGLIB directory with a --pglib invocation "
        echo "option, a PGLIB environment variable, or a postconfig program."
        exit 1
    fi
done

B
Bruce Momjian 已提交
236
[ "$debug" -ne 0 ] && echo "$CMDNAME: using $TEMPLATE as input to create the template database."
M
 
Marc G. Fournier 已提交
237
if [ $template_only -eq 0 ]; then
B
Bruce Momjian 已提交
238 239
    [ "$debug" -ne 0 ] && echo "$CMDNAME: using $GLOBAL as input to create the global classes."
    [ "$debug" -ne 0 ] && echo "$CMDNAME: using $PG_HBA_SAMPLE as the host-based authentication" \
M
 
Marc G. Fournier 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
         "control file."
    echo
fi  

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

if [ -z "$POSTGRES_SUPERUSERNAME" ]; then 
    echo "Can't tell what username to use.  You don't have the USER"
    echo "environment variable set to your username and didn't specify the "
    echo "--username option"
    exit 1
fi

POSTGRES_SUPERUID=`pg_id $POSTGRES_SUPERUSERNAME`

257 258 259 260 261 262 263 264
if [ ${POSTGRES_SUPERUID:=-1} -eq -1 ]; then
    echo "Unable to determine a valid username.  If you are running"
    echo "initdb without an explicit username specified, then there"
    echo "may be a problem with finding the Postgres shared library"
    echo "and/or the pg_id utility."
    exit 10
fi

M
 
Marc G. Fournier 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
if [ $POSTGRES_SUPERUID = NOUSER ]; then
    echo "Valid username not given.  You must specify the username for "
    echo "the Postgres superuser for the database system you are "
    echo "initializing, either with the --username option or by default "
    echo "to the USER environment variable."
    exit 10
fi

if [ $POSTGRES_SUPERUID -ne `pg_id` -a `pg_id` -ne 0 ]; then 
    echo "Only the unix superuser may initialize a database with a different"
    echo "Postgres superuser.  (You must be able to create files that belong"
    echo "to the specified unix user)."
    exit 2
fi

echo "We are initializing the database system with username" \
  "$POSTGRES_SUPERUSERNAME (uid=$POSTGRES_SUPERUID)."   
echo "This user will own all the files and must also own the server process."
echo

# -----------------------------------------------------------------------
# Create the data directory if necessary
# -----------------------------------------------------------------------

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

if [ -f "$PGDATA/PG_VERSION" ]; then
    if [ $template_only -eq 0 ]; then
294 295
        echo "$CMDNAME: The file $PGDATA/PG_VERSION already exists."
        echo "This probably means initdb has already been run and the"
M
 
Marc G. Fournier 已提交
296 297
        echo "database system already exists."
        echo 
298 299
        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 已提交
300 301 302 303 304
        echo "other than $PGDATA."
        exit 1
    fi
else
    if [ ! -d $PGDATA ]; then
305 306 307 308 309
        echo "Creating database system directory $PGDATA"
        mkdir $PGDATA || exit_nicely
    else
        echo "Fixing permissions on pre-existing data directory $PGDATA"
	chmod go-rwx $PGDATA || exit_nicely
M
 
Marc G. Fournier 已提交
310
    fi
311

M
 
Marc G. Fournier 已提交
312
    if [ ! -d $PGDATA/base ]; then
313 314
        echo "Creating database system directory $PGDATA/base"
        mkdir $PGDATA/base || exit_nicely
M
 
Marc G. Fournier 已提交
315
    fi
316
    if [ ! -d $PGDATA/pg_xlog ]; then
317 318
        echo "Creating database XLOG directory $PGDATA/pg_xlog"
        mkdir $PGDATA/pg_xlog || exit_nicely
319
    fi
M
 
Marc G. Fournier 已提交
320 321 322 323 324 325
fi

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

326 327
rm -rf $PGDATA/base/template1 || exit_nicely
mkdir $PGDATA/base/template1 || exit_nicely
M
 
Marc G. Fournier 已提交
328 329 330 331 332 333 334 335

if [ "$debug" -eq 1 ]; then
    BACKEND_TALK_ARG="-d"
else
    BACKEND_TALK_ARG="-Q"
fi

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

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

cat $TEMPLATE \
342 343 344
| sed -e "s/PGUID/$POSTGRES_SUPERUSERID/g" \
| $PGPATH/postgres $FIRSTRUN template1 \
|| exit_nicely
M
 
Marc G. Fournier 已提交
345

346
$PGPATH/pg_version $PGDATA/base/template1 || exit_nicely
M
 
Marc G. Fournier 已提交
347 348 349 350 351 352

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

if [ $template_only -eq 0 ]; then
353 354
    echo "Creating global relations in $PGDATA/base"
    [ "$debug" -ne 0 ] && echo "Running: $PGPATH/postgres $BACKENDARGS template1"
M
 
Marc G. Fournier 已提交
355 356

    cat $GLOBAL \
357 358 359 360 361
    | sed -e "s/POSTGRES/$POSTGRES_SUPERUSERNAME/g" \
          -e "s/PGUID/$POSTGRES_SUPERUSERID/g" \
          -e "s/PASSWORD/$Password/g" \
    | $PGPATH/postgres $BACKENDARGS template1 \
    || exit_nicely
M
 
Marc G. Fournier 已提交
362

363
    $PGPATH/pg_version $PGDATA || exit_nicely
M
 
Marc G. Fournier 已提交
364

365 366
    cp $PG_HBA_SAMPLE $PGDATA/pg_hba.conf     || exit_nicely
    cp $PG_GEQO_SAMPLE $PGDATA/pg_geqo.sample || exit_nicely
M
 
Marc G. Fournier 已提交
367

368
    echo "Adding template1 database to pg_database"
M
 
Marc G. Fournier 已提交
369

370 371 372 373
    echo "open pg_database" > $TEMPFILE
    echo "insert (template1 $POSTGRES_SUPERUSERID $MULTIBYTEID template1)" >> $TEMPFILE
    #echo "show" >> $TEMPFILE
    echo "close pg_database" >> $TEMPFILE
M
 
Marc G. Fournier 已提交
374

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

377 378 379 380 381
    $PGPATH/postgres $BACKENDARGS template1 < $TEMPFILE
    # Gotta remove that temp file before exiting on error.
    retval=$?
    if [ $noclean -eq 0 ]; then
            rm -f $TEMPFILE || exit_nicely
M
 
Marc G. Fournier 已提交
382
    fi
383
    [ $retval -ne 0 ] && exit_nicely
M
 
Marc G. Fournier 已提交
384 385 386 387
fi

echo

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

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

396
# Create the initial pg_pwd (flat-file copy of pg_shadow)
397 398 399 400
echo "Writing password file."
echo "COPY pg_shadow TO '$PGDATA/pg_pwd' USING DELIMITERS '\\t'" \
	| $PGPATH/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely

401
# An ordinary COPY will leave the file too loosely protected.
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
# 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.
chmod go-rw $PGDATA/pg_pwd || exit_nicely

echo "Creating view pg_user."
echo "CREATE VIEW pg_user AS
        SELECT
            usename,
            usesysid,
            usecreatedb,
            usetrace,
            usesuper,
            usecatupd,
            '********'::text as passwd,
            valuntil
        FROM pg_shadow" \
        | $PGPATH/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely

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

echo "Creating view pg_rules."
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'
            AND C.oid = R.ev_class;" \
	| $PGPATH/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely

echo "Creating view pg_views."
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'
            )" \
	| $PGPATH/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely

echo "Creating view pg_tables."
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'
            )" \
	| $PGPATH/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely

echo "Creating view pg_indexes."
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
            AND I.oid = X.indexrelid" \
        | $PGPATH/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely

echo "Loading pg_description."
echo "COPY pg_description FROM '$TEMPLATE_DESCR'" \
	| $PGPATH/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
echo "COPY pg_description FROM '$GLOBAL_DESCR'" \
	| $PGPATH/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
echo "Vacuuming database."
echo "VACUUM ANALYZE" \
	| $PGPATH/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely

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

exit 0