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

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

原创 手机用户0512 2022-12-12
613

学习目标

学习openGauss数据库客户端工具gsql的使用。

课程学习

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

1.使用gsql命令连接数据库

--使用omm用户连接到本机omm数据库的5432端口,命令中的-r选项提供了对gsql命令的历史版本支持。 su - omm gsql -r --或 gsql -d omm -p 5432 -r

2.在gsql中查看数据库的版本、pg基础版本和版权信息

--数据库的版本、pg基础版本: select version(); show server_version; --版权信息: \copyright

3.常见元命令的使用

--元命令\l显示openGauss数据库集簇中现有数据库。 \l --元命令\conninfo显示会话的连接信息。 \conninfo --元命令\ c[onnect] [DBNAME]连接/切换连接的数据库如:postgres。 \c postgres --元命令\dg与\du命令都是显示openGauss数据库集簇中用户和角色信息。 \du \dg --元命令\db显示openGauss数据库集簇中表空间信息。 \db --元命令\dn显示当前数据库现有数据库模式。 \dn 表相关元命令,先创建表演示: --创建表 CREATE TABLE customer_t ( c_customer_sk integer, c_customer_id char(5), c_first_name char(6), c_last_name char(8) ) ; --插入数据 INSERT INTO customer_t (c_customer_sk, c_customer_id, c_first_name,c_last_name) VALUES (3769, 5, 'Grace','White'); --元命令\dt的作用是显示数据库中所有的表。 \dt --元命令\d TableName的作用是查看某个表的信息。 \d customer_t --元命令\di IndexName的作用是查看某个索引的信息。 --创建一个索引 create index idx_customer_id on customer_t(c_customer_id); \di --命令\pset以不同的方法显示表(是否有边框): \pset border 2 SELECT * FROM customer_t; --打开扩展表格式模式。 \x

4.gsql中的事务:测试gsql中的默认事务自动提交功能

--查看gsql中事务是否默认为自动提交 show AUTOCOMMIT; --测试gsql中事务默认为自动提交功能 create table customer_new as select * from customer_t; \q --重新登录后看到之前创建的表customer_new: gsql -d postgres -p 5432 -r \dt

5.gsql中的事务:测试gsql中的事务手动提交功能

--测试gsql手动提交 #Opengauss默认执行完一条语句后,立即提交。可以关闭自动提交功能: #注意:此处设置ATUOCOMMIT必须用大写! \set AUTOCOMMIT off --插入一些数据 INSERT INTO customer_t (c_customer_sk, c_customer_id, c_first_name,c_last_name) VALUES (6885, 1, ‘Joes’, ‘Hunter’), (4321, 2, ‘Lily’,‘Carter’), (9527, 3, ‘James’, ‘Cook’), (9500, 4, ‘Lucy’, ‘Baker’); --查看表中数据 select * from customer_t; --执行回滚 ROLLBACK; --检查是否回滚成功 SELECT * FROM customer_t;

6.gsql相关的帮助

–连接数据库时,可以使用如下命令获取帮助信息。 gsql --help –\h获取和SQL语法有关的帮助信息 \h –\? 获取和元命令有关的帮助信息 \?

课程作业

1.gsql命令连到数据库omm

先查看gsql帮助了解基础使用:

omm@modb:~$ gsql --help gsql is the openGauss interactive terminal. Usage: gsql [OPTION]... [DBNAME [USERNAME]] General options: -d, --dbname=DBNAME database name to connect to (default: "omm") ...... -n, --no-libedit disable enhanced command line editing (libedit) ...... -r if this parameter is set,use libedit ...... -p, --port=PORT database server port (default: "5432") ......

然后实际连接:

gsql -r #或 指明连接数据库具体情况 #gsql -d 数据库名 -p 端口 -r gsql -d omm -p 5432 -r
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=# \q 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=#

2.查看数据库的版本、版权信息

数据库的版本查看: select version(); show server\_version; 版权信息查看: \copyright
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=# \copyrightGaussDB Kernel Database Management System Copyright (c) Huawei Technologies Co., Ltd. 2018. All rights reserved. omm=#

3.常见元命令使用

