Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the ownership of a table in database

I Have a database rndb and created a new table myname_record which is created with owner "postgres" by default. In my program i should have change the owner to "rndb" but i missed it. Now i need to do it in console so i am login with command

psql=>psql -Urndb  

and then changing the owner with following query

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

but it is saying you must be owner to do this changes. I can understood because i am login through rndb it is giving this error. But how to make this changes actually.

like image 892
monu Avatar asked Aug 05 '15 06:08

monu


People also ask

How do you change ownership of a table?

You must own the table to use ALTER TABLE. To change the schema of a table, you must also have CREATE privilege on the new schema. To alter the owner, you must also be a direct or indirect member of the new owning role, and that role must have CREATE privilege on the table's schema.

How do I change the owner of a table in SQL Server?

To change the owner of a securable, use ALTER AUTHORIZATION. To change a schema, use ALTER SCHEMA.

How do I change the owner of a table in Oracle?

You can't change the owner of a table. You can create a new table that is owned by NEW_USER , copy the data from the old table to the new table, drop the foreign key constraints that reference the old table, and create new foreign key constraints that reference the new table.


2 Answers

Select a role with superuser permission and try to change the owner of your table.

ALTER TABLE public.myname_record OWNER TO rndb; 
like image 122
Abhishek Avatar answered Sep 23 '22 08:09

Abhishek


You must connect as the current table owner, not the user you wish to change the table ownership to. Since that's postgres:

psql -U postgres 

or

sudo -u postgres psql 

as required.

(Also, a superuser can always change table ownerships from anything to anything).

like image 28
Craig Ringer Avatar answered Sep 26 '22 08:09

Craig Ringer