Can someone help me with this? I need to find out all the objects in a database using a script. The reason why I need this is, I am asked to change the owner of all database objects so for that I need to first list down all objects.
4 Answers
I think you probably want REASSIGN OWNED instead. No need to identify all the objects, just the users.
Comments
If you're changing every object from role A to role B, you might like REASSIGN OWNED:
REASSIGN OWNED BY A TO B
5 Comments
REASSIGN OWNED BY A TO B.In PostgreSQL (or almost any other RDBMS for that matter), I would recommend to take a look at metadata tables (system catalog).
Example. You want all tables:
db => \d pg_tables
View "pg_catalog.pg_tables"
Column | Type | Modifiers
-------------+---------+-----------
schemaname | name |
tablename | name |
tableowner | name |
tablespace | name |
hasindexes | boolean |
hasrules | boolean |
hastriggers | boolean |
db => select tablename from pg_tables;
Will get you a list of all tables. You can use a query to build a script to change ownership of the tables you want.
Similarly, you can query other views/tables in the catalog to get other object types (sequences, indexes, you name it).
2 Comments
pg_class and/or pg_index is probably what you need for indexes. pg_proc for triggers.