【云小课】【第46课】opengauss-yb体育官方
【摘要】 gsql是gaussdb(for opengauss)提供在命令行下运行的数据库连接工具,可以通过此工具连接服务器并对其进行操作和维护,除了具备操作数据库的基本功能,gsql还提供了若干高级特性,便于用户使用。基本功能连接数据库:可以通过gsql远程连接数据库实例。如何使用gsql连接数据库请参考连接实例。执行sql语句:支持交互式地键入并执行sql语句,也可以执行一个文件中指定的sql语句...
gsql是gaussdb(for opengauss)提供在命令行下运行的数据库连接工具,可以通过此工具连接服务器并对其进行操作和维护,除了具备操作数据库的基本功能,gsql还提供了若干高级特性,便于用户使用。
基本功能
- 连接数据库:可以通过gsql远程连接数据库实例。如何使用gsql连接数据库请参考。
- 执行sql语句:支持交互式地键入并执行sql语句,也可以执行一个文件中指定的sql语句。
- 执行元命令:元命令可以帮助管理员查看数据库对象的信息、查询缓存区信息、格式化sql输出结果,以及连接到新的数据库等。
使用指导
步骤 1 使用gsql连接到gaussdb(for opengauss)实例。
gsql工具使用-d参数指定目标数据库名、-u参数指定数据库用户名、-h参数指定主机名、-p参数指定端口号信息。
若未指定数据库名称,则使用初始化时默认生成的数据库名称;若未指定数据库用户名,则默认使用当前操作系统用户作为数据库用户名;当某个值没有前面的参数(-d、-u等)时,若连接的命令中没有指定数据库名(-d)则该参数会被解释成数据库名;如果已经指定数据库名(-d)而没有指定数据库用户名(-u)时,该参数则会被解释成数据库用户名。
示例,使用jack用户连接到远程主机postgres数据库的8000端口。
gsql -h 10.180.123.163 -d postgres -u jack -p 8000
详细的gsql参数请参见。
步骤 2 执行sql语句。
以创建数据库human_staff为例。
create database human_staff;
create database
通常,输入的命令行在遇到分号的时候结束。如果输入的命令行没有错误,结果就会输出到屏幕上。
步骤 3 执行gsql元命令。
以列出gaussdb(for opengauss)中所有的数据库和描述信息为例。
postgres=# \l
list of databases
name | owner | encoding | collate | ctype | access privileges
---------------- ---------- ----------- --------- ------- -----------------------
human_resource | root | sql_ascii | c | c |
postgres | root | sql_ascii | c | c |
template0 | root | sql_ascii | c | c | =c/root
| | | | | root=ctc/root
template1 | root | sql_ascii | c | c | =c/root
| | | | | root=ctc/root
human_staff | root | sql_ascii | c | c |
(5 rows)
更多gsql元命令请参见。
示例
以把一个查询分成多行输入为例。注意提示符的变化:
postgres=# create table hr.areas(
postgres(# area_id number,
postgres(# area_name varchar2(25)
postgres-# )tablespace example;
create table
查看表的定义:
postgres=# \d hr.areas
table "hr.areas"
column | type | modifiers
----------- ----------------------- -----------
area_id | numeric | not null
area_name | character varying(25) |
向hr.areas表插入四行数据:
postgres=# insert into hr.areas (area_id, area_name) values (1, 'europe');
insert 0 1
postgres=# insert into hr.areas (area_id, area_name) values (2, 'americas');
insert 0 1
postgres=# insert into hr.areas (area_id, area_name) values (3, 'asia');
insert 0 1
postgres=# insert into hr.areas (area_id, area_name) values (4, 'middle east and africa');
insert 0 1
切换提示符:
postgres=# \set prompt1 '%n@%m %~%r%#'
root@[local] postgres=#
查看表:
root@[local] postgres=#select * from hr.areas;
area_id | area_name
--------- ------------------------
1 | europe
4 | middle east and africa
2 | americas
3 | asia
(4 rows)
可以用\pset命令以不同的方法显示表:
root@[local] postgres=#\pset border 2
border style is 2.
root@[local] postgres=#select * from hr.areas;
--------- ------------------------
| area_id | area_name |
--------- ------------------------
| 1 | europe |
| 2 | americas |
| 3 | asia |
| 4 | middle east and africa |
--------- ------------------------
(4 rows)
root@[local] postgres=#\pset border 0
border style is 0.
root@[local] postgres=#select * from hr.areas;
area_id area_name
------- ----------------------
1 europe
2 americas
3 asia
4 middle east and africa
(4 rows)
使用元命令:
root@[local] postgres=#\a \t \x
output format is unaligned.
showing only tuples.
expanded display is on.
root@[local] postgres=#select * from hr.areas;
area_id|2
area_name|americas
area_id|1
area_name|europe
area_id|4
area_name|middle east and africa
area_id|3
area_name|asia
【亚博平台下载的版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区),文章链接,文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
- 点赞
- 收藏
- 关注作者
评论(0)