0

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 4

2

I think you probably want REASSIGN OWNED instead. No need to identify all the objects, just the users.

Sign up to request clarification or add additional context in comments.

Comments

1

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

I am trying below query but it is throwing a syntax error. Am I missing something? reassign owned postgres to "Arun";
What specific query are you using? Also, what version of PostgreSQL?
The query I have used is: reassign owned postgres to "Arun"; (using pgadmin). I am using version 8.3
It's REASSIGN OWNED BY A TO B.
Please link to the current version of the manual - or to a matching version if required. v8.2 is outdated. I fixed the link (and the format) - Arun has declared version 8.3.
1

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

Thanks a lot for your reply. This will give me all tables, but what about other objects like schema, constraints, indexes, triggers etc? Is there any way I can list them?
I have just updated the answer. Take a look at the link I posted. pg_class and/or pg_index is probably what you need for indexes. pg_proc for triggers.
0

If you can do a pg_dump and pg_restore to create a new database the adding the --no-owner flag on the pg_restore and running the pg_restore as the user you wish to set ownership to should work for this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.