使用指导
前提条件
连接数据库时使用的用户需要具备访问数据库的权限。
背景信息
使用gsql命令可以连接本机的数据库服务,也可以连接远程数据库服务。连接远程数据库服务时,需要在服务器上设置允许远程连接,详细操作请参见《快速入门》中“访问openGauss > 使用gsql访问openGauss”章节。
操作步骤
- 使用gsql连接到openGauss服务器。
gsql工具使用-d参数指定目标数据库名、-U参数指定数据库用户名、-h参数指定主机名、-p参数指定端口号信息。
说明: 若未指定数据库名称,则使用初始化时默认生成的数据库名称;若未指定数据库用户名,则默认使用当前操作系统用户作为数据库用户名;当某个值没有前面的参数(-d、-U等)时,若连接的命令中没有指定数据库名(-d)则该参数会被解释成数据库名;如果已经指定数据库名(-d)而没有指定数据库用户名(-U)时,该参数则会被解释成数据库用户名。
示例1,使用omm用户连接到本机postgres数据库的8000端口。
1. gsql -d postgres -p 8000
示例2,使用jack用户连接到远程主机postgres数据库的8000端口。
1. gsql -h 10.180.123.163 -d postgres -U jack -p 8000
集中式集群中,连接主DataNode时可以把DataNode的IP地址使用逗号分割全部添加到-h后,gsql将依次从前往后连接每个IP地址,查询当前DataNode是否为主DataNode,如果不是则断开连接尝试下一个IP地址,直到找到主DataNode为止。
1. gsql -h 10.180.123.163,10.180.123.164,10.180.123,165 -d postgres -U jack -p 8000
示例3,参数postgres和omm不属于任何选项时,分别被解释成了数据库名和用户名。
1. gsql postgres omm -p 8000
等效于
1. gsql -d postgres -U omm -p 8000
详细的gsql参数请参见命令参考。 2. 执行SQL语句。
以创建数据库human_staff为例。
1. CREATE DATABASE human_staff;
2. CREATE DATABASE
通常,输入的命令行在遇到分号的时候结束。如果输入的命令行没有错误,结果就会输出到屏幕上。 3. 执行gsql元命令。
以列出openGauss中所有的数据库和描述信息为例。
1. openGauss=# \l
2. List of databases
3. Name | Owner | Encoding | Collate | Ctype | Access privileges
4. ----------------+----------+-----------+---------+-------+-----------------------
5. human_resource | omm | SQL_ASCII | C | C |
6. postgres | omm | SQL_ASCII | C | C |
7. template0 | omm | SQL_ASCII | C | C | =c/omm +
8. | | | | | omm=CTc/omm
9. template1 | omm | SQL_ASCII | C | C | =c/omm +
10. | | | | | omm=CTc/omm
11. human_staff | omm | SQL_ASCII | C | C |
12. (5 rows)
更多gsql元命令请参见元命令参考。
示例
以把一个查询分成多行输入为例。注意提示符的变化:
1. openGauss=# CREATE TABLE HR.areaS(
2. openGauss(# area_ID NUMBER,
3. openGauss(# area_NAME VARCHAR2(25)
4. openGauss-# )tablespace EXAMPLE;
5. CREATE TABLE
查看表的定义:
1. openGauss=# \d HR.areaS
2. Table "hr.areas"
3. Column | Type | Modifiers
4. -----------+-----------------------+-----------
5. area_id | numeric | not null
6. area_name | character varying(25) |
向HR.areaS表插入四行数据:
1. openGauss=# INSERT INTO HR.areaS (area_ID, area_NAME) VALUES (1, 'Europe');
2. INSERT 0 1
3. openGauss=# INSERT INTO HR.areaS (area_ID, area_NAME) VALUES (2, 'Americas');
4. INSERT 0 1
5. openGauss=# INSERT INTO HR.areaS (area_ID, area_NAME) VALUES (3, 'Asia');
6. INSERT 0 1
7. openGauss=# INSERT INTO HR.areaS (area_ID, area_NAME) VALUES (4, 'Middle East and Africa');
8. INSERT 0 1
切换提示符:
1. openGauss=# \set PROMPT1 '%n@%m %~%R%#'
2. omm@[local] openGauss=#
查看表:
1. omm@[local] openGauss=# SELECT * FROM HR.areaS;
2. area_id | area_name
3. ---------+------------------------
4. 1 | Europe
5. 4 | Middle East and Africa
6. 2 | Americas
7. 3 | Asia
8. (4 rows)
可以用\pset命令以不同的方法显示表:
1. omm@[local] openGauss=# \pset border 2
2. Border style is 2.
3. omm@[local] openGauss=# SELECT * FROM HR.areaS;
4. +---------+------------------------+
5. | area_id | area_name |
6. +---------+------------------------+
7. | 1 | Europe |
8. | 2 | Americas |
9. | 3 | Asia |
10. | 4 | Middle East and Africa |
11. +---------+------------------------+
12. (4 rows)
1. omm@[local] openGauss=# \pset border 0
2. Border style is 0.
3. omm@[local] openGauss=# SELECT * FROM HR.areaS;
4. area_id area_name
5. ------- ----------------------
6. 1 Europe
7. 2 Americas
8. 3 Asia
9. 4 Middle East and Africa
10. (4 rows)
使用元命令:
1. omm@[local] openGauss=# \a \t \x
2. Output format is unaligned.
3. Showing only tuples.
4. Expanded display is on.
5. omm@[local] openGauss=# SELECT * FROM HR.areaS;
6. area_id|2
7. area_name|Americas
9. area_id|1
10. area_name|Europe
12. area_id|4
13. area_name|Middle East and Africa
15. area_id|3
16. area_name|Asia
17. omm@[local] openGauss=#
说明: 若未指定数据库名称,则使用初始化时默认生成的数据库名称;若未指定数据库用户名,则默认使用当前操作系统用户作为数据库用户名;当某个值没有前面的参数(-d、-U等)时,若连接的命令中没有指定数据库名(-d)则该参数会被解释成数据库名;如果已经指定数据库名(-d)而没有指定数据库用户名(-U)时,该参数则会被解释成数据库用户名。