Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 36d2cc1

Browse files
committed
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.
1 parent a833c44 commit 36d2cc1

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

src/bin/pg_dump/pg_dumpall.sh

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# and "pg_group" tables, which belong to the whole installation rather
77
# than any one individual database.
88
#
9-
# $Header: /cvsroot/pgsql/src/bin/pg_dump/Attic/pg_dumpall.sh,v 1.15 2002/02/11 00:18:20 tgl Exp $
9+
# $Header: /cvsroot/pgsql/src/bin/pg_dump/Attic/pg_dumpall.sh,v 1.16 2002/02/24 21:57:23 tgl Exp $
1010

1111
CMDNAME=`basename $0`
1212

@@ -146,7 +146,7 @@ if [ "$usage" ] ; then
146146
echo " -p, --port=PORT Server port number"
147147
echo " -U, --username=NAME Connect as specified database user"
148148
echo " -W, --password Force password prompts (should happen automatically)"
149-
echo "Any extra options will be passed to pg_dump. The dump will be written"
149+
echo "Any other options will be passed to pg_dump. The dump will be written"
150150
echo "to the standard output."
151151
echo
152152
echo "Report bugs to <pgsql-bugs@postgresql.org>."
@@ -155,13 +155,13 @@ fi
155155

156156

157157
PSQL="${PGPATH}/psql $connectopts"
158-
PGDUMP="${PGPATH}/pg_dump $connectopts $pgdumpextraopts -Fp"
158+
PGDUMP="${PGPATH}/pg_dump $connectopts $pgdumpextraopts -X use-set-session-authorization -Fp"
159159

160160

161161
echo "--"
162162
echo "-- pg_dumpall ($VERSION) $connectopts $pgdumpextraopts"
163163
echo "--"
164-
echo "${BS}connect template1"
164+
echo "${BS}connect \"template1\""
165165

166166
#
167167
# Dump users (but not the user created by initdb)
@@ -189,7 +189,8 @@ echo
189189
echo "DELETE FROM pg_group;"
190190
echo
191191

192-
$PSQL -d template1 -At -F ' ' -c 'SELECT * FROM pg_group;' | \
192+
$PSQL -d template1 -At -F ' ' \
193+
-c 'SELECT groname,grosysid,grolist FROM pg_group;' | \
193194
while read GRONAME GROSYSID GROLIST ; do
194195
echo "CREATE GROUP \"$GRONAME\" WITH SYSID ${GROSYSID};"
195196
raw_grolist=`echo "$GROLIST" | sed 's/^{\(.*\)}$/\1/' | tr ',' ' '`
@@ -206,44 +207,54 @@ test "$globals_only" = yes && exit 0
206207
# Save stdin for pg_dump password prompts.
207208
exec 4<&0
208209

209-
# For each database, run pg_dump to dump the contents of that database.
210+
# To minimize the number of reconnections (and possibly ensuing password
211+
# prompts) required by the output script, we emit all CREATE DATABASE
212+
# commands during the initial phase of the script, and then run pg_dump
213+
# for each database to dump the contents of that database.
210214
# We skip databases marked not datallowconn, since we'd be unable to
211215
# connect to them anyway (and besides, we don't want to dump template0).
212216

217+
DATABASES=""
218+
213219
$PSQL -d template1 -At -F ' ' \
214220
-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;" | \
215221
while read DATABASE DBOWNER ENCODING ISTEMPLATE DBPATH; do
216-
echo
217-
echo "--"
218-
echo "-- Database $DATABASE"
219-
echo "--"
220-
echo "${BS}connect template1 \"$DBOWNER\""
221-
222-
if [ "$cleanschema" = yes -a "$DATABASE" != template1 ] ; then
223-
echo "DROP DATABASE \"$DATABASE\";"
224-
fi
222+
DATABASES="$DATABASES $DATABASE"
225223

226224
if [ "$DATABASE" != template1 ] ; then
227-
createdbcmd="CREATE DATABASE \"$DATABASE\" WITH TEMPLATE = template0"
225+
echo
226+
227+
if [ "$cleanschema" = yes ] ; then
228+
echo "DROP DATABASE \"$DATABASE\";"
229+
fi
230+
231+
createdbcmd="CREATE DATABASE \"$DATABASE\" WITH OWNER = \"$DBOWNER\" TEMPLATE = template0"
228232
if [ x"$DBPATH" != x"" ] ; then
229233
createdbcmd="$createdbcmd LOCATION = '$DBPATH'"
230234
fi
231235
if [ x"$MULTIBYTE" != x"" ] ; then
232236
createdbcmd="$createdbcmd ENCODING = '$ENCODING'"
233237
fi
234238
echo "$createdbcmd;"
239+
if [ x"$ISTEMPLATE" = xt ] ; then
240+
echo "UPDATE pg_database SET datistemplate = 't' WHERE datname = '$DATABASE';"
241+
fi
235242
fi
243+
done
236244

237-
echo "${BS}connect \"$DATABASE\" \"$DBOWNER\""
245+
for DATABASE in $DATABASES; do
238246
echo "dumping database \"$DATABASE\"..." 1>&2
247+
echo
248+
echo "--"
249+
echo "-- Database $DATABASE"
250+
echo "--"
251+
echo "${BS}connect \"$DATABASE\""
252+
239253
$PGDUMP "$DATABASE" <&4
240254
if [ "$?" -ne 0 ] ; then
241255
echo "pg_dump failed on $DATABASE, exiting" 1>&2
242256
exit 1
243257
fi
244-
if [ x"$ISTEMPLATE" = xt ] ; then
245-
echo "UPDATE pg_database SET datistemplate = 't' WHERE datname = '$DATABASE';"
246-
fi
247258
done
248259

249260
exit 0

0 commit comments

Comments
 (0)