0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

2022-07-29 意図せず postgres ユーザが owner となっているテーブルを洗い出す

Posted at

前書き

\d コマンドでテーブルの一覧を確認していたら、 owner が postgres になっていたり、アプリケーションユーザになっていたりと秩序のない状態となっていた。

これをアプリケーションユーザに揃えることにした。

                               List of relations
 Schema |                      Name                      |   Type   |   Owner
--------+------------------------------------------------+----------+-----------
 public | test1                                          | table    | postgres
 public | test2                                          | table    | user
 public | test3                                          | table    | postgres
 public | test4                                          | table    | user
 public | test5                                          | table    | postgres
 public | test6                                          | table    | postgres
 public | test7                                          | table    | user

方法

下記のようなコマンドラインを流し込み、一括で変更をかける。

echo "select tablename from pg_tables where schemaname = 'public' AND tableowner = 'postgres';" \
| psql -U user -d db -t \
| xargs -I {} echo 'ALTER TABLE {} OWNER TO user;' \
| psql -U user -d db

change table owner postgres Code Example (codegrepper.com)

pg_tables

\d クエリにおいて、where 句を利用した絞り込みは行うことができない。

pg_tables テーブルで似たような情報を得ることができるので、これを利用する。

What is a PostgreSQL table owner? - Stack Overflow

また、 pg_tables にはデータベースシステムの設定情報のテーブルが含まれる。

これのユーザを postgres ユーザから変更したくはないため、 public 名前空間に絞って変換する。

psql コマンドの -t オプション

これを用いて psql コマンドを実行することで、ヘッダと末尾の行数出力がなくなる。

パイプを使って値を受け渡す際に利用しやすくなる。

How to turn off header only in psql (postgresql) - Database Administrators Stack Exchange

実行結果の確認

再度 \d を実行し、各テーブルの owner を確認する。

                               List of relations
 Schema |                      Name                      |   Type   |   Owner
--------+------------------------------------------------+----------+-----------
 public | test1                                          | table    | user
 public | test2                                          | table    | user
 public | test3                                          | table    | user
 public | test4                                          | table    | user
 public | test5                                          | table    | user
 public | test6                                          | table    | user
 public | test7                                          | table    | user
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?