-
Notifications
You must be signed in to change notification settings - Fork 0
/
change_db_owner.sh
executable file
·51 lines (43 loc) · 1.5 KB
/
change_db_owner.sh
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
#!/bin/bash
usage()
{
cat << EOF
usage: $0 options
This script set ownership for all table, sequence and views for a given database
Credit: Based on http://stackoverflow.com/a/2686185/305019 by Alex Soto
Also merged changes from @sharoonthomas
OPTIONS:
-h Show this message
-d Database name
-o Owner
EOF
}
DB_NAME=
NEW_OWNER=
while getopts "hd:o:" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
d)
DB_NAME=$OPTARG
;;
o)
NEW_OWNER=$OPTARG
;;
esac
done
if [[ -z $DB_NAME ]] || [[ -z $NEW_OWNER ]]
then
usage
exit 1
fi
for tbl in `psql -U postgres -qAt -c "select '\"' || schemaname || '\".\"' || tablename || '\"' from pg_tables where schemaname <> 'pg_catalog' and schemaname <> 'information_schema';" ${DB_NAME}` \
`psql -U postgres -qAt -c "select '\"' || sequence_schema || '\".\"' || sequence_name || '\"' from information_schema.sequences where sequence_schema <> 'pg_catalog' and sequence_schema <> 'information_schema';" ${DB_NAME}` \
`psql -U postgres -qAt -c "select '\"' || table_schema || '\".\"' || table_name || '\"' from information_schema.views where table_schema <> 'pg_catalog' and table_schema <> 'information_schema';" ${DB_NAME}` ;
do
echo "psql -U postgres -c \"ALTER TABLE $tbl OWNER TO \"${NEW_OWNER}\"\" ${DB_NAME}" ;
psql -U postgres -c "ALTER TABLE $tbl OWNER TO \"${NEW_OWNER}\"" ${DB_NAME} ;
done