📄 initdb.sh
字号:
#!@SHELL@#-------------------------------------------------------------------------## initdb creates (initializes) a PostgreSQL database cluster (site,# instance, installation, whatever). A database cluster is a# collection of PostgreSQL databases all managed by the same postmaster.## To create the database cluster, we create the directory that contains# all its data, create the files that hold the global tables, create# a few other control files for it, and create two databases: the# template0 and template1 databases.## 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.## 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.### Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group# Portions Copyright (c) 1994, Regents of the University of California## $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.204.2.3 2004/05/05 16:09:56 tgl Exp $##-------------------------------------------------------------------------############################################################################ INITIALIZATIONexit_nicely(){ stty echo > /dev/null 2>&1 echo 1>&2 echo "$CMDNAME: failed" 1>&2 if [ "$noclean" != yes ]; then if [ "$made_new_pgdata" = yes ]; then echo "$CMDNAME: removing data directory \"$PGDATA\"" 1>&2 rm -rf "$PGDATA" || echo "$CMDNAME: failed" 1>&2 fi else echo "$CMDNAME: data directory \"$PGDATA\" not removed at user's request" 1>&2 fi exit 1}pg_getlocale(){ arg=$1 unset ret for var in "PGLC_$arg" PGLOCALE LC_ALL "LC_$arg" LANG; do varset=`eval echo '${'"$var"'+set}'` varval=`eval echo '$'"$var"` if test "$varset" = set; then ret=$varval break fi done if test "${ret+set}" != set; then ret=C fi echo "$ret"}CMDNAME=`basename $0`# Placed here during buildVERSION='@VERSION@'HAVE_IPV6='@HAVE_IPV6@'bindir='@bindir@'# Note that "datadir" is not the directory we're initializing, it's# merely how Autoconf names PREFIX/share.datadir='@datadir@'# Check for echo -n vs echo \cif echo '\c' | grep -s c >/dev/null 2>&1then ECHO_N="echo -n" ECHO_C=""else ECHO_N="echo" ECHO_C='\c'fi## Find out where we're located#if echo "$0" | grep '/' > /dev/null 2>&1 then # explicit dir name given self_path=`echo $0 | sed 's,/[^/]*$,,'` # (dirname command is not portable)else # look for it in PATH ('which' command is not portable) for dir in `echo "$PATH" | sed 's/:/ /g'` do # empty entry in path means current dir [ -z "$dir" ] && dir='.' if [ -f "$dir/$CMDNAME" ] then self_path="$dir" break fi donefi# 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" ] \ && [ x"`$self_path/postgres -V 2>/dev/null`" = x"postgres (PostgreSQL) $VERSION" ]then PGPATH=$self_pathelif [ -x "$bindir/postgres" ]; then if [ x"`$bindir/postgres -V 2>/dev/null`" = x"postgres (PostgreSQL) $VERSION" ] then PGPATH=$bindir else # 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 exit 1 fielse echo "The program \"postgres\" is needed by $CMDNAME but was not found in" 1>&2 echo "the directory \"$bindir\". Check your installation." 1>&2 exit 1fi# 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 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 exit 1fiEffectiveUser=`$PGPATH/pg_id -n -u`if [ -z "$EffectiveUser" ]; then echo "$CMDNAME: could not determine current user name" 1>&2 exit 1fiif [ `$PGPATH/pg_id -u` -eq 0 ]then echo "$CMDNAME: cannot be run as root" 1>&2 echo "Please log in (using, e.g., \"su\") as the (unprivileged) user that will" 1>&2 echo "own the server process." 1>&2 exit 1fishort_version=`echo $VERSION | sed -e 's!^\([0-9][0-9]*\.[0-9][0-9]*\).*!\1!'`if [ x"$short_version" = x"" ] ; then echo "$CMDNAME: bug: version number has wrong format" 1>&2 exit 1fi############################################################################ COMMAND LINE OPTIONS# 0 is the default (non-)encodingENCODINGID=0# Set defaults:debug=noclean=show_setting=# 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.POSTGRES_SUPERUSERNAME="$EffectiveUser"while [ "$#" -gt 0 ]do case "$1" in --help|-\?) usage=t break ;; --version|-V) echo "initdb (PostgreSQL) $VERSION" exit 0 ;; --debug|-d) debug=yes echo "Running in debug mode." ;; --show|-s) show_setting=yes ;; --noclean|-n) noclean=yes echo "Running in noclean mode. Mistakes will not be cleaned up." ;;# The name of the database superuser. Can be freely changed. --username|-U) POSTGRES_SUPERUSERNAME="$2" shift;; --username=*) POSTGRES_SUPERUSERNAME=`echo $1 | sed 's/^--username=//'` ;; -U*) POSTGRES_SUPERUSERNAME=`echo $1 | sed 's/^-U//'` ;;# The default password of the database superuser.# Make initdb prompt for the default password of the database superuser. --pwprompt|-W) PwPrompt=1 ;;# 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 .bki input files are stored. Normally# they are in PREFIX/share and this option should be unnecessary. -L) datadir="$2" shift;; -L*) datadir=`echo $1 | sed 's/^-L//'` ;;# The encoding of the template1 database. Defaults to what you chose# at configure time. (see above) --encoding|-E) ENCODING="$2" shift;; --encoding=*) ENCODING=`echo $1 | sed 's/^--encoding=//'` ;; -E*) ENCODING=`echo $1 | sed 's/^-E//'` ;;# Locale flags --locale) PGLOCALE="$2" shift;; --locale=*) PGLOCALE=`echo $1 | sed 's/^[^=]*=//'` ;; --no-locale) PGLOCALE=C ;; --lc-collate) PGLC_COLLATE=$2 shift;; --lc-collate=*) PGLC_COLLATE=`echo $1 | sed 's/^[^=]*=//'` ;; --lc-ctype) PGLC_CTYPE=$2 shift;; --lc-ctype=*) PGLC_CTYPE=`echo $1 | sed 's/^[^=]*=//'` ;; --lc-messages) PGLC_MESSAGES=$2 shift;; --lc-messages=*) PGLC_MESSAGES=`echo $1 | sed 's/^[^=]*=//'` ;; --lc-monetary) PGLC_MONETARY=$2 shift;; --lc-monetary=*) PGLC_MONETARY=`echo $1 | sed 's/^[^=]*=//'` ;; --lc-numeric) PGLC_NUMERIC=$2 shift;; --lc-numeric=*) PGLC_NUMERIC=`echo $1 | sed 's/^[^=]*=//'` ;; --lc-time) PGLC_TIME=$2 shift;; --lc-time=*) PGLC_TIME=`echo $1 | sed 's/^[^=]*=//'` ;; -*) echo "$CMDNAME: invalid option: $1" echo "Try \"$CMDNAME --help\" for more information." exit 1 ;;# Non-option argument specifies data directory *) PGDATA=$1 ;; esac shiftdoneif [ "$usage" ]; then echo "$CMDNAME initializes a PostgreSQL database cluster." echo echo "Usage:" echo " $CMDNAME [OPTION]... [DATADIR]" echo echo "Options:" echo " [-D, --pgdata=]DATADIR location for this database cluster" echo " -E, --encoding=ENCODING set default encoding for new databases" echo " --locale=LOCALE initialize database cluster with given locale" echo " --lc-collate, --lc-ctype, --lc-messages=LOCALE" echo " --lc-monetary, --lc-numeric, --lc-time=LOCALE" echo " initialize database cluster with given locale" echo " in the respective category (default taken from" echo " environment)" echo " --no-locale equivalent to --locale=C" echo " -U, --username=NAME database superuser name" echo " -W, --pwprompt prompt for a password for the new superuser" echo " --help show this help, then exit" echo " --version output version information, then exit" echo echo "Less commonly used options: " echo " -d, --debug generate lots of debugging output" echo " -L DIRECTORY where to find the input files" echo " -n, --noclean do not clean up after errors" echo echo "If the data directory is not specified, the environment variable PGDATA" echo "is used." echo echo "Report bugs to <pgsql-bugs@postgresql.org>." exit 0fi#-------------------------------------------------------------------------# Resolve the encoding name#-------------------------------------------------------------------------if [ "$ENCODING" ]then ENCODINGID=`$PGPATH/pg_encoding -b $ENCODING` if [ "$?" -ne 0 ] then ( echo "$CMDNAME: pg_encoding failed" echo "Make sure the program was installed correctly." ) 1>&2 exit 1 fi if [ -z "$ENCODINGID" ] then echo "$CMDNAME: \"$ENCODING\" is not a valid server encoding name" 1>&2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -