使用指导
前提条件
连接数据库时使用的用户需要具备访问数据库的权限。
背景信息
使用gsql命令可以连接远程数据库服务。连接远程数据库服务时,需要在服务器上设置允许远程连接,详细操作请参见远程连接数据库。
操作步骤
- 使用gsql连接到GaussDB 200服务器。
gsql工具使用-d参数指定目标数据库名、-U参数指定数据库用户名、-h参数指定主机名、-p参数指定端口号信息。
说明:
若未指定数据库名称,则使用初始化时默认生成的数据库名称;若未指定数据库用户名,则默认使用当前操作系统用户作为数据库用户名;当某个值没有前面的参数(-d、-U等)时,若连接的命令中没有指定数据库名(-d)则该参数会被解释成数据库名;如果已经指定数据库名(-d)而没有指定数据库用户名(-U)时,该参数则会被解释成数据库用户名。
示例1,使用omm用户连接到本机postgres数据库的25308端口。
gsql -d postgres -p 25308
示例2,使用jack用户连接到远程主机postgres数据库的25308端口。
gsql -h 10.180.123.163 -d postgres -U jack -p 25308
示例3,参数postgres和omm不属于任何选项时,分别被解释成了数据库名和用户名。
gsql postgres omm -p 25308
等效于
gsql -d postgres -U omm -p 25308
详细的gsql参数请参见命令参考。
- 执行SQL语句。
以创建数据库human_staff为例。
1 2
CREATE DATABASE human_staff; CREATE DATABASE
通常,输入的命令行在遇到分号的时候结束。如果输入的命令行没有错误,结果就会输出到屏幕上。
- 执行gsql元命令。
以列出GaussDB 200中所有的数据库和描述信息为例。
1 2 3 4 5 6 7 8 9 10 11 12
\l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges ----------------+----------+-----------+---------+-------+----------------------- human_resource | omm | SQL_ASCII | C | C | postgres | omm | SQL_ASCII | C | C | template0 | omm | SQL_ASCII | C | C | =c/omm + | | | | | omm=CTc/omm template1 | omm | SQL_ASCII | C | C | =c/omm + | | | | | omm=CTc/omm human_staff | omm | SQL_ASCII | C | C | (5 rows)
更多gsql元命令请参见元命令参考。
示例
以把一个查询分成多行输入为例。注意提示符的变化:
1 2 3 4 5
postgres=# CREATE TABLE HR.areaS( postgres(# area_ID NUMBER, postgres(# area_NAME VARCHAR2(25) postgres-# )tablespace EXAMPLE; CREATE TABLE
查看表的定义:
1 2 3 4 5 6
\d HR.areaS Table "hr.areas" Column | Type | Modifiers -----------+-----------------------+----------- area_id | numeric | not null area_name | character varying(25) |
向HR.areaS表插入四行数据:
1 2 3 4 5 6 7 8
INSERT INTO HR.areaS (area_ID, area_NAME) VALUES (1, 'Europe'); INSERT 0 1 INSERT INTO HR.areaS (area_ID, area_NAME) VALUES (2, 'Americas'); INSERT 0 1 INSERT INTO HR.areaS (area_ID, area_NAME) VALUES (3, 'Asia'); INSERT 0 1 INSERT INTO HR.areaS (area_ID, area_NAME) VALUES (4, 'Middle East and Africa'); INSERT 0 1
切换提示符:
1 2
\set PROMPT1 '%n@%m %~%R%#' omm@[local] postgres=#
查看表:
1 2 3 4 5 6 7 8
omm@[local] postgres=#SELECT * FROM HR.areaS; area_id | area_name ---------+------------------------ 1 | Europe 4 | Middle East and Africa 2 | Americas 3 | Asia (4 rows)
可以用\pset命令以不同的方法显示表:
1 2 3 4 5 6 7 8 9 10 11 12
omm@[local] postgres=#\pset border 2 Border style is 2. omm@[local] postgres=#SELECT * FROM HR.areaS; +---------+------------------------+ | area_id | area_name | +---------+------------------------+ | 1 | Europe | | 2 | Americas | | 3 | Asia | | 4 | Middle East and Africa | +---------+------------------------+ (4 rows)
1 2 3 4 5 6 7 8 9 10
omm@[local] postgres=#\pset border 0 Border style is 0. omm@[local] postgres=#SELECT * FROM HR.areaS; area_id area_name ------- ---------------------- 1 Europe 2 Americas 3 Asia 4 Middle East and Africa (4 rows)
使用元命令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
omm@[local] postgres=#\a \t \x Output format is unaligned. Showing only tuples. Expanded display is on. omm@[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 omm@[local] postgres=#
查看更多:华为GaussDB 200 gsql「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」关注作者【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。评论
- 执行SQL语句。