1. If the user by mistake creates all his objects with super-user(postgres), and later intend to change to other user, then REASSIGN OWNED BY will not work and it merely error out as:
postgres=# reassign owned by postgres to user1;
ERROR: cannot reassign ownership of objects owned by role postgres because they are required by the database system
2. If user wish to change just only one schema objects ownership.
Either cases of changing objects, from “postgres” user to other user or just changing only one schema objects, we need to loop through each object by collecting object details from pg_catalog’s & information_schema and calling ALTER TABLE / FUNCTION / AGGREGATE / TYPE etc.
I liked the technique of tweaking pg_dump output using OS commands(sed/egrep), because it known that by nature the pg_dump writes ALTER .. OWNER TO of every object (TABLES / SEQUENCES / VIEWS / FUNCTIONS / AGGREGATES / TYPES) in its output. Grep’ing those statements from pg_dump stdout by replacing new USER/SCHEMANAME with sed and then passing back those statements to psql client will fix the things even if the object owned by Postgres user. I used same approach in script and allowed user to pass NEW USER NAME and SCHEMA NAME, so to replace it in ALTER…OWNER TO.. statement.
Script usage and output:
sh change_owner.sh -n new_rolename -S schema_name
-bash-4.1$ sh change_owner.sh -n user1 -S public
Tables/Sequences/Views : 16
Functions : 43
Aggregates : 1
Type : 2
You can download the script from here, and there’s also README to help you on the usage.