Help us understand the problem. What is going on with this article?

postgresのテーブルの所有者を一括で変更する

More than 3 years have passed since last update.

postgresでデータベースの所有者を変更したいときにALTER DATABASE hoge OWNER TO new_userで行けるかと思ったのですが、database以下のtableもひとつずつ所有者を変更する必要があるみたいです。
テーブルが大量にある場合、ひとつずつALTER TABLE huga OWNER TO new_userをするのは大変!ということで、ぐぐったら下記のような解決策がでてきました。

下記を実行するとSQL文が発行されます。

SELECT
    'ALTER TABLE ' || schemaname 
    || '.' || tablename || 
    ' OWNER TO new_user;'
FROM pg_tables 
WHERE tableowner ='old_user'
 ALTER TABLE public.xxxxxxx OWNER TO xxxxxx;
 ALTER TABLE public.xxxxxxx OWNER TO xxxxxx;
 ALTER TABLE public.xxxxxxx OWNER TO xxxxxx;
 ALTER TABLE public.xxxxxxx OWNER TO xxxxxx;
 ALTER TABLE public.xxxxxxx OWNER TO xxxxxx;
 ALTER TABLE public.xxxxxxx OWNER TO xxxxxx;
 ALTER TABLE public.xxxxxxx OWNER TO xxxxxx;
 ALTER TABLE public.xxxxxxx OWNER TO xxxxxx;
…

あとはこれをコピーして実行すればok。
テーブルに紐づくスキーマの権限も同時に変更されました。

(参照:http://docolog.cocolog-nifty.com/papalog/2011/06/postgresql-8caf.html)

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away