PostgreSQL | データベース | 作成済みデータベース一覧の確認
PostgreSQL で作成されたデータベースの一覧を確認する方法について説明する。
\l コマンドを使用する
まず psql のメタコマンドを使用する方法である。作成されたデータベース一覧を取得するには、次のように実行する。
postgres=# \l
데이터베이스 목록
이름 | 소유주 | 인코딩 | Collate | Ctype | 액세스 권한
-----------+----------+--------+------------------+------------------+-----------------------
devkuma | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949 |
devkuma2 | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949 |
postgres | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949 |
sample | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949 |
template0 | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949 | =c/postgres +
| | | | | postgres=CTc/postgres
(6개 행)
postgres=#
作成されたデータベース一覧を確認できる。
システムカタログ pg_database から確認する
続いて、PostgreSQL のシステムカタログの 1 つである pg_database から取得する方法について見てみる。システムカタログは、PostgreSQL の管理システムが使用するテーブルでデータベースやテーブルなどの情報を管理するもので、pg_database には次のような列がある。
| 名前 | 型 | 説明 |
|---|---|---|
| oid | oid | 識別子。明示的に指定しないと取得できない。 |
| datname | name | データベース名 |
| datdba | oid | データベース所有者 |
| encoding | int4 | データベース文字セットの識別番号 |
| datcollate | name | データベース文字列の照合順序 |
| datctype | name | データベース文字分類 |
| datistemplate | bool | すべてのユーザーが複製できるかどうか。false の場合は、スーパーユーザーまたはデータベース所有者のみ複製可能。 |
| datallowconn | bool | データベースに接続できるかどうか。template0 のデータ変更を防ぐために使用される。 |
| datconnlimit | int4 | 同時接続の最大数。-1 は無制限。 |
| datlastsysoid | oid | データベースの最終システム OID |
| datfrozenxid | xid | このデータベースでこの値より前のトランザクション ID は、永続的な凍結済みトランザクション ID を持つように変更されている。これは、このデータベースのトランザクション ID 周回(wraparound)問題を防ぎ、pg_xact を縮小するための VACUUM 作業を実行すべきかどうかを追跡するために使用される。 |
| datminmxid | xid | このデータベース内で、トランザクション ID に置き換えられる前のすべてのマルチトランザクション ID。これは、トランザクション ID 周回(wraparound)問題を防ぐか、pg_multixact を縮小するためにデータベースを VACUUM すべきかどうかを追跡するために使用される。 |
| dattablespace | oid | データベースのデフォルトテーブルスペース |
| datacl | aclitem [] | アクセス権限一覧 |
今回は pg_database から次のカラムのデータを取得してみる。
postgres=# select datname, datdba, encoding, datcollate, datctype from pg_database;
datname | datdba | encoding | datcollate | datctype
-----------+--------+----------+------------------+------------------
postgres | 10 | 6 | Korean_Korea.949 | Korean_Korea.949
template1 | 10 | 6 | Korean_Korea.949 | Korean_Korea.949
template0 | 10 | 6 | Korean_Korea.949 | Korean_Korea.949
sample | 10 | 6 | Korean_Korea.949 | Korean_Korea.949
devkuma | 10 | 6 | Korean_Korea.949 | Korean_Korea.949
devkuma2 | 10 | 6 | Korean_Korea.949 | Korean_Korea.949
(6개 행)
postgres=#
作成されたデータベース一覧が取得できたことがわかる。
ここで、データベースの所有者を表す datdba には、ロールに関するデータを管理するシステムカタログ pg_authid の oid 値が表示されている。そのため、実際のロール名を取得するには pg_authid の rolname を参照する必要がある。また、文字セットを表す encoding は、pg_encoding_to_char 関数を使用すると実際の文字セット名を取得できる。
たとえば、次のように実行する。
postgres=# select datname,
postgres-# pg_authid.rolname as dbrollname,
postgres-# pg_encoding_to_char(encoding) as dbencoding,
postgres-# datcollate,
postgres-# datctype
postgres-# from pg_database
postgres-# join pg_authid on pg_authid.oid = pg_database.datdba;
datname | dbrollname | dbencoding | datcollate | datctype
-----------+------------+------------+------------------+------------------
devkuma2 | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949
devkuma | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949
sample | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949
template0 | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949
template1 | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949
postgres | postgres | UTF8 | Korean_Korea.949 | Korean_Korea.949
(6개 행)
postgres=#
データベース所有者や文字セットを含めて、データベース一覧を確認できる。
–
作成されたデータベース一覧を取得する方法について説明した。