暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

openGauss 每日一练第 2 天|学习 openGauss 客户端工具 gsql 的使用

原创 代野(Tank) 2022-11-29
806

本节课的学习目标是“学习 openGauss 数据库客户端工具 gsql 的使用”,似乎找回了初始 sqlplus 命令的感觉。

gsql

openGauss gsql 工具可参考其在线文档的描述,贴出部分信息如下:

gsql是openGauss提供在命令行下运行的数据库连接工具,可以通过此工具连接服务器并对其进行操作和维护,除了具备操作数据库的基本功能,gsql还提供了若干高级特性,便于用户使用。

根据概述内容描述,gsql 基本功能包括连接数据库、执行 SQL 语句和执行元命令,共 3 部分能力。

  • 连接数据库:详细操作请参见《开发者指南》中“数据库使用 > 连接数据库 > 使用gsql连接 > 远程连接数据库”章节。
  • 执行SQL语句:支持交互式地键入并执行SQL语句,也可以执行一个文件中指定的SQL语句。
  • 执行元命令:元命令可以帮助管理员查看数据库对象的信息、查询缓存区信息、格式化SQL输出结果、以及连接到新的数据库等。元命令的详细说明请参见元命令参考

高级特性内容较多,可参考官网具体描述信息。

作业

  1. gsql 命令连到数据库 omm
root@modb:~# su - omm -- -r,开启在客户端操作中可以进行编辑的模式 omm@modb:~$ gsql -r gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr ) Non-SSL connection (SSL connection is recommended when requiring high-security) Type "help" for help. omm=# \q omm@modb:~$ gsql -d omm -p 5432 -r gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr ) Non-SSL connection (SSL connection is recommended when requiring high-security) Type "help" for help. omm=#
  1. 查看数据库的版本、版权信息
omm=# select version(); version ------------------------------------------------------------------------------------------------------------------------------------------------------- (openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr on aarch64-unknown-linux-gnu, compiled by g++ (GCC) 7.3.0, 64-bit (1 row) omm=# show server_version; server_version ---------------- 9.2.4 (1 row) omm=# \copyright GaussDB Kernel Database Management System Copyright (c) Huawei Technologies Co., Ltd. 2018. All rights reserved.
  1. 常见元命令使用
-- \l,列出服务器上所有数据库的名称、所有者、字符集编码以及使用权限 omm=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+-------+----------+---------+-------+------------------- omm | omm | UTF8 | C | C | postgres | omm | UTF8 | C | C | template0 | omm | UTF8 | C | C | =c/omm + | | | | | omm=CTc/omm template1 | omm | UTF8 | C | C | =c/omm + | | | | | omm=CTc/omm (4 rows) -- \conninfo,输出当前连接的数据库的信息输出当前连接的数据库的信息 omm=# \conninfo You are connected to database "omm" as user "omm" via socket in "/tmp" at port "5432". -- \c,连接到一个新的数据库 omm=# \c postgres Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "postgres" as user "omm". openGauss=# -- \du,列出所有数据库角色 openGauss=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------------------------------------------------------------+----------- gaussdb | Sysadmin | {} omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {} -- \dg,列出所有数据库角色,等价于 \du openGauss=# \dg List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------------------------------------------------------------+----------- gaussdb | Sysadmin | {} omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {} -- \db,列出所有可用的表空间 openGauss=# \db List of tablespaces Name | Owner | Location ------------+-------+---------- pg_default | omm | pg_global | omm | (2 rows) -- \dn,列出所有的模式(名称空间) openGauss=# \dn List of schemas Name | Owner -----------------+--------- blockchain | omm cstore | omm db4ai | omm dbe_perf | omm dbe_pldebugger | omm dbe_pldeveloper | omm gaussdb | gaussdb pkg_service | omm public | omm snapshot | omm sqladvisor | omm (11 rows) openGauss=# openGauss=# CREATE TABLE customer_t openGauss-# ( c_customer_sk integer, openGauss(# c_customer_id char(5), openGauss(# c_first_name char(6), openGauss(# c_last_name char(8) openGauss(# ) ; openGauss=# CREATE TABLE openGauss=# INSERT INTO customer_t (c_customer_sk, c_customer_id, c_first_name,c_last_name) VALUES (3769, 5, 'Grace','White'); INSERT 0 1 openGauss=# openGauss=# SELECT * FROM customer_t; openGauss=# c_customer_sk | c_customer_id | c_first_name | c_last_name ---------------+---------------+--------------+------------- 3769 | 5 | Grace | White (1 row) -- \dt,查看所有的表 openGauss=# \dt List of relations Schema | Name | Type | Owner | Storage --------+------------+-------+-------+---------------------------------- public | customer_t | table | omm | {orientation=row,compression=no} (1 row) -- \d,查看指定表的信息 openGauss=# \d customer_t Table "public.customer_t" Column | Type | Modifiers ---------------+--------------+----------- c_customer_sk | integer | c_customer_id | character(5) | c_first_name | character(6) | c_last_name | character(8) | openGauss=# create index idx_customer_id on customer_t(c_customer_id); CREATE INDEX -- \di,查看指定表的索引信息 openGauss=# \di List of relations Schema | Name | Type | Owner | Table | Storage --------+-----------------+-------+-------+------------+--------- public | idx_customer_id | index | omm | customer_t | (1 row) -- \psert,设置影响查询结果表输出的选项 openGauss=# \pset border 2 Border style is 2. openGauss=# SELECT * FROM customer_t; +---------------+---------------+--------------+-------------+ | c_customer_sk | c_customer_id | c_first_name | c_last_name | +---------------+---------------+--------------+-------------+ | 3769 | 5 | Grace | White | +---------------+---------------+--------------+-------------+ (1 row) -- \x,打开扩展表格式模式 openGauss=# \x openGauss=# Expanded display is on. openGauss=# SELECT * FROM customer_t; +-[ RECORD 1 ]--+----------+ | c_customer_sk | 3769 | | c_customer_id | 5 | | c_first_name | Grace | | c_last_name | White | +---------------+----------+
  1. 使用两种方法,连到 postgres 数据库中
