Skip to content

Instantly share code, notes, and snippets.

@shivanshs9
Created September 5, 2020 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shivanshs9/b321f21bf78cedd660ee94f1d4352eb1 to your computer and use it in GitHub Desktop.
Save shivanshs9/b321f21bf78cedd660ee94f1d4352eb1 to your computer and use it in GitHub Desktop.
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