pg_catalog

PostgreSQL DB一覧(データベース一覧)

データベース一覧

データベース一覧出力SQL
select d.datname,u.usename as owner,pg_encoding_to_char(d.encoding),s.spcname
,pg_tablespace_location(d.dattablespace) as "Location"
,pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges"
from pg_database d inner join pg_user u on d.datdba = u.usesysid
left outer join pg_tablespace s on d.dattablespace = s.oid
order by d.oid;
確認例

spcname:データベースのデフォルト表領域名(pg_defaultはシステムセットアップ時のデフォルト)
Location:表領域がマッピングされているフォルダ(pg_defaultの場合PGDATA(補1))
(補1)show data_directory で確認可能。

  datname  |  owner   | pg_encoding_to_char |  spcname   |          Location           |   Access privileges
-----------+----------+---------------------+------------+-----------------------------+-----------------------
 template1 | postgres | UTF8                | pg_default |                             | =c/postgres          +
           |          |                     |            |                             | postgres=CTc/postgres
 template0 | postgres | UTF8                | pg_default |                             | =c/postgres          +
           |          |                     |            |                             | postgres=CTc/postgres
 postgres  | postgres | UTF8                | pg_default |                             |
 myposdb   | sooni    | UTF8                | pg_default |                             | =Tc/sooni            +
           |          |                     |            |                             | sooni=CTc/sooni
 prdb      | sooni    | UTF8                | tbspc_01   | /var/lib/pgsql/pgdata/spc01 |
(5 rows)


postgres=# show data_directory;
     data_directory
------------------------
 /var/lib/pgsql/13/data
(1 row)


postgres=#
メタコマンド版
\l
メタコマンド実行例
myposdb=# \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
-----------+----------+----------+---------+-------+-----------------------
 myposdb   | sooni    | UTF8     | C       | C     | =Tc/sooni            +
           |          |          |         |       | sooni=CTc/sooni
 postgres  | postgres | UTF8     | C       | C     |
 prdb      | sooni    | UTF8     | C       | C     |
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
(5 rows)


myposdb=#
内部的に発行されるSQL
SELECT d.datname as "Name",
       pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
       pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
       d.datcollate as "Collate",
       d.datctype as "Ctype",
       pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges"
        FROM pg_catalog.pg_database d
        ORDER BY 1;

データベースへアクセスできるユーザ

has_database_privilege ()を使いconnect権限を保持しているユーザを一覧します

select usename as username, datname as database_name
from pg_database
cross join pg_user
where has_database_privilege (usename, datname, 'connect')
and datname not in ('template0','template1', 'postgres')
and usename not in ('postgres','rdsadmin', 'rdsproxyadmin')
order by 2, 1

CONNECT: データベースに接続する権限。
CREATE: データベースオブジェクト(テーブル、ビュー、インデックスなど)を作成する権限。
TEMPORARY/TEMP: 一時テーブルを作成する権限。

データベースへのアクセス権限を付与

grant connect on database myposdb to udonman;

データベースの作成

各種オブジェクト一覧

スポンサーリンク
タイトルとURLをコピーしました