-- 方法 1:\c omm=# \c postgres Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "postgres" as user "omm". openGauss=# -- 方法 2:gsql root@modb:~# su - omm omm@modb:~$ gsql -d omm -p 5432 -r gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr ) Non-SSL connection (SSL connection is recommended when requiring high-security) Type "help" for help. omm=#
  1. 测试 gsql 中的默认事务自动提交功能
openGauss=# show AUTOCOMMIT; +------------+ | autocommit | +------------+ | on | +------------+ (1 row) openGauss=# create table customer_new as select * from customer_t; INSERT 0 1 openGauss=# openGauss=# \q omm@modb:~$ gsql -d postgres -p 5432 -r gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr ) Non-SSL connection (SSL connection is recommended when requiring high-security) Type "help" for help. openGauss=# openGauss=# \dt List of relations Schema | Name | Type | Owner | Storage --------+--------------+-------+-------+---------------------------------- public | customer_new | table | omm | {orientation=row,compression=no} public | customer_t | table | omm | {orientation=row,compression=no} (2 rows) openGauss=#
  1. 测试 gsql 中的事务手动提交功能
openGauss=# show AUTOCOMMIT; +------------+ | autocommit | +------------+ | on | +------------+ (1 row) openGauss=# create table customer_new as select * from customer_t; INSERT 0 1 openGauss=# openGauss=# \q omm@modb:~$ gsql -d postgres -p 5432 -r gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr ) Non-SSL connection (SSL connection is recommended when requiring high-security) Type "help" for help. openGauss=# openGauss=# \dt List of relations Schema | Name | Type | Owner | Storage --------+--------------+-------+-------+---------------------------------- public | customer_new | table | omm | {orientation=row,compression=no} public | customer_t | table | omm | {orientation=row,compression=no} (2 rows) openGauss=#
  1. 了解 gsql 相关帮助

通过以下 3 种方式来获取 gsql 相关帮助,输出内容较多,将不在这里展示。

-- 方法 1: $ gsql --help -- 方法 2: \h -- 方法 3: \?

总结

在前面练习时联想到了许久未用的 sqlplus,随着工作路线的变化,sqlplus 快成为了最熟悉的陌人。回到 gsql,gsql 支持的内容比较全,短时间也无法记住大部分的命令。照旧整理脑图供参考。

image.png
历史打卡记录:

openGauss 每日一练第 1 天|openGauss 数据库状态查看

最后修改时间:2022-11-29 23:54:59
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论