openGauss每日一练第2天 | 学习openGauss客户端工具gsql的使用
学习目标
学习openGauss数据库客户端工具gsql的使用。
课程学习
gsql是openGauss提供在命令行下运行的数据库连接工具,可以通过此工具连接服务器并对其进行操作和维护,除了具备操作数据库的基本功能,gsql还提供了若干高级特性,便于用户使用。
1.使用gsql命令连接数据库
2.在gsql中查看数据库的版本、pg基础版本和版权信息
3.常见元命令的使用
4.gsql中的事务:测试gsql中的默认事务自动提交功能
5.gsql中的事务:测试gsql中的事务手动提交功能
6.gsql相关的帮助
课程作业
1.gsql命令连到数据库omm
2.查看数据库的版本、版权信息
3.常见元命令使用
4.使用两种方法,连到postgres数据库中
5.测试gsql中的默认事务自动提交功能
6.测试gsql中的事务手动提交功能
7.了解gsql相关帮助
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")
-f, --file=FILENAME execute commands from file, then exit
-l, --list list available databases, then exit
-v, --set=, --variable=NAME=VALUE
set gsql variable NAME to VALUE
-V, --version output version information, then exit
-X, --no-gsqlrc do not read startup file (~/.gsqlrc)
-1 ("one"), --single-transaction
execute command file as a single transaction
-?, --help show this help, then exit
Input and output options:
-a, --echo-all echo all input from script
-e, --echo-queries echo commands sent to server
-E, --echo-hidden display queries that internal commands generate
-k, --with-key=KEY the key for decrypting the encrypted file
-L, --log-file=FILENAME send session log to file
-m, --maintenance can connect to cluster during 2-pc transaction recovery
-n, --no-libedit disable enhanced command line editing (libedit)
-o, --output=FILENAME send query results to file (or |pipe)
-q, --quiet run quietly (no messages, only query outp set record separator to zero byte
-2, --pipeline use pipeline to pass the password, forbidden to use in terminal
must use with -c or -f
Connection options:
ut)
-C, --enable-client-encryption enable client encryption feature
-s, --single-step single-step mode (confirm each query)
-S, --single-line single-line mode (end of line terminates SQL command)
Output format options:
-A, --no-align unaligned table output mode
-F, --field-separator=STRING
set field separator (default: "|")
-H, --html HTML table output mode
-P, --pset=VAR[=ARG] set printing option VAR to ARG (see \pset command)
-R, --record-separator=STRING
set record separator (default: newline)
-r if this parameter is set,use libedit
-t, --tuples-only print rows only
-T, --table-attr=TEXT set HTML table tag attributes (e.g., width, border)
-x, --expanded turn on expanded table output
-z, --field-separator-zero
set field separator to zero byte
-0, --record-separator-zero
-h, --host=HOSTNAME database server host or socket directory (default: "local socket")
allow multi host IP address with comma separator in centralized cluster
-p, --port=PORT database server port (default: "5432")
-U, --username=USERNAME database user name (default: "omm")
-W, --password=PASSWORD the password of specified database user
For more information, type "\?" (for internal commands) or "\help" (for SQL
commands) from within gsql, or consult the gsql section in the openGauss documentation.
openGauss每日一练第2天 | 学习openGauss客户端工具gsql的使用
学习目标
学习openGauss数据库客户端工具gsql的使用。
课程学习
gsql是openGauss提供在命令行下运行的数据库连接工具,可以通过此工具连接服务器并对其进行操作和维护,除了具备操作数据库的基本功能,gsql还提供了若干高级特性,便于用户使用。
1.使用gsql命令连接数据库
2.在gsql中查看数据库的版本、pg基础版本和版权信息
3.常见元命令的使用
4.gsql中的事务:测试gsql中的默认事务自动提交功能
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相关的帮助
课程作业
1.gsql命令连到数据库omm
2.查看数据库的版本、版权信息
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.3.常见元命令使用
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=# \conninfoomm=# You are connected to database "omm" as user "omm" via socket in "/tmp" at port "5432". omm=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------------------------------------------------------------+----------- gaussdb | Sysadmin | {} omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {} omm=# \dg List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------------------------------------------------------------+----------- gaussdb | Sysadmin | {} omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT | {} omm-# \db pg_default | omm | pg_global | omm | (2 rows)4.使用两种方法,连到postgres数据库中
5.测试gsql中的默认事务自动提交功能
omm=# show AUTOCOMMIT; autocommit ------------ on (1 row) omm=# create table customer_new as select * from customer_t; INSERT 0 1 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 ) omm=# Non-SSL connection (SSL connection is recommended when requiring high-security) Type "help" for help. omm=# select * from customer_new; c_customer_sk | c_customer_id | c_first_name | c_last_name ---------------+---------------+--------------+------------- 3769 | 5 | Grace | White (1 row) omm=# \dt (2 rows) omm=# 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}6.测试gsql中的事务手动提交功能
omm=# \set AUTOCOMMIT off omm=# \echo :AUTOCOMMIT off omm=# INSERT INTO customer_t (c_customer_sk, c_customer_id, c_first_name,c_last_name) VALUES omm-# (6885, 1, 'Joes', 'Hunter'), omm-# (4321, 2, 'Lily','Carter'), omm-# (9527, 3, 'James', 'Cook'), omm-# (9500, 4, 'Lucy', 'Baker'); omm=# INSERT 0 4 omm=# 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) omm=# ROLLBACK; ROLLBACK omm=# SELECT * FROM customer_t; (1 row) omm=# c_customer_sk | c_customer_id | c_first_name | c_last_name ---------------+---------------+--------------+------------- 3769 | 5 | Grace | White7.了解gsql相关帮助
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") -f, --file=FILENAME execute commands from file, then exit -l, --list list available databases, then exit -v, --set=, --variable=NAME=VALUE set gsql variable NAME to VALUE -V, --version output version information, then exit -X, --no-gsqlrc do not read startup file (~/.gsqlrc) -1 ("one"), --single-transaction execute command file as a single transaction -?, --help show this help, then exit Input and output options: -a, --echo-all echo all input from script -e, --echo-queries echo commands sent to server -E, --echo-hidden display queries that internal commands generate -k, --with-key=KEY the key for decrypting the encrypted file -L, --log-file=FILENAME send session log to file -m, --maintenance can connect to cluster during 2-pc transaction recovery -n, --no-libedit disable enhanced command line editing (libedit) -o, --output=FILENAME send query results to file (or |pipe) -q, --quiet run quietly (no messages, only query outp set record separator to zero byte -2, --pipeline use pipeline to pass the password, forbidden to use in terminal must use with -c or -f Connection options: ut) -C, --enable-client-encryption enable client encryption feature -s, --single-step single-step mode (confirm each query) -S, --single-line single-line mode (end of line terminates SQL command) Output format options: -A, --no-align unaligned table output mode -F, --field-separator=STRING set field separator (default: "|") -H, --html HTML table output mode -P, --pset=VAR[=ARG] set printing option VAR to ARG (see \pset command) -R, --record-separator=STRING set record separator (default: newline) -r if this parameter is set,use libedit -t, --tuples-only print rows only -T, --table-attr=TEXT set HTML table tag attributes (e.g., width, border) -x, --expanded turn on expanded table output -z, --field-separator-zero set field separator to zero byte -0, --record-separator-zero -h, --host=HOSTNAME database server host or socket directory (default: "local socket") allow multi host IP address with comma separator in centralized cluster -p, --port=PORT database server port (default: "5432") -U, --username=USERNAME database user name (default: "omm") -W, --password=PASSWORD the password of specified database user For more information, type "\?" (for internal commands) or "\help" (for SQL commands) from within gsql, or consult the gsql section in the openGauss documentation.