📄 initdb.sh
字号:
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 10fiif [ $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 2fiecho "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 dirsumask 077if [ -f "$PGDATA/PG_VERSION" ]; then if [ $template_only -eq 0 ]; then echo "$CMDNAME: error: File $PGDATA/PG_VERSION already exists." echo "This probably means initdb has already been run and the " echo "database system already exists." echo echo "If you want to create a new database system, either remove " echo "the directory $PGDATA or run initdb with a --pgdata option " echo "other than $PGDATA." exit 1 fielse if [ ! -d $PGDATA ]; then echo "Creating Postgres database system directory $PGDATA" echo mkdir $PGDATA if [ $? -ne 0 ]; then exit 5; fi fi if [ ! -d $PGDATA/base ]; then echo "Creating Postgres database system directory $PGDATA/base" echo mkdir $PGDATA/base if [ $? -ne 0 ]; then exit 5; fi fifi#----------------------------------------------------------------------------# Create the template1 database#----------------------------------------------------------------------------rm -rf $PGDATA/base/template1mkdir $PGDATA/base/template1if [ "$debug" -eq 1 ]; then BACKEND_TALK_ARG="-d"else BACKEND_TALK_ARG="-Q"fiBACKENDARGS="-boot -C -F -D$PGDATA $BACKEND_TALK_ARG"echo "Creating template database in $PGDATA/base/template1"[ "$debug" -ne 0 ] && echo "Running: postgres $BACKENDARGS template1"cat $TEMPLATE \| sed -e "s/postgres PGUID/$POSTGRES_SUPERUSERNAME $POSTGRES_SUPERUID/" \ -e "s/PGUID/$POSTGRES_SUPERUID/" \| postgres $BACKENDARGS template1if [ $? -ne 0 ]; then echo "$CMDNAME: could not create template database" if [ $noclean -eq 0 ]; then echo "$CMDNAME: cleaning up by wiping out $PGDATA/base/template1" rm -rf $PGDATA/base/template1 else echo "$CMDNAME: cleanup not done because noclean options was used." fi exit 1;fiechopg_version $PGDATA/base/template1#----------------------------------------------------------------------------# Create the global classes, if requested.#----------------------------------------------------------------------------if [ $template_only -eq 0 ]; then echo "Creating global classes in $PGDATA/base" [ "$debug" -ne 0 ] && echo "Running: postgres $BACKENDARGS template1" cat $GLOBAL \ | sed -e "s/postgres PGUID/$POSTGRES_SUPERUSERNAME $POSTGRES_SUPERUID/" \ -e "s/PGUID/$POSTGRES_SUPERUID/" \ | postgres $BACKENDARGS template1 if (test $? -ne 0) then echo "$CMDNAME: could not create global classes." if (test $noclean -eq 0); then echo "$CMDNAME: cleaning up." rm -rf $PGDATA else echo "$CMDNAME: cleanup not done (noclean mode set)." fi exit 1; fi echo pg_version $PGDATA cp $PG_HBA_SAMPLE $PGDATA/pg_hba.conf cp $PG_GEQO_SAMPLE $PGDATA/pg_geqo.sample echo "Adding template1 database to pg_database..." echo "open pg_database" > /tmp/create.$$ echo "insert (template1 $POSTGRES_SUPERUID $MULTIBYTEID template1)" >> /tmp/create.$$ #echo "show" >> /tmp/create.$$ echo "close pg_database" >> /tmp/create.$$ [ "$debug" -ne 0 ] && echo "Running: postgres $BACKENDARGS template1 < /tmp/create.$$" postgres $BACKENDARGS template1 < /tmp/create.$$ if [ $? -ne 0 ]; then echo "$CMDNAME: could not log template database" if [ $noclean -eq 0 ]; then echo "$CMDNAME: cleaning up." rm -rf $PGDATA else echo "$CMDNAME: cleanup not done (noclean mode set)." fi exit 1; fi rm -f /tmp/create.$$fiechoPGSQL_OPT="-o /dev/null -O -F -Q -D$PGDATA"# If the COPY is first, the VACUUM generates an error, so we vacuum firstecho "Vacuuming template1"echo "vacuum" | postgres $PGSQL_OPT template1 > /dev/nullecho "COPY pg_shadow TO '$PGDATA/pg_pwd' USING DELIMITERS '\\t'" | \ postgres $PGSQL_OPT template1 > /dev/nullecho "Creating public pg_user view"echo "CREATE TABLE pg_user ( \ usename name, \ usesysid int4, \ usecreatedb bool, \ usetrace bool, \ usesuper bool, \ usecatupd bool, \ passwd text, \ valuntil abstime);" | postgres $PGSQL_OPT template1 > /dev/nullecho "CREATE RULE \"_RETpg_user\" AS ON SELECT TO pg_user DO INSTEAD \ SELECT usename, usesysid, usecreatedb, usetrace, \ usesuper, usecatupd, '********'::text as passwd, \ valuntil FROM pg_shadow;" | \ postgres $PGSQL_OPT template1 > /dev/nullecho "REVOKE ALL on pg_shadow FROM public" | \ postgres $PGSQL_OPT template1 > /dev/nullecho "Creating view pg_rules"echo "CREATE TABLE pg_rules ( \ tablename name, \ rulename name, \ definition text);" | postgres $PGSQL_OPT template1 > /dev/nullecho "CREATE RULE \"_RETpg_rules\" AS ON SELECT TO pg_rules DO INSTEAD \ 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;" | \ postgres $PGSQL_OPT template1 > /dev/nullecho "Creating view pg_views"echo "CREATE TABLE pg_views ( \ viewname name, \ viewowner name, \ definition text);" | postgres $PGSQL_OPT template1 > /dev/nullecho "CREATE RULE \"_RETpg_views\" AS ON SELECT TO pg_views DO INSTEAD \ 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');" | \ postgres $PGSQL_OPT template1 > /dev/nullecho "Creating view pg_tables"echo "CREATE TABLE pg_tables ( \ tablename name, \ tableowner name, \ hasindexes bool, \ hasrules bool, \ hastriggers bool);" | postgres $PGSQL_OPT template1 > /dev/nullecho "CREATE RULE \"_RETpg_tables\" AS ON SELECT TO pg_tables DO INSTEAD \ 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');" | \ postgres $PGSQL_OPT template1 > /dev/nullecho "Creating view pg_indexes"echo "CREATE TABLE pg_indexes ( \ tablename name, \ indexname name, \ indexdef text);" | postgres $PGSQL_OPT template1 > /dev/nullecho "CREATE RULE \"_RETpg_indexes\" AS ON SELECT TO pg_indexes DO INSTEAD \ 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;" | \ postgres $PGSQL_OPT template1 > /dev/nullecho "Loading pg_description"echo "copy pg_description from '$TEMPLATE_DESCR'" | \ postgres $PGSQL_OPT template1 > /dev/nullecho "copy pg_description from '$GLOBAL_DESCR'" | \ postgres $PGSQL_OPT template1 > /dev/nullecho "vacuum analyze" | \ postgres $PGSQL_OPT template1 > /dev/null
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -