PostgreSql 数据库对象查询

这是我参与11月更文挑战的第1天,活动详情查看:2021最后一次更文挑战

下列查询需要定位到具体对象,加条件即可,表、视图、索引等对象需在对应数据库下执行。

查询所有数据库

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;

   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 chis      | postgres | UTF8     | en_US.utf8  | en_US.utf8  |
 chisapp   | postgres | UTF8     | en_US.utf8  | en_US.utf8  |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 syd       | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 test      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(7 rows)
复制代码

查询所有用户角色

SELECT rolname,
	CASE rolcanlogin WHEN 'true' THEN 'user' WHEN 	'false' THEN 'role' END AS TYPE
FROM pg_roles
WHERE rolname !~ '^pg_'
ORDER BY 1;

        rolname         | type
------------------------+------
 admin                  | user
 all_read               | role
 anay_app               | user
 anay_app_group         | role
 anay_owner             | user
 anay_read              | user
 anay_read_group        | role
 apply                  | user
 apply_app              | user
 apply_app_group        | role
 apply_owner            | user
 apply_read             | user
复制代码

查询数据库下所有 schema

SELECT n.nspname AS "Name",
  pg_catalog.pg_get_userbyid(n.nspowner) AS "Owner"
FROM pg_catalog.pg_namespace n
WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'
ORDER BY 1;

    Name     |       Owner
-------------+-------------------
 apply       | apply_owner
 bis         | bis_owner
 cams        | cams_owner
 chisbak     | postgres
 comm        | comm_owner
 cvms        | cvms_owner
 ecg         | ecg_owner
 emis        | emis_owner
 emr         | emr_owner
 event       | event_owner
 ext         | ext_owner
 finance     | finance_owner
………………
………………
………………
复制代码

查看数据库下所有 schema 的所有表

SELECT n.nspname as "Schema",
  c.relname as "Name",
  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' END as "Type",
  pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','p','')
      AND n.nspname <> 'pg_catalog'
      AND n.nspname <> 'information_schema'
      AND n.nspname !~ '^pg_toast'
ORDER BY 1,2;

  Schema  |                      Name                       |       Type        |     Owner
----------+-------------------------------------------------+-------------------+----------------
 apply    | apply_a                                         | table             | apply_owner
 apply    | apply_b                                         | table             | apply_owner
 apply    | apply_c                                         | table             | apply_owner
 bis      | bis_a                                           | table             | bis_owner
 bis      | bis_b                                           | table             | bis_owner
 bis      | bis_c                                           | table             | bis_owner
………………
………………
………………
复制代码

查看数据库下所有 schema 的所有视图

SELECT n.nspname as "Schema",
  c.relname as "Name",
  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' END as "Type",
  pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('v','m')
      AND n.nspname <> 'pg_catalog'
      AND n.nspname <> 'information_schema'
      AND n.nspname !~ '^pg_toast'
ORDER BY 1,2;

  Schema  |                      Name                      |       Type        |     Owner
----------+------------------------------------------------+-------------------+----------------
 hrms     | hr_view_a                                      | view              | hrms_owner
 hrms     | hr_view_b                                      | view              | hrms_owner
 lis      | lis_v_a                                        | view              | lis_owner
 lis      | lis_v_b                                        | view              | lis_owner
 lis      | lis_v_c                                        | view              | lis_owner
 mr       | v_mr_a                                         | view              | mr_owner
 mr       | v_mr_b                                         | view              | mr_owner
 mr       | v_mr_c                                         | view              | mr_owner
 v        | v_a                                            | view              | v_owner
 ………………
 ………………
 ………………
复制代码

查看数据库下所有索引

SELECT n.nspname as "Schema",
  c.relname as "Name",
  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' END as "Type",
  pg_catalog.pg_get_userbyid(c.relowner) as "Owner",
 c2.relname as "Table"
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
     LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
     LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid
WHERE c.relkind IN ('i','I','')
      AND n.nspname <> 'pg_catalog'
      AND n.nspname <> 'information_schema'
      AND n.nspname !~ '^pg_toast'
ORDER BY 1,2;

 Schema |                   Name                   |    Type    |     Owner      |               Table
--------+------------------------------------------+------------+----------------+-----------------------------------
 apply  | apply_pk1                                | index      | apply_owner    | apply_1
 apply  | apply_pk2                                | index      | apply_owner    | apply_2
 apply  | apply_pk3                                | index      | apply_owner    | apply_3
 bis    | apply_idx1                               | index      | bis_owner      | apply_a
 bis    | apply_idx2                               | index      | bis_owner      | apply_b
 bis    | apply_idx3                               | index      | bis_owner      | apply_c
………………
………………
………………
复制代码

Guess you like

Origin juejin.im/post/7031350645256880164