Last active
July 8, 2020 08:33
-
-
Save dmutende/4c5da8d26e57f0900e923ff3baea2c3c to your computer and use it in GitHub Desktop.
ALTER owner to tables, sequences and views on PostgreSQL databases using bash
This file contains 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
#!/bin/bash | |
DB=[YOUR_DB_NAME] | |
OWNER=[NEW_OWNER] | |
# alter tables owner | |
for tbl in `psql -qAt -c "select tablename from pg_tables where schemaname = 'public';" $DB` ; do psql -c "alter table \"$tbl\" owner to $OWNER" $DB ; done | |
# alter sequences owner | |
for tbl in `psql -qAt -c "select sequence_name from information_schema.sequences where sequence_schema = 'public';" $DB` ; do psql -c "alter table \"$tbl\" owner to $OWNER" $DB ; done | |
# alter views owner | |
for tbl in `psql -qAt -c "select table_name from information_schema.views where table_schema = 'public';" $DB` ; do psql -c "alter table \"$tbl\" owner to $OWNER" $DB ; done | |
#set timezone | |
psql -c "ALTER DATABASE\"$DB\" SET timezone TO 'Africa/Nairobi';" $DB |
I've corrected a minor error on # alter tables owner, It was missing the backticks (`)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah. The first one for tables, then for sequences then for views. Respectively. It'll loop through the various database objects