使用gsql连接openGauss
gsql是GaussDB自带的客户端工具。使用gsql连接数据库,可以交互式地输入、编辑、执行SQL语句。
格式:$gsql [-h ip] [-p port] [-U username] [-W password] -d databasename
选项:
-h,指定gsql连接的服务器ip
-p, 指定gsql连接的服务器端口号
-U,用户名,非数据库安装用户
-W, 用户密码
-d,gsql连接的数据库名
-f <FILE_PATH>: 执行指定的 SQL 文件
-c <sql_command> 用来指定 SQL 命令
1.操作系统用户(数据库安装管理员)本地登录
[ouser@openEuler2203 ~]$ gsql -d postgres -p 5432 #通过gsql登录数据库
gsql ((openGauss 5.0.1 build 33b035fd) compiled at 2023-12-15 19:51:49 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
openGauss=# \q --退出gsql
2.数据库普通用户远程登录
[ouser@openEuler2203 ~]$ gsql -h 192.168.116.147 -p 5432 -U student -W Mar152024@ -d postgres #通过gsql1以非数据库安装管理员远程登录数据库
gsql: could not connect to server: Operation now in progress
Is the server running on host "192.168.116.147" and accepting
TCP/IP connections on port 5432?
这是因为,未配置pg_hba.conf和postgresql.conf文件和未赋予student用户访问数据库权限。
(1)pg_hba.conf配置
添加
host all student 0.0.0.0/0 sha256
[ouser@openEuler2203 ~]$ vim /opt/software/openGauss/data/single_node/pg_hba.conf
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all student 0.0.0.0/0 sha257
(2)postgresql.conf配置
[ouser@openEuler2203 ~]$ vim /opt/software/openGauss/data/single_node/postgresql.conf
设置
isten_addresses = 'localhost,192.168.116.147'
local_bind_address = '192.168.116.147'
port = 5432
password_encryption_type = 2
(3)为数据库用户student赋予访问权限
[ouser@openEuler2203 ~]$ gsql -d postgres -p 5432 #通过gsql登录数据库,为数据库用户student赋予访问权限。
gsql ((openGauss 5.0.1 build 33b035fd) compiled at 2023-12-15 19:51:49 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
openGauss=# GRANT SELECT ON pg_catalog.pg_roles to student;
GRANT
openGauss=# GRANT SELECT ON pg_catalog.pg_user_status to student;
GRANT
openGauss=# GRANT ALL PRIVILEGES on TABLESPACE pg_default,pg_global to student;
GRANT
openGauss=# \q
(4)配置完成后,通过数据库普通用户student远程访问openGauss
[ouser@openEuler2203 ~]$ gsql -h 192.168.116.147 -p 5432 -U student -W Mar152024@ -d dbcourseselect #通过gsql以非数据库安装管理员登录数据库,连接到dbcourseselect。
gsql ((openGauss 5.0.1 build 33b035fd) compiled at 2023-12-15 19:51:49 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
- 点赞
- 收藏
- 关注作者
评论(0)