How to change ownership of a table in a database

I have an rndb database and created a new table myname_record, which is created with the default owner "postgres". In my program, I had to change the owner to "rndb", but I missed it. Now I need to do this in the console, so I enter the command

psql=>psql -Urndb 

and then change of ownership with the following query

 rndb=>ALTER TABLE public.myname_record OWNER to rndb; 

but he says that you must be the owner to make these changes. I can understand, because I login via rndb, it gives this error. But how to do it really.

+13
source share
2 answers

Select a role with superuser privileges and try changing the owner of the table.

 ALTER TABLE public.myname_record OWNER TO rndb; 
+28
source

You must connect as the current owner of the table, not the user to whom you want to change the owner of the table. Since this is postgres :

 psql -U postgres 

or

 sudo -u postgres psql 

as needed.

(In addition, the superuser can always change the owners of the tables from anything to anything).

+2
source

All Articles