--元命令\l显示openGauss数据库集簇中现有数据库。 omm=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+-------+----------+---------+-------+------------------- omm | omm | UTF8 | C | C | | | | | | omm=CTc/omm (4 rows) postgres | omm | UTF8 | C | C | template0 | omm | UTF8 | C | C | =c/omm + | | | | | omm=CTc/omm template1 | omm | UTF8 | C | C | =c/omm + --元命令\conninfo显示会话的连接信息。 omm=# \conninfo You are connected to database "omm" as user "omm" via socket in "/tmp" at port "5432". --元命令\c [onnect] [DBNAME]连接/切换连接的数据库如:\c postgres。 omm=# \c postgres Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "postgres" as user "omm". --元命令\dg与\du命令都是显示openGauss数据库集簇中用户和角色信息。 openGauss=# \dg List of roles Role name | Attributes | Member of -----------+----------------------------------------------------------------------------------------- -------------------------+----------- gaussdb | Sysadmin | {} omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatora dmin, Policyadmin, UseFT | {} openGauss=# \du List of roles Role name | Attributes | Member of -----------+----------------------------------------------------------------------------------------- -------------------------+----------- gaussdb | Sysadmin | {} omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatora dmin, Policyadmin, UseFT | {} --元命令\db显示openGauss数据库集簇中表空间信息。 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=# 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(# ) ; 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 --元命令\dt的作用是显示数据库中所有的表。 openGauss=# \dt List of relations Schema | Name | Type | Owner | Storage --------+------------+-------+-------+---------------------------------- public | customer_t | table | omm | {orientation=row,compression=no} (1 row) --元命令\d TableName的作用是查看某个表的信息。 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) | --元命令\di IndexName的作用是查看某个索引的信息。 --我们先创建一个索引再查看: openGauss=# create index idx_customer_id on customer_t(c_customer_id); CREATE INDEX openGauss=# \di List of relations Schema | Name | Type | Owner | Table | Storage --------+-----------------+-------+-------+------------+--------- public | idx_customer_id | index | omm | customer_t | (1 row) --命令\pset以不同的方法显示表(是否有边框): 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) --打开扩展表格式模式。 openGauss=# \x Expanded display is on. openGauss=# List of relations +-[ RECORD 1 ]--------------+ | Schema | public | | Name | idx_customer_id | | Type | index | | Owner | omm | | Table | customer_t | openGauss=# SELECT * FROM customer_t; +-[ RECORD 1 ]--+----------+ | c_customer_sk | 3769 | | c_customer_id | 5 | | c_first_name | Grace | | c_last_name | White | +---------------+----------+

4.使用两种方法,连到postgres数据库中
连接数据库可以先进gsql终端在使用元命令\c连接postgres

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) omm=# \c postgres openGauss=# Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "postgres" as user "omm". openGauss=#

或直接gsql指定连接的数据库

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=# \conninfo You are connected to database "postgres" as user "omm" via socket in "/tmp" at port "5432".

5.测试gsql中的默认事务自动提交功能

--查看gsql中事务是否默认为自动提交 openGauss=# show AUTOCOMMIT; autocommit ------------ on (1 row) --测试gsql中事务默认为自动提交功能 openGauss=# create table customer_new as select * from customer_t; INSERT 0 1 openGauss=# \q --重新登录后看到之前创建的表customer_new: 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=# \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)

6.测试gsql中的事务手动提交功能

--测试gsql手动提交
--Opengauss默认执行完一条语句后,立即提交。可以关闭自动提交功能:\set AUTOCOMMIT off
--将会发现没有手动提交的任务没有自动保存, 即需要手动处理。

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=# create  table customer_new2 as select * from customer_t;
INSERT 0 1
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=# \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=# 
--测试回滚操作 openGauss=# \set AUTOCOMMIT off --先为表customer_new插入一些数据 openGauss=# INSERT INTO customer_t (c_customer_sk, c_customer_id, c_first_name,c_last_name) VALUES openGauss-# (6885, 1, 'Joes', 'Hunter'), (4321, 2, 'Lily','Carter'), openGauss-# openGauss-# (9527, 3, 'James', 'Cook'), openGauss-# (9500, 4, 'Lucy', 'Baker'); INSERT 0 4 openGauss=# select * from customer_t; c_customer_sk | c_customer_id | c_first_name | c_last_name ---------------+---------------+--------------+------------- 3769 | 5 | Grace | White 6885 | 1 | Joes | Hunter 4321 | 2 | Lily | Carter 9527 | 3 | James | Cook 9500 | 4 | Lucy | Baker (5 rows) --查看表中数据 openGauss=# select * from customer_t; c_customer_sk | c_customer_id | c_first_name | c_last_name ---------------+---------------+--------------+------------- 3769 | 5 | Grace | White 6885 | 1 | Joes | Hunter 4321 | 2 | Lily | Carter 9527 | 3 | James | Cook 9500 | 4 | Lucy | Baker (5 rows) --执行回滚 openGauss=# ROLLBACK; --检查是否回滚成功 openGauss=# select * from customer_t; openGauss=# c_customer_sk | c_customer_id | c_first_name | c_last_name ---------------+---------------+--------------+------------- 3769 | 5 | Grace | White (1 row)

7.了解gsql相关帮助
系统命令行下
gsql --help 获取帮助:

omm@modb:~$ gsql --help gsql is the openGauss interactive terminal. Usage: gsql [OPTION]... [DBNAME [USERNAME]] General options: -c, --command=COMMAND run only single command (SQL or internal) and exit -d, --dbname=DBNAME database name to connect to (default: "omm") ......

进入psql终端后:

--\h获取和SQL语法有关的帮助信息 omm=# \h Available help: ABORT CREATE TABLE SUBPARTITION ALTER APP WORKLOAD GROUP CREATE TABLESPACE ALTER APP WORKLOAD GROUP MAPPING CREATE TEXT SEARCH CONFIGURATION ALTER AUDIT POLICY CREATE TEXT SEARCH DICTIONARY ...... --More-- --\? 获取和元命令有关的帮助信息 omm=# \? General \copyright show openGauss usage and distribution terms \g [FILE] or ; execute query (and send results to file or |pipe) \h(\help) [NAME] help on syntax of SQL commands, * for all commands ...... --More--
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论