Created
September 5, 2020 22:17
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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