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 5 years have passed since last update.

データベースクラスタのロール(ユーザ)を確認する(PostgreSQL)

Last updated at Posted at 2019-10-26

はじめに

 ユーザを確認する方法はいろいろあります。今回はpsqlのメタコマンドを使う方法、情報スキーマを使う方法、システムカタログを使う方法の3つを紹介します。

(1)psqlのメタコマンドでユーザを確認する

 一番簡単な方法です。以下のようにメタコマンドを実行することでデータベースのユーザを確認することができます。

書き方:\du


postgres=# \du                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 suzuki    | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

(2)情報スキーマでユーザを確認する

 情報スキーマのenabled_rolesビューを確認することで、ユーザを確認することができます。
 ちなみに、自分で作ったユーザ(今回の場合はsuzukiのみ)以外にもユーザ名が表示されています。これはもともとPostgreSQLが用意しているデフォルトロールというものです。

書き方:SELECT * FROM information_schema.enabled_roles;

postgres=# select * from information_schema.enabled_roles;
         role_name
---------------------------
 suzuki
 pg_monitor
 pg_read_all_settings
 pg_read_all_stats
 pg_stat_scan_tables
 pg_read_server_files
 pg_write_server_files
 pg_execute_server_program
 pg_signal_backend
(9 rows)

(3)システムカタログでユーザを確認する

 システムカタログのpg_rolesビューを確認することでユーザを確認することができます。情報スキーマ同様、デフォルトロールも表示されます。

書き方:SELECT * FROM pg_catalog.pg_roles;

postgres=# select * from pg_catalog.pg_roles;
-[ RECORD 1 ]--+--------------------------
rolname        | pg_signal_backend
rolsuper       | f
rolinherit     | t
rolcreaterole  | f
rolcreatedb    | f
rolcanlogin    | f
rolreplication | f
rolconnlimit   | -1
rolpassword    | ********
rolvaliduntil  |
rolbypassrls   | f
rolconfig      |
oid            | 4200
-[ RECORD 2 ]--+--------------------------
rolname        | pg_read_server_files
rolsuper       | f
rolinherit     | t
rolcreaterole  | f
rolcreatedb    | f
rolcanlogin    | f
rolreplication | f
rolconnlimit   | -1
rolpassword    | ********
rolvaliduntil  |
rolbypassrls   | f
rolconfig      |
oid            | 4569
-[ RECORD 3 ]--+--------------------------
rolname        | suzuki
rolsuper       | t
rolinherit     | t
rolcreaterole  | t
rolcreatedb    | t
rolcanlogin    | t
rolreplication | t
rolconnlimit   | -1
rolpassword    | ********
rolvaliduntil  |
rolbypassrls   | t
rolconfig      |
oid            | 10
-[ RECORD 4 ]--+--------------------------
rolname        | pg_write_server_files
rolsuper       | f
・・・

まとめ

 psqlのメタコマンド\duや、情報スキーマのenabled_rolesビューシステムカタログのpg_rolesビューを使うことでユーザを確認することができました。
 読んでくださり、ありがとうございました。

参考

PostgreSQLのマニュアル(psql)
https://www.postgresql.jp/document/11/html/app-psql.html
PostgreSQLのマニュアル(情報スキーマのenabled-roles)
https://www.postgresql.jp/document/11/html/infoschema-enabled-roles.html
PostgreSQLのマニュアル(システムカタログのpg_roles)
https://www.postgresql.jp/document/11/html/view-pg-roles.html

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?