📄 initdb.sh
字号:
exit 1 fifi#-------------------------------------------------------------------------# Make sure he told us where to build the database system#-------------------------------------------------------------------------if [ -z "$PGDATA" ]then ( echo "$CMDNAME: no data directory specified" echo "You must identify the directory where the data for this database system" echo "will reside. Do this with either the invocation option -D or the" echo "environment variable PGDATA." ) 1>&2 exit 1fi#-------------------------------------------------------------------------# Find the input files#-------------------------------------------------------------------------POSTGRES_BKI="$datadir"/postgres.bkiPOSTGRES_DESCR="$datadir"/postgres.descriptionPG_HBA_SAMPLE="$datadir"/pg_hba.conf.samplePG_IDENT_SAMPLE="$datadir"/pg_ident.conf.samplePOSTGRESQL_CONF_SAMPLE="$datadir"/postgresql.conf.sampleif [ "$show_setting" = yes ] || [ "$debug" = yes ]then ( echo echo "$CMDNAME: internal variables:" for var in PGDATA datadir PGPATH ENCODING ENCODINGID \ POSTGRES_SUPERUSERNAME POSTGRES_BKI \ POSTGRES_DESCR POSTGRESQL_CONF_SAMPLE \ PG_HBA_SAMPLE PG_IDENT_SAMPLE ; do eval "echo ' '$var=\$$var" done ) 1>&2fiif [ "$show_setting" = yes ] ; then exit 0fifor PREREQ_FILE in "$POSTGRES_BKI" "$POSTGRES_DESCR" \ "$PG_HBA_SAMPLE" "$PG_IDENT_SAMPLE" "$POSTGRESQL_CONF_SAMPLE"do if [ ! -f "$PREREQ_FILE" ] ; then ( echo "$CMDNAME: file \"$PREREQ_FILE\" not found" echo "This means you have a corrupted installation or identified the" echo "wrong directory with the invocation option -L." ) 1>&2 exit 1 fidonefor file in "$POSTGRES_BKI"do if [ x"`sed 1q $file`" != x"# PostgreSQL $short_version" ]; then ( echo "$CMDNAME: input file \"$file\" does not belong to PostgreSQL $VERSION" echo "Check your installation or specify the correct path using the option -L." ) 1>&2 exit 1 fidonetrap 'echo "Caught signal." ; exit_nicely' 1 2 3 15# Let's goecho "The files belonging to this database system will be owned by user \"$EffectiveUser\"."echo "This user must also own the server process."echoTAB=' 'if test x`pg_getlocale CTYPE` = x`pg_getlocale COLLATE` \ && test x`pg_getlocale CTYPE` = x`pg_getlocale TIME` \ && test x`pg_getlocale CTYPE` = x`pg_getlocale NUMERIC` \ && test x`pg_getlocale CTYPE` = x`pg_getlocale MONETARY` \ && test x`pg_getlocale CTYPE` = x`pg_getlocale MESSAGES`then echo "The database cluster will be initialized with locale `pg_getlocale CTYPE`."else echo "The database cluster will be initialized with locales:" echo " COLLATE: `pg_getlocale COLLATE`" echo " CTYPE: `pg_getlocale CTYPE`" echo " MESSAGES: `pg_getlocale MESSAGES`" echo " MONETARY: `pg_getlocale MONETARY`" echo " NUMERIC: `pg_getlocale NUMERIC`" echo " TIME: `pg_getlocale TIME`"fiecho############################################################################ CREATE DATABASE DIRECTORY# umask must disallow access to group, other for files and dirsumask 077# find out if directory is emptypgdata_contents=`ls -A "$PGDATA" 2>/dev/null`if [ x"$pgdata_contents" != x ]then ( echo "$CMDNAME: 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 $CMDNAME with an argument other than" echo "\"$PGDATA\"." ) 1>&2 exit 1else if [ ! -d "$PGDATA" ]; then $ECHO_N "creating directory $PGDATA... "$ECHO_C mkdir -p "$PGDATA" >/dev/null 2>&1 || mkdir "$PGDATA" || exit_nicely made_new_pgdata=yes else $ECHO_N "fixing permissions on existing directory $PGDATA... "$ECHO_C chmod go-rwx "$PGDATA" || exit_nicely fi echo "ok" if [ ! -d "$PGDATA"/base ] then $ECHO_N "creating directory $PGDATA/base... "$ECHO_C mkdir "$PGDATA"/base || exit_nicely echo "ok" fi if [ ! -d "$PGDATA"/global ] then $ECHO_N "creating directory $PGDATA/global... "$ECHO_C mkdir "$PGDATA"/global || exit_nicely echo "ok" fi if [ ! -d "$PGDATA"/pg_xlog ] then $ECHO_N "creating directory $PGDATA/pg_xlog... "$ECHO_C mkdir "$PGDATA"/pg_xlog || exit_nicely echo "ok" fi if [ ! -d "$PGDATA"/pg_clog ] then $ECHO_N "creating directory $PGDATA/pg_clog... "$ECHO_C mkdir "$PGDATA"/pg_clog || exit_nicely echo "ok" fifi# Top level PG_VERSION is checked by bootstrapper, so make it firstecho "$short_version" > "$PGDATA/PG_VERSION" || exit_nicely############################################################################ DETERMINE PLATFORM-SPECIFIC CONFIG SETTINGS## Use reasonable values if kernel will let us, else scale back. Probe for# max_connections first since it is subject to more constraints than# shared_buffers.# common backend optionsPGSQL_OPT="-F -D$PGDATA"cp /dev/null "$PGDATA"/postgresql.conf || exit_nicely$ECHO_N "selecting default max_connections... "$ECHO_Cfor nconns in 100 50 40 30 20 10do nbuffers=`expr $nconns '*' 5` TEST_OPT="$PGSQL_OPT -c shared_buffers=$nbuffers -c max_connections=$nconns" if "$PGPATH"/postgres -boot -x0 $TEST_OPT template1 </dev/null >/dev/null 2>&1 then break fidoneecho "$nconns"$ECHO_N "selecting default shared_buffers... "$ECHO_Cfor nbuffers in 1000 900 800 700 600 500 400 300 200 100 50do TEST_OPT="$PGSQL_OPT -c shared_buffers=$nbuffers -c max_connections=$nconns" if "$PGPATH"/postgres -boot -x0 $TEST_OPT template1 </dev/null >/dev/null 2>&1 then break fidoneecho "$nbuffers"############################################################################ CREATE CONFIG FILES$ECHO_N "creating configuration files... "$ECHO_Csed -e "s/^#shared_buffers = 1000/shared_buffers = $nbuffers/" \ -e "s/^#max_connections = 100/max_connections = $nconns/" \ -e "s/^#lc_messages = 'C'/lc_messages = '`pg_getlocale MESSAGES`'/" \ -e "s/^#lc_monetary = 'C'/lc_monetary = '`pg_getlocale MONETARY`'/" \ -e "s/^#lc_numeric = 'C'/lc_numeric = '`pg_getlocale NUMERIC`'/" \ -e "s/^#lc_time = 'C'/lc_time = '`pg_getlocale TIME`'/" \ "$POSTGRESQL_CONF_SAMPLE" > "$PGDATA"/postgresql.conf || exit_nicelyif [ "x$HAVE_IPV6" = xyes ]then cp "$PG_HBA_SAMPLE" "$PGDATA"/pg_hba.conf || exit_nicelyelse sed -e "/ ::1 / s/^host/#host/" \ "$PG_HBA_SAMPLE" > "$PGDATA"/pg_hba.conf || exit_nicelyficp "$PG_IDENT_SAMPLE" "$PGDATA"/pg_ident.conf || exit_nicelychmod 0600 "$PGDATA"/pg_hba.conf "$PGDATA"/pg_ident.conf \ "$PGDATA"/postgresql.confecho "ok"############################################################################ RUN BKI SCRIPT IN BOOTSTRAP MODE TO CREATE TEMPLATE1if [ "$debug" = yes ]then BOOTSTRAP_TALK_ARG="-d 5"fiunset PGCLIENTENCODING$ECHO_N "creating template1 database in $PGDATA/base/1... "$ECHO_Crm -rf "$PGDATA"/base/1 || exit_nicelymkdir "$PGDATA"/base/1 || exit_nicelycat "$POSTGRES_BKI" \| sed -e "s/POSTGRES/$POSTGRES_SUPERUSERNAME/g" \ -e "s/ENCODING/$ENCODINGID/g" \| ( LC_COLLATE=`pg_getlocale COLLATE` LC_CTYPE=`pg_getlocale CTYPE` export LC_COLLATE export LC_CTYPE unset LC_ALL "$PGPATH"/postgres -boot -x1 $PGSQL_OPT $BOOTSTRAP_TALK_ARG template1) \|| exit_nicely# Make the per-database PGVERSION for template1 only after init'ing itecho "$short_version" > "$PGDATA/base/1/PG_VERSION" || exit_nicelyecho "ok"############################################################################ CREATE VIEWS and other things## NOTE: because here we are driving a standalone backend (not psql), we must# follow the standalone backend's convention that commands end at a newline.# To break an SQL command across lines in this script, backslash-escape all# internal newlines in the command.PGSQL_OPT="$PGSQL_OPT -O -c search_path=pg_catalog -c exit_on_error=true"$ECHO_N "initializing pg_shadow... "$ECHO_C"$PGPATH"/postgres $PGSQL_OPT template1 >/dev/null <<EOF-- Create a trigger so that direct updates to pg_shadow will be written-- to the flat password/group files pg_pwd and pg_groupCREATE TRIGGER pg_sync_pg_pwd AFTER INSERT OR UPDATE OR DELETE ON pg_shadow \FOR EACH ROW EXECUTE PROCEDURE update_pg_pwd_and_pg_group();CREATE TRIGGER pg_sync_pg_group AFTER INSERT OR UPDATE OR DELETE ON pg_group \FOR EACH ROW EXECUTE PROCEDURE update_pg_pwd_and_pg_group();-- needs to be done before alter user, because alter user checks that-- pg_shadow is secure ...REVOKE ALL on pg_shadow FROM public;EOFif [ "$?" -ne 0 ]; then exit_nicelyfiecho "ok"# set up passwordif [ "$PwPrompt" ]; then $ECHO_N "Enter new superuser password: "$ECHO_C stty -echo > /dev/null 2>&1 read FirstPw stty echo > /dev/null 2>&1 echo $ECHO_N "Enter it again: "$ECHO_C stty -echo > /dev/null 2>&1 read SecondPw stty echo > /dev/null 2>&1 echo if [ "$FirstPw" != "$SecondPw" ]; then echo "Passwords didn't match." 1>&2 exit_nicely fi $ECHO_N "setting password... "$ECHO_C "$PGPATH"/postgres $PGSQL_OPT template1 >/dev/null <<EOF ALTER USER "$POSTGRES_SUPERUSERNAME" WITH PASSWORD '$FirstPw';EOF if [ "$?" -ne 0 ]; then exit_nicely fi if [ ! -f "$PGDATA"/global/pg_pwd ]; then echo echo "$CMDNAME: The password file wasn't generated. Please report this problem." 1>&2 exit_nicely fi echo "ok"fi$ECHO_N "enabling unlimited row size for system tables... "$ECHO_C"$PGPATH"/postgres $PGSQL_OPT template1 >/dev/null <<EOFALTER TABLE pg_attrdef CREATE TOAST TABLE;ALTER TABLE pg_constraint CREATE TOAST TABLE;ALTER TABLE pg_database CREATE TOAST TABLE;ALTER TABLE pg_description CREATE TOAST TABLE;ALTER TABLE pg_group CREATE TOAST TABLE;ALTER TABLE pg_proc CREATE TOAST TABLE;ALTER TABLE pg_rewrite CREATE TOAST TABLE;ALTER TABLE pg_shadow CREATE TOAST TABLE;ALTER TABLE pg_statistic CREATE TOAST TABLE;EOFif [ "$?" -ne 0 ]; then exit_nicelyfiecho "ok"$ECHO_N "initializing pg_depend... "$ECHO_C"$PGPATH"/postgres $PGSQL_OPT template1 >/dev/null <<EOF-- Make PIN entries in pg_depend for all objects made so far in the tables-- that the dependency code handles. This is overkill (the system doesn't-- really depend on having every last weird datatype, for instance)-- but generating only the minimum required set of dependencies seems hard.-- Note that we deliberately do not pin the system views.-- First delete any already-made entries; PINs override all else, and must-- be the only entries for their objects.DELETE FROM pg_depend;INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' FROM pg_class;INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' FROM pg_proc;INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' FROM pg_type;INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' FROM pg_cast;INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' FROM pg_constraint;INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' FROM pg_attrdef;INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' FROM pg_language;INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' FROM pg_operator;INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' FROM pg_opclass;INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' FROM pg_rewrite;INSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' FROM pg_trigger;-- restriction here to avoid pinning the public namespaceINSERT INTO pg_depend SELECT 0,0,0, tableoid,oid,0, 'p' FROM pg_namespace \ WHERE nspname LIKE 'pg%';EOFif [ "$?" -ne 0 ]; then exit_nicelyfiecho "ok"$ECHO_N "creating system views... "$ECHO_C"$PGPATH"/postgres $PGSQL_OPT template1 >/dev/null <<EOFCREATE VIEW pg_user AS \ SELECT \ usename, \ usesysid, \ usecreatedb, \ usesuper, \ usecatupd, \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -