aboutsummaryrefslogtreecommitdiff
path: root/src/bin/pg_dump/pg_dumpall
blob: 58bedcb1fab018df881ef3612753db06d2ffc13b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/sh
#
# pg_dumpall [pg_dump parameters]
# dumps all databases to standard output
# It also dumps the pg_shadow and pg_group tables, which belong to the
# whole installation rather than any one individual database.
#
# $Header: /cvsroot/pgsql/src/bin/pg_dump/Attic/pg_dumpall,v 1.26 2000/05/05 17:50:38 tgl Exp $
#
# to adapt to System V vs. BSD 'echo'
if echo '\\' | grep '\\\\' >/dev/null 2>&1
then	
	BS='\'			# BSD
else
	BS='\\'			# System V
fi
#
# Dump everyone but the postgres user
# initdb creates him
#
# get the postgres user id
#
POSTGRES_SUPER_USER_ID="`echo \" \
			select datdba \
			from pg_database \
			where datname = 'template1'; \" | \
			psql -A -q -t template1`"
echo "${BS}connect template1"
#
# delete all users in case they run this twice
#
# we don't use POSTGRES_SUPER_USER_ID because the postgres super user id
# could be different on the two installations
#
echo "select datdba into table tmp_pg_shadow \
      from pg_database where datname = 'template1';"
echo "delete from pg_shadow where usesysid <> tmp_pg_shadow.datdba;"
echo "drop table tmp_pg_shadow;"
#
# load all the non-postgres users
# XXX this breaks badly if the layout of pg_shadow ever changes.
# It'd be better to convert the data into CREATE USER commands.
#
echo "copy pg_shadow from stdin;"
psql -q template1 <<END
select pg_shadow.* 
into table tmp_pg_shadow
from pg_shadow
where usesysid <> $POSTGRES_SUPER_USER_ID;
copy tmp_pg_shadow to stdout;
drop table tmp_pg_shadow;
END
echo "${BS}."
#
# copy the pg_group table too
# XXX this breaks badly if the layout of pg_group ever changes.
# It'd be better to convert the data into CREATE GROUP commands.
#
echo "delete from pg_group;"
echo "copy pg_group from stdin;"
psql -q template1 <<END
copy pg_group to stdout;
END
echo "${BS}."
#
# For each database, run pg_dump to dump the contents of that database.
#
psql -A -q -t -c "select * from pg_database" template1 | grep '|' | tr '|' ' ' | \
grep -v '^template1 ' | \
while read DATABASE DBUSERID ENCODING DATAPATH
do
	DBUSERNAME="`echo \" \
		select usename \
		from pg_user \
		where usesysid = $DBUSERID; \" | \
		psql -A -q -t template1`"

	echo "${BS}connect template1 $DBUSERNAME"

	if sh -c "pg_encoding $ENCODING" >/dev/null 2>&1
	then
		echo "create database $DATABASE with encoding='`pg_encoding $ENCODING`';"
	else
		echo "create database $DATABASE;"
	fi

	echo "${BS}connect $DATABASE $POSTGRES_USER"
	pg_dump ${1+"$@"} $DATABASE
	if [ "$?" -ne 0 ]
	then	echo "pg_dump failed on $DATABASE, exiting" 1>&2
		exit 1
	fi
done

exit 0