• openGauss gsql 常用元命令 一


    openGauss gsql 常用元命令 一

    连接数据库 使用 -E 参数可以显示元命令具体执行的 SQL 信息

    1. [omm@og1 ~]$ gsql -d postgres -p15400 -E
    2. gsql ((openGauss 2.1.0 build 590b0f8e) compiled at 2021-09-30 14:29:04 commit 0 last mr )
    3. Non-SSL connection (SSL connection is recommended when requiring high-security)
    4. Type "help" for help.

    \l 显示数据库中数据库信息

    1. openGauss=# \l
    2. ********* QUERY **********
    3. SELECT d.datname as "Name",
    4. pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
    5. pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
    6. d.datcollate as "Collate",
    7. d.datctype as "Ctype",
    8. pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges"
    9. FROM pg_catalog.pg_database d
    10. ORDER BY 1;
    11. **************************
    12. List of databases
    13. Name | Owner | Encoding | Collate | Ctype | Access privileges
    14. -----------+-------+----------+------------+------------+-------------------
    15. mydb | omm | UTF8 | en_US.utf8 | en_US.utf8 |
    16. postgres | omm | UTF8 | en_US.utf8 | en_US.utf8 |
    17. studentdb | omm | UTF8 | en_US.utf8 | en_US.utf8 |
    18. template0 | omm | UTF8 | en_US.utf8 | en_US.utf8 | =c/omm +
    19. | | | | | omm=CTc/omm
    20. template1 | omm | UTF8 | en_US.utf8 | en_US.utf8 | =c/omm +
    21. | | | | | omm=CTc/omm
    22. (5 rows)

    \du 同\dg 显示数据库中所有用户和角色

    1. openGauss=# \du
    2. ********* QUERY **********
    3. SELECT r.rolname, r.rolsuper, r.rolinherit,
    4. r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,
    5. r.rolconnlimit, r.rolvalidbegin, r.rolvaliduntil,
    6. ARRAY(SELECT b.rolname
    7. FROM pg_catalog.pg_auth_members m
    8. JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
    9. WHERE m.member = r.oid) as memberof
    10. , r.rolreplication
    11. , r.rolauditadmin
    12. , r.rolsystemadmin
    13. , r.rolmonitoradmin
    14. , r.roloperatoradmin
    15. , r.rolpolicyadmin
    16. , r.roluseft
    17. , r.rolkind
    18. FROM pg_catalog.pg_roles r
    19. WHERE r.rolname not in ('gs_role_copy_files', 'gs_role_signal_backend', 'gs_role_tablespace', 'gs_role_replication', 'gs_role_account_lock', 'gs_role_pldebugger')
    20. ORDER BY 1;
    21. **************************
    22. List of roles
    23. Role name | Attributes | Member of
    24. -----------+------------------------------------------------------------------------------------------------------------------+-----------
    25. omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {}
    26. student | Sysadmin | {}
    27. openGauss=# \dg
    28. ********* QUERY **********
    29. SELECT r.rolname, r.rolsuper, r.rolinherit,
    30. r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,
    31. r.rolconnlimit, r.rolvalidbegin, r.rolvaliduntil,
    32. ARRAY(SELECT b.rolname
    33. FROM pg_catalog.pg_auth_members m
    34. JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
    35. WHERE m.member = r.oid) as memberof
    36. , r.rolreplication
    37. , r.rolauditadmin
    38. , r.rolsystemadmin
    39. , r.rolmonitoradmin
    40. , r.roloperatoradmin
    41. , r.rolpolicyadmin
    42. , r.roluseft
    43. , r.rolkind
    44. FROM pg_catalog.pg_roles r
    45. WHERE r.rolname not in ('gs_role_copy_files', 'gs_role_signal_backend', 'gs_role_tablespace', 'gs_role_replication', 'gs_role_account_lock', 'gs_role_pldebugger')
    46. ORDER BY 1;
    47. **************************
    48. List of roles
    49. Role name | Attributes | Member of
    50. -----------+------------------------------------------------------------------------------------------------------------------+-----------
    51. omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {}
    52. student | Sysadmin | {}

    \db 显示数据库中所有表空间信息

    1. openGauss=# \db
    2. ********* QUERY **********
    3. SELECT spcname AS "Name",
    4. pg_catalog.pg_get_userbyid(spcowner) AS "Owner",
    5. pg_catalog.pg_tablespace_location(oid) AS "Location"
    6. FROM pg_catalog.pg_tablespace
    7. ORDER BY 1;
    8. **************************
    9. List of tablespaces
    10. Name | Owner | Location
    11. ------------+-------+------------------------
    12. pg_default | omm |
    13. pg_global | omm |
    14. student_ts | omm | tablespace/student_ts1
    15. (3 rows)

    \dn 显示数据库中所有 schema 信息

    1. openGauss=# \dn
    2. ********* QUERY **********
    3. SELECT n.nspname AS "Name",
    4. pg_catalog.pg_get_userbyid(n.nspowner) AS "Owner"
    5. FROM pg_catalog.pg_namespace n
    6. WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'
    7. ORDER BY 1;
    8. **************************
    9. List of schemas
    10. Name | Owner
    11. ----------------+---------
    12. blockchain | omm
    13. cstore | omm
    14. db4ai | omm
    15. dbe_perf | omm
    16. dbe_pldebugger | omm
    17. pkg_service | omm
    18. pmk | omm
    19. public | omm
    20. snapshot | omm
    21. sqladvisor | omm
    22. student | student
    23. (11 rows)

    \d 显示当前数据库下相关数据库对象信息(包含表、视图、物化视图、序列、外部表、stream\ contview)

    1. openGauss=# \d
    2. ********* QUERY **********
    3. SELECT n.nspname as "Schema",
    4. c.relname as "Name",
    5. CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'I' THEN 'global partition index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'm' THEN 'materialized view' WHEN 'e' THEN 'stream' WHEN 'o' THEN 'contview' END as "Type",
    6. pg_catalog.pg_get_userbyid(c.relowner) as "Owner",
    7. c.reloptions as "Storage"
    8. FROM pg_catalog.pg_class c
    9. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    10. WHERE c.relkind IN ('r','v','m','S','f','e','o','')
    11. AND n.nspname <> 'pg_catalog'
    12. AND n.nspname <> 'db4ai'
    13. AND n.nspname <> 'information_schema'
    14. AND n.nspname !~ '^pg_toast'
    15. AND c.relname not like 'matviewmap_%'
    16. AND c.relname not like 'mlog_%'
    17. AND pg_catalog.pg_table_is_visible(c.oid)
    18. ORDER BY 1,2;
    19. **************************
    20. List of relations
    21. Schema | Name | Type | Owner | Storage
    22. --------+--------+-------+-------+----------------------------------
    23. public | test | table | omm | {orientation=row,compression=no}
    24. public | v_test | view | omm |
    25. (2 rows)

    \d tablename 查看某个表的详细信息

    1. openGauss=# \d test
    2. ********* QUERY **********
    3. SELECT c.oid,
    4. n.nspname,
    5. c.relname
    6. FROM pg_catalog.pg_class c
    7. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    8. WHERE c.relname ~ '^(test)$'
    9. AND pg_catalog.pg_table_is_visible(c.oid)
    10. ORDER BY 2, 3;
    11. **************************
    12. ********* QUERY **********
    13. SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, c.relhastriggers, c.relhasoids, '', c.reltablespace, CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, c.relpersistence,c.relhasclusterkey, c.relreplident, (select count(1) as haspolicy from pg_catalog.pg_class WHERE relname = 'pg_rlspolicy')
    14. FROM pg_catalog.pg_class c
    15. LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)
    16. WHERE c.oid = '16575';
    17. **************************
    18. ********* QUERY **********
    19. SELECT * FROM pg_catalog.pg_class WHERE relname = 'gs_encrypted_columns' AND relnamespace = 11;
    20. **************************
    21. ********* QUERY **********
    22. SELECT a.attname,
    23. pg_catalog.format_type(a.atttypid, a.atttypmod),
    24. (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 176)
    25. FROM pg_catalog.pg_attrdef d
    26. WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),
    27. a.attnotnull, a.attnum,
    28. (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t
    29. WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation,
    30. NULL AS indexdef,
    31. NULL AS attfdwoptions,
    32. (SELECT pg_catalog.format_type (a.atttypmod, g.data_type_original_mod) AS clientlogic_original_type FROM gs_encrypted_columns g WHERE g.column_name = a.attname AND g.rel_id = 16575group by g.data_type_original_oid, g.data_type_original_mod),
    33. (SELECT g.data_type_original_oid AS clientlogic_original_type_oid FROM gs_encrypted_columns g WHERE g.column_name = a.attname AND g.rel_id = 16575group by g.data_type_original_oid, g.data_type_original_mod),
    34. (SELECT h.adgencol
    35. FROM pg_catalog.pg_attrdef h
    36. WHERE h.adrelid = a.attrelid AND h.adnum = a.attnum AND a.atthasdef) AS generated_column
    37. FROM pg_catalog.pg_attribute a
    38. WHERE a.attrelid = '16575' AND a.attnum > 0 AND NOT a.attisdropped AND a.attkvtype != 4 AND a.attname <> 'tableoid' AND a.attname <> 'tablebucketid'
    39. ORDER BY a.attnum;
    40. **************************
    41. ********* QUERY **********
    42. SELECT c2.relname, i.indisprimary, i.indisunique, i.indisclustered, i.indisvalid, pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),
    43. pg_catalog.pg_get_constraintdef(con.oid, true), contype, condeferrable, condeferred, i.indisreplident, c2.reltablespace, i.indisusable
    44. FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i
    45. LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))
    46. WHERE c.oid = '16575' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
    47. ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname;
    48. **************************
    49. ********* QUERY **********
    50. SELECT pol.policyname, pol.policypermissive, trim(pol.policyroles::text, '{}'), pol.policyqual, pol.policycmd
    51. FROM pg_catalog.pg_rlspolicies pol
    52. LEFT JOIN pg_catalog.pg_namespace N on (N.nspname = pol.schemaname)
    53. LEFT JOIN pg_catalog.pg_class C on (pol.tablename = C.relname and C.relnamespace = N.oid)
    54. WHERE C.oid = '16575' ORDER BY 1;
    55. **************************
    56. ********* QUERY **********
    57. SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhparent AND i.inhrelid = '16575' ORDER BY inhseqno;
    58. **************************
    59. ********* QUERY **********
    60. SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhrelid AND i.inhparent = '16575' ORDER BY c.oid::pg_catalog.regclass::pg_catalog.text;
    61. **************************
    62. ********* QUERY **********
    63. select partkey,partstrategy from pg_partition where parentid = 16575 order by partkey
    64. **************************
    65. Table "public.test"
    66. Column | Type | Modifiers
    67. --------+---------+-----------
    68. id | integer |
    69. Indexes:
    70. "idx_id_test" btree (id) TABLESPACE pg_default
    71. openGauss=#

    \dt 显示当前数据库中所有的表

    1. openGauss=# \dt
    2. ********* QUERY **********
    3. SELECT n.nspname as "Schema",
    4. c.relname as "Name",
    5. CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'I' THEN 'global partition index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'm' THEN 'materialized view' WHEN 'e' THEN 'stream' WHEN 'o' THEN 'contview' END as "Type",
    6. pg_catalog.pg_get_userbyid(c.relowner) as "Owner",
    7. c.reloptions as "Storage"
    8. FROM pg_catalog.pg_class c
    9. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    10. WHERE c.relkind IN ('r','')
    11. AND n.nspname <> 'pg_catalog'
    12. AND n.nspname <> 'db4ai'
    13. AND n.nspname <> 'information_schema'
    14. AND n.nspname !~ '^pg_toast'
    15. AND c.relname not like 'matviewmap_%'
    16. AND c.relname not like 'mlog_%'
    17. AND pg_catalog.pg_table_is_visible(c.oid)
    18. ORDER BY 1,2;
    19. **************************
    20. List of relations
    21. Schema | Name | Type | Owner | Storage
    22. --------+------+-------+-------+----------------------------------
    23. public | test | table | omm | {orientation=row,compression=no}
    24. (1 row)

    \dt+ 以扩展方式显示当前数据库所有表信息,比起\dt 多了最后一列描述信息

    1. openGauss=# \dt+
    2. ********* QUERY **********
    3. SELECT n.nspname as "Schema",
    4. c.relname as "Name",
    5. CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'I' THEN 'global partition index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'm' THEN 'materialized view' WHEN 'e' THEN 'stream' WHEN 'o' THEN 'contview' END as "Type",
    6. pg_catalog.pg_get_userbyid(c.relowner) as "Owner",
    7. pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as "Size",
    8. c.reloptions as "Storage",
    9. pg_catalog.obj_description(c.oid, 'pg_class') as "Description"
    10. FROM pg_catalog.pg_class c
    11. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    12. WHERE c.relkind IN ('r','')
    13. AND n.nspname <> 'pg_catalog'
    14. AND n.nspname <> 'db4ai'
    15. AND n.nspname <> 'information_schema'
    16. AND n.nspname !~ '^pg_toast'
    17. AND c.relname not like 'matviewmap_%'
    18. AND c.relname not like 'mlog_%'
    19. AND pg_catalog.pg_table_is_visible(c.oid)
    20. ORDER BY 1,2;
    21. **************************
    22. List of relations
    23. Schema | Name | Type | Owner | Size | Storage | Description
    24. --------+------+-------+-------+---------+----------------------------------+-------------
    25. public | test | table | omm | 0 bytes | {orientation=row,compression=no} |
    26. (1 row)

    \di 查看当前数据库中索引信息

    1. openGauss=# \di
    2. ********* QUERY **********
    3. SELECT n.nspname as "Schema",
    4. c.relname as "Name",
    5. CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'I' THEN 'global partition index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'm' THEN 'materialized view' WHEN 'e' THEN 'stream' WHEN 'o' THEN 'contview' END as "Type",
    6. pg_catalog.pg_get_userbyid(c.relowner) as "Owner",
    7. c2.relname as "Table",
    8. c.reloptions as "Storage"
    9. FROM pg_catalog.pg_class c
    10. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    11. LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
    12. LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid
    13. WHERE c.relkind IN ('i','I','')
    14. AND n.nspname <> 'pg_catalog'
    15. AND n.nspname <> 'db4ai'
    16. AND n.nspname <> 'information_schema'
    17. AND n.nspname !~ '^pg_toast'
    18. AND c.relname not like 'matviewmap_%'
    19. AND c.relname not like 'mlog_%'
    20. AND pg_catalog.pg_table_is_visible(c.oid)
    21. ORDER BY 1,2;
    22. **************************
    23. List of relations
    24. Schema | Name | Type | Owner | Table | Storage
    25. --------+-------------+-------+-------+-------+---------
    26. public | idx_id_test | index | omm | test |
    27. (1 row)

    \di indexname 查看当前数据库某个索引的信息

    1. openGauss=# \di idx_id_test
    2. ********* QUERY **********
    3. SELECT n.nspname as "Schema",
    4. c.relname as "Name",
    5. CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'I' THEN 'global partition index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'm' THEN 'materialized view' WHEN 'e' THEN 'stream' WHEN 'o' THEN 'contview' END as "Type",
    6. pg_catalog.pg_get_userbyid(c.relowner) as "Owner",
    7. c2.relname as "Table",
    8. c.reloptions as "Storage"
    9. FROM pg_catalog.pg_class c
    10. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    11. LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
    12. LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid
    13. WHERE c.relkind IN ('i','I','s','')
    14. AND n.nspname !~ '^pg_toast'
    15. AND c.relname not like 'matviewmap_%'
    16. AND c.relname not like 'mlog_%'
    17. AND c.relname ~ '^(idx_id_test)$'
    18. AND pg_catalog.pg_table_is_visible(c.oid)
    19. ORDER BY 1,2;
    20. **************************
    21. List of relations
    22. Schema | Name | Type | Owner | Table | Storage
    23. --------+-------------+-------+-------+-------+---------
    24. public | idx_id_test | index | omm | test |
    25. (1 row)

    \dv 查看当前数据库视图信息

    1. openGauss=# \dv
    2. ********* QUERY **********
    3. SELECT n.nspname as "Schema",
    4. c.relname as "Name",
    5. CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'I' THEN 'global partition index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'm' THEN 'materialized view' WHEN 'e' THEN 'stream' WHEN 'o' THEN 'contview' END as "Type",
    6. pg_catalog.pg_get_userbyid(c.relowner) as "Owner",
    7. c.reloptions as "Storage"
    8. FROM pg_catalog.pg_class c
    9. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    10. WHERE c.relkind IN ('v','')
    11. AND n.nspname <> 'pg_catalog'
    12. AND n.nspname <> 'db4ai'
    13. AND n.nspname <> 'information_schema'
    14. AND n.nspname !~ '^pg_toast'
    15. AND c.relname not like 'matviewmap_%'
    16. AND c.relname not like 'mlog_%'
    17. AND pg_catalog.pg_table_is_visible(c.oid)
    18. ORDER BY 1,2;
    19. **************************
    20. List of relations
    21. Schema | Name | Type | Owner | Storage
    22. --------+--------+------+-------+---------
    23. public | v_test | view | omm |
    24. (1 row)

    \ds 查看当前数据库序列信息

    1. openGauss=# \ds
    2. ********* QUERY **********
    3. SELECT n.nspname as "Schema",
    4. c.relname as "Name",
    5. CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'I' THEN 'global partition index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'm' THEN 'materialized view' WHEN 'e' THEN 'stream' WHEN 'o' THEN 'contview' END as "Type",
    6. pg_catalog.pg_get_userbyid(c.relowner) as "Owner",
    7. c.reloptions as "Storage"
    8. FROM pg_catalog.pg_class c
    9. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    10. WHERE c.relkind IN ('S','')
    11. AND n.nspname <> 'pg_catalog'
    12. AND n.nspname <> 'db4ai'
    13. AND n.nspname <> 'information_schema'
    14. AND n.nspname !~ '^pg_toast'
    15. AND c.relname not like 'matviewmap_%'
    16. AND c.relname not like 'mlog_%'
    17. AND pg_catalog.pg_table_is_visible(c.oid)
    18. ORDER BY 1,2;
    19. **************************
    20. List of relations
    21. Schema | Name | Type | Owner | Storage
    22. --------+------+----------+-------+---------
    23. public | sq1 | sequence | omm |
    24. (1 row)
  • 相关阅读:
    基于restful页面数据交互(前端页面访问处理)
    C++泛型编程——模板(初识)
    SpringCloud-Rest微服务工程的构建
    RabbitMQ入门
    226. 翻转二叉树
    使用docker-compose管理freeswitch容器
    php实战案例记录(7)可变变量$$str
    【MYSQL】约束
    web期末作业设计网页 HTML+CSS+JavaScript仿王者荣耀游戏新闻咨询(网页设计期末课程设计)
    Java 复习笔记 - 字符串篇
  • 原文地址:https://blog.csdn.net/renxyz/article/details/44044525