Commit 36d2cc1d authored by Tom Lane's avatar Tom Lane

Modify pg_dumpall so that output script uses new OWNER option of CREATE

DATABASE; also make it use SET SESSION AUTHORIZATION commands rather than
\connect commands.  This makes it possible to restore databases belonging
to users who do not have CREATEDB privilege.  It should also become at
least somewhat feasible to run the restore script under password
authentication --- you'll get one superuser password prompt per database,
rather than a large number of challenges for passwords belonging to
varying unspecified user names.
parent a833c441
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
# and "pg_group" tables, which belong to the whole installation rather # and "pg_group" tables, which belong to the whole installation rather
# than any one individual database. # than any one individual database.
# #
# $Header: /cvsroot/pgsql/src/bin/pg_dump/Attic/pg_dumpall.sh,v 1.15 2002/02/11 00:18:20 tgl Exp $ # $Header: /cvsroot/pgsql/src/bin/pg_dump/Attic/pg_dumpall.sh,v 1.16 2002/02/24 21:57:23 tgl Exp $
CMDNAME=`basename $0` CMDNAME=`basename $0`
...@@ -146,7 +146,7 @@ if [ "$usage" ] ; then ...@@ -146,7 +146,7 @@ if [ "$usage" ] ; then
echo " -p, --port=PORT Server port number" echo " -p, --port=PORT Server port number"
echo " -U, --username=NAME Connect as specified database user" echo " -U, --username=NAME Connect as specified database user"
echo " -W, --password Force password prompts (should happen automatically)" echo " -W, --password Force password prompts (should happen automatically)"
echo "Any extra options will be passed to pg_dump. The dump will be written" echo "Any other options will be passed to pg_dump. The dump will be written"
echo "to the standard output." echo "to the standard output."
echo echo
echo "Report bugs to <pgsql-bugs@postgresql.org>." echo "Report bugs to <pgsql-bugs@postgresql.org>."
...@@ -155,13 +155,13 @@ fi ...@@ -155,13 +155,13 @@ fi
PSQL="${PGPATH}/psql $connectopts" PSQL="${PGPATH}/psql $connectopts"
PGDUMP="${PGPATH}/pg_dump $connectopts $pgdumpextraopts -Fp" PGDUMP="${PGPATH}/pg_dump $connectopts $pgdumpextraopts -X use-set-session-authorization -Fp"
echo "--" echo "--"
echo "-- pg_dumpall ($VERSION) $connectopts $pgdumpextraopts" echo "-- pg_dumpall ($VERSION) $connectopts $pgdumpextraopts"
echo "--" echo "--"
echo "${BS}connect template1" echo "${BS}connect \"template1\""
# #
# Dump users (but not the user created by initdb) # Dump users (but not the user created by initdb)
...@@ -189,7 +189,8 @@ echo ...@@ -189,7 +189,8 @@ echo
echo "DELETE FROM pg_group;" echo "DELETE FROM pg_group;"
echo echo
$PSQL -d template1 -At -F ' ' -c 'SELECT * FROM pg_group;' | \ $PSQL -d template1 -At -F ' ' \
-c 'SELECT groname,grosysid,grolist FROM pg_group;' | \
while read GRONAME GROSYSID GROLIST ; do while read GRONAME GROSYSID GROLIST ; do
echo "CREATE GROUP \"$GRONAME\" WITH SYSID ${GROSYSID};" echo "CREATE GROUP \"$GRONAME\" WITH SYSID ${GROSYSID};"
raw_grolist=`echo "$GROLIST" | sed 's/^{\(.*\)}$/\1/' | tr ',' ' '` raw_grolist=`echo "$GROLIST" | sed 's/^{\(.*\)}$/\1/' | tr ',' ' '`
...@@ -206,25 +207,28 @@ test "$globals_only" = yes && exit 0 ...@@ -206,25 +207,28 @@ test "$globals_only" = yes && exit 0
# Save stdin for pg_dump password prompts. # Save stdin for pg_dump password prompts.
exec 4<&0 exec 4<&0
# For each database, run pg_dump to dump the contents of that database. # To minimize the number of reconnections (and possibly ensuing password
# prompts) required by the output script, we emit all CREATE DATABASE
# commands during the initial phase of the script, and then run pg_dump
# for each database to dump the contents of that database.
# We skip databases marked not datallowconn, since we'd be unable to # We skip databases marked not datallowconn, since we'd be unable to
# connect to them anyway (and besides, we don't want to dump template0). # connect to them anyway (and besides, we don't want to dump template0).
DATABASES=""
$PSQL -d template1 -At -F ' ' \ $PSQL -d template1 -At -F ' ' \
-c "SELECT datname, coalesce(usename, (select usename from pg_shadow where usesysid=(select datdba from pg_database where datname='template0'))), pg_encoding_to_char(d.encoding), datistemplate, datpath FROM pg_database d LEFT JOIN pg_shadow u ON (datdba = usesysid) WHERE datallowconn ORDER BY 1;" | \ -c "SELECT datname, coalesce(usename, (select usename from pg_shadow where usesysid=(select datdba from pg_database where datname='template0'))), pg_encoding_to_char(d.encoding), datistemplate, datpath FROM pg_database d LEFT JOIN pg_shadow u ON (datdba = usesysid) WHERE datallowconn ORDER BY 1;" | \
while read DATABASE DBOWNER ENCODING ISTEMPLATE DBPATH; do while read DATABASE DBOWNER ENCODING ISTEMPLATE DBPATH; do
DATABASES="$DATABASES $DATABASE"
if [ "$DATABASE" != template1 ] ; then
echo echo
echo "--"
echo "-- Database $DATABASE"
echo "--"
echo "${BS}connect template1 \"$DBOWNER\""
if [ "$cleanschema" = yes -a "$DATABASE" != template1 ] ; then if [ "$cleanschema" = yes ] ; then
echo "DROP DATABASE \"$DATABASE\";" echo "DROP DATABASE \"$DATABASE\";"
fi fi
if [ "$DATABASE" != template1 ] ; then createdbcmd="CREATE DATABASE \"$DATABASE\" WITH OWNER = \"$DBOWNER\" TEMPLATE = template0"
createdbcmd="CREATE DATABASE \"$DATABASE\" WITH TEMPLATE = template0"
if [ x"$DBPATH" != x"" ] ; then if [ x"$DBPATH" != x"" ] ; then
createdbcmd="$createdbcmd LOCATION = '$DBPATH'" createdbcmd="$createdbcmd LOCATION = '$DBPATH'"
fi fi
...@@ -232,18 +236,25 @@ while read DATABASE DBOWNER ENCODING ISTEMPLATE DBPATH; do ...@@ -232,18 +236,25 @@ while read DATABASE DBOWNER ENCODING ISTEMPLATE DBPATH; do
createdbcmd="$createdbcmd ENCODING = '$ENCODING'" createdbcmd="$createdbcmd ENCODING = '$ENCODING'"
fi fi
echo "$createdbcmd;" echo "$createdbcmd;"
if [ x"$ISTEMPLATE" = xt ] ; then
echo "UPDATE pg_database SET datistemplate = 't' WHERE datname = '$DATABASE';"
fi fi
fi
done
echo "${BS}connect \"$DATABASE\" \"$DBOWNER\"" for DATABASE in $DATABASES; do
echo "dumping database \"$DATABASE\"..." 1>&2 echo "dumping database \"$DATABASE\"..." 1>&2
echo
echo "--"
echo "-- Database $DATABASE"
echo "--"
echo "${BS}connect \"$DATABASE\""
$PGDUMP "$DATABASE" <&4 $PGDUMP "$DATABASE" <&4
if [ "$?" -ne 0 ] ; then if [ "$?" -ne 0 ] ; then
echo "pg_dump failed on $DATABASE, exiting" 1>&2 echo "pg_dump failed on $DATABASE, exiting" 1>&2
exit 1 exit 1
fi fi
if [ x"$ISTEMPLATE" = xt ] ; then
echo "UPDATE pg_database SET datistemplate = 't' WHERE datname = '$DATABASE';"
fi
done done
exit 0 exit 0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment