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

openGauss每日一练第2天 | gsql与元命令的使用

原创 2022-11-25
238

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

1.gsql命令连到数据库omm

进入环境

su - omm gsql -r

或者指名数据库与端口

gsql -d omm -p 5432 -r

看到提示符变为

omm=#
进入openGauss环境成功!!

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

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)
show server_version;
server_version
9.2.4
(1 row)
\copyright
GaussDB Kernel Database Management System
Copyright (c) Huawei Technologies Co., Ltd. 2018. All rights reserved.

3.常见元命令使用

基本命令功能说明

  • \l
    元命令\l的作用是显示openGauss数据库集簇中,目前有哪些数据库。

  • \conninfo
    元命令\conninfo的作用是在gsql中,显示会话的连接信息。

  • \c[onnect] <DBNAME>
    元命令\ c[onnect] <DBNAME>的作用是在gsql中,切换到连接的数据库<DBNAME>。

  • \du
    见下\dg

  • \dg
    元命令\dg命令与元命令\du命令的作用类似,都是显示openGauss数据库集簇中,目前有哪些用户和角色。

  • \db
    元命令\db的作用是显示openGauss数据库集簇中,目前有哪些表空间。

  • \dn
    元命令\dn的作用是显示当前数据库有哪些数据库模式。

  • \dt
    命令\dt的作用是显示数据库中所有的表。

  • \d <TableName>
    元命令\d <TableName>的作用是查看某个表的信息。

  • \di <IndexName>
    查看索引信息,元命令\di <IndexName>的作用是查看某个索引的信息。

  • \pset
    可以用\pset命令以不同的方法显示表:

  • \x
    \x打开扩展表格式模式。

命令实战

创建表customer_t:

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');

创建并查看索引

create index idx_customer_id on customer_t(c_customer_id); \di

修改格式并查看

\pset border 2 SELECT * FROM customer_t;

4.使用两种方法,连到postgres数据库中

一种是在bash中使用gsql时指明数据库postgres

gsql -d postgres -p 5432 -r

另一种是在数据库中用\c更换数据库为postgres

\c postgres
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "postgres" as user "omm".

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

查看gsql中事务是否默认为自动提交

show AUTOCOMMIT;
测试gsql中事务默认为自动提交功能 create table customer_new as select * from customer_t;
\q

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

关闭自动提交

\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’, ‘Bake’);

查看表中数据

select * from customer_t;

7.了解gsql相关帮助

在bash中查询gsql帮助信息:

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 output)
     -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:
     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
     -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 set field separator to zero byte
     -0, --record-separator-zero
     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:
     -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.

进入gsql交互界面后利用\?查询元命令信息:

\?
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
     \parallel [on [num]|off] toggle status of execute (currently off)
     \q quit gsql
    
    Query Buffer
     \e [FILE] [LINE] edit the query buffer (or file) with external editor
     \ef [FUNCNAME [LINE]] edit function definition with external editor
     \p show the contents of the query buffer
     \copy ... perform SQL COPY with data stream to the client host
     \echo [STRING] write string to standard output
     \i FILE execute commands from file
     \i+ FILE KEY execute commands from encrypted file
     \ir FILE as \i, but relative to location of current script
     \ir+ FILE KEY as \i+, but relative to location of current script
     \o [FILE] send all query results to file or |pipe
     \qecho [STRING] write string to query output stream (see \o)
     \r reset (clear) the query buffer
     \w FILE write query buffer to file

...

进入gsql交互界面后利用\h查询sql命令信息:

\h
Available help:
    ABORT CREATE DATA SOURCE DROP NODE GROUP
    ALTER APP WORKLOAD GROUP CREATE DATABASE DROP OPERATOR
    ALTER APP WORKLOAD GROUP MAPPING CREATE DIRECTORY DROP OWNED
    ALTER AUDIT POLICY CREATE EXTENSION DROP PACKAGE
    ALTER DATA SOURCE CREATE FOREIGN TABLE DROP PACKAGE BODY
    ALTER DIRECTORY CREATE INDEX DROP RESOURCE LABEL
    ALTER EXTENSION CREATE LANGUAGE DROP RESOURCE POOL
    ALTER DATABASE CREATE FUNCTION DROP PROCEDURE
    ALTER DEFAULT PRIVILEGES CREATE GROUP DROP PUBLICATION
    ALTER FOREIGN TABLE CREATE MASKING POLICY DROP ROLE
    ALTER FOREIGN TABLE FOR HDFS CREATE MATERIALIZED VIEW DROP ROW LEVEL SECURITY POLICY
    ALTER GROUP CREATE NODE GROUP DROP SERVER
...

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

评论