How do I modify all tables in a PostgreSQL database to change the owner?
I tried ALTER TABLE * OWNER TO new_owner
but it doesn't support the asterix syntax.
How do I modify all tables in a PostgreSQL database to change the owner? I tried |
||||
|
Since you're changing the owner ship for all tables, you likely want views and sequences too. Here's what I did: Tables:
Sequences:
Views:
You could probably DRY that up a bit since the alter statements are identical for all three. As @trygvis mentions below, the |
|||||||||||||||||
|
There is no such command in PostgreSQL. But you can work around it using method I described some time ago for GRANTs. |
|||
|
I recently had to change the ownership of all objects in a database. Although tables, views, triggers and sequences were somewhat easily changed the above approach failed for functions as the signature is part of the function name. Granted, I have a MySQL background and am not that familiar with Postgres. However, pg_dump allows you to dump just the schema and this contains the ALTER xxx OWNER TO yyy; statements you need. Here is my bit of shell magic on the topic
|
|||
|
Then pipe the backup file back in to PostgreSQL using:
As there is no owner included then all of the created table, schema, etc, are created under the login user you specify. I have read this could be a good approach for migrating between PostgreSQL versions as well. |
||||
|
This: http://archives.postgresql.org/pgsql-bugs/2007-10/msg00234.php is also a nice and fast solution:
|
|||
If you want to do it in one sql statement, you need to define an exec() function as mentioned in http://wiki.postgresql.org/wiki/Dynamic_DDL
Then you can execute this query, it will change the owner of tables, sequences and views:
$NEWUSER is the postgresql new name of the new owner. In most circumstances you need to be superuser to execute this. You can avoid that by changing the owner from your own user to a role group you are a member of. Thanks to RhodiumToad on #postgresql for helping out with this. |
|||||||||
|
Starting in PostgreSQL 9.0, you have the ability to
PostgreSQL's docs on
|
|||
|
The answer by @Alex Soto is the right one and the gist uploaded by @Yoav Aner also works provided there are no special characters in the table/view names (which are legal in postgres). You need to escape them to work and I have uploaded a gist for that: https://gist.github.com/2911117 |
|||
|
The accepted solution does not take care of function ownership following solution takes care of everything (while reviewing I noticed that it is similar to @magiconair above)
|
|||
|
You can use the Synopsis:
This changes all objects owned by It is available back to at least 8.2. Their online documentation only goes that far back. |
|||||||||||||||
|
very simple, try it...
|
|||
|