#!/bin/bash
MASTER_USER=
MASTER_PW=
INSTANCE=""
PORT=5432

cat db_list | while read line
do
	array[i]="$line"
#Change owner Tables
	for tbl in `PGPASSWORD=$MASTER_PW psql -h $INSTANCE -U $MASTER_USER -qAt -c "select tablename from pg_tables where schemaname = 'public';" ${array[i]}`;
	do
		PGPASSWORD=$MASTER_PW psql -h $INSTANCE -U $MASTER_USER -c "alter table $tbl owner to ${array[i]}_user" ${array[i]};
	done

#Change owner Sequences
       	for tbl in `PGPASSWORD=$MASTER_PW psql -h $INSTANCE -U $MASTER_USER -qAt -c "select sequence_name from information_schema.sequences where sequence_schema = 'public';" ${array[i]}`;
       	do
		PGPASSWORD=$MASTER_PW psql -h $INSTANCE -U $MASTER_USER -c "alter table \"$tbl\" owner to ${array[i]}_user" ${array[i]};
	done

#Change owner Views
	for tbl in `PGPASSWORD=$MASTER_PW psql -h $INSTANCE -U $MASTER_USER -qAt -c "select table_name from information_schema.views where table_schema = 'public';" ${array[i]}`;
	do
		PGPASSWORD=$MASTER_PW psql -h $INSTANCE -U $MASTER_USER -c "alter table \"$tbl\" owner to ${array[i]}_user" ${array[i]};
	done
    let i++
done

