Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to simultaneously reassign owner of all tables in a given DB (source: https://stackoverflow.com/a/2686185/2674983)
### Use:
# reassign_owner dbName newOwner oldOwner/root dbHost
reassign_owner() {
db="$1"
newUser="$2"
owner="${3:-$newUser}"
dbHost="$4"
local pw
echo -n "Password: " > /dev/tty
read -s pw
for tbl in $(PGPASSWORD="$pw" \
psql -At -h "$dbHost" \
-c "select tablename from pg_tables where schemaname = 'public';" \
-U "$owner" \
"$db" \
); do
PGPASSWORD="$pw" psql -c "alter table \"$tbl\" owner to $newUser" \
-h "$dbHost" \
-U "$owner" \
"$db"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment