openGauss每日一练第5天 | openGauss中一个用户可以访问多个数据库

一、学习目标

学习openGauss体系结构,使用一个用户访问多个数据库。

二、课程学习

数据库用户user1可以访问不同的数据库(musicdb1、musicdb2、musicdb3)。

实验中user1用户在数据库musicdb1中创建了表t11、在数据库musicdb2创建了表t21、在数据库musicdb3中创建了表t31,验证了这个结论。

三、课程作业

1. 创建数据库musicdb10,创建用户user10,赋予sysadmin权限

su - omm
gsql -r
--进入数据库omm,创建表空间、测试数据库
drop DATABASE  IF EXISTS  musicdb10;
drop tablespace IF EXISTS music_tbs;
CREATE TABLESPACE music_tbs RELATIVE LOCATION 'tablespace/test_ts1';
CREATE DATABASE musicdb10  WITH TABLESPACE = music_tbs;

--执行下面的SQL语句,创建用户user10:
 CREATE USER user10 IDENTIFIED BY 'kunpeng@1234';
--授予user1数据库系统的SYSADMIN权限:
ALTER USER user10 SYSADMIN;
--查看数据库中目前有哪些用户和角色
\du

实训环境示例:

omm=# drop DATABASE  IF EXISTS  musicdb10;
NOTICE:  database "musicdb10" does not exist, skipping
DROP DATABASE
omm=# drop tablespace IF EXISTS music_tbs;
NOTICE:  Tablespace "music_tbs" does not exist, skipping.
DROP TABLESPACE
omm=# CREATE TABLESPACE music_tbs RELATIVE LOCATION 'tablespace/test_ts1';
CREATE TABLESPACE
omm=# CREATE DATABASE musicdb10  WITH TABLESPACE = music_tbs;
CREATE DATABASE
omm=# \db
omm=#            List of tablespaces
    Name    | Owner |      Location       
------------+-------+---------------------
 music_tbs  | omm   | tablespace/test_ts1
 pg_default | omm   | 
 pg_global  | omm   | 
(3 rows)

omm=# 
\l
                         List of databases
   Name    | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+-------+----------+---------+-------+-------------------
 musicdb10 | omm   | UTF8     | C       | C     | 
 omm       | omm   | UTF8     | C       | C     | 
 postgres  | omm   | UTF8     | C       | C     | 
 template0 | omm   | UTF8     | C       | C     | =c/omm           +
           |       |          |         |       | omm=CTc/omm
 template1 | omm   | UTF8     | C       | C     | =c/omm           +
           |       |          |         |       | omm=CTc/omm
(5 rows)

omm=# CREATE USER user10 IDENTIFIED BY 'kunpeng@1234';
NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
CREATE ROLE
omm=# ALTER USER user10 SYSADMIN;
ALTER ROLE
omm=# \du
                                                              List of roles
 Role name |                                                    Attributes                     
                               | Member of 
-----------+-----------------------------------------------------------------------------------
-------------------------------+-----------
 gaussdb   | Sysadmin                                                                          
                               | {
    
    }
 omm       | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Ope
ratoradmin, Policyadmin, UseFT | {
    
    }
 user1     | Sysadmin                                                                          
                               | {
    
    }
 user10    | Sysadmin                                                                          
                               | {
    
    }

omm=# 

2. 用户user10访问数据库postgres,创建一个表并插入数据

--用户user10登录到数据库postgres,创建表test1,并插入、查看数据

 \c postgres user10
 create table test1(id int primary key,col1 char(20));
 insert into test1 values(1,'Hello openGauss! 11');
 insert into test1 values(2,'Hello openGauss! 11');
 select * from test1; 
omm=# \c postgres user10
Password for user user10: 
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "postgres" as user "user10".
openGauss=> create table test1(id int primary key,col1 char(20));
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test1_pkey" for table "test1"
CREATE TABLE
openGauss=>  insert into test1 values(1,'Hello openGauss! 11');
INSERT 0 1
openGauss=>  insert into test1 values(2,'Hello openGauss! 11');
INSERT 0 1
openGauss=>  select * from test1; 
 id |         col1         
----+----------------------
  1 | Hello openGauss! 11 
  2 | Hello openGauss! 11 
(2 rows)

openGauss=> 

3. 用户user10访问数据库omm,创建一个表并插入数据

--用户user10登录到数据库omm,创建表test2,并插入、查看数据

 \c omm user10
 create table test2(id int primary key,col1 char(20));
 insert into test2 values(1,'Hello World! 11');
 insert into test2 values(2,'Hello World! 22');
 select * from test2; 
openGauss=> \c omm user10
Password for user user10: 
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "omm" as user "user10".
omm=>  create table test2(id int primary key,col1 char(20));
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test2_pkey" for table "test2"
CREATE TABLE
omm=>  insert into test2 values(1,'Hello World! 11');
INSERT 0 1
omm=>  insert into test2 values(2,'Hello World! 22');
INSERT 0 1
omm=>  select * from test2; 
 id |         col1         
----+----------------------
  1 | Hello World! 11     
  2 | Hello World! 22     
(2 rows)

omm=> 

4. 用户user10访问数据库musicdb10,创建一个表并插入数据

--用户user10登录到数据库musicdb10,创建表test3,并插入、查看数据

 \c musicdb10 user10
 create table test3(id int primary key,col1 char(20),col2 date);
 insert into test3 values(1,'Hello World! 11','2022-11-28');
 insert into test3 values(2,'Hello World! 22',sysdate);
 select * from test3 order by id; 
omm=# \c musicdb10 user10
Password for user user10: 
Non-SSL connection (SSL connection is recommended when requiring high-security)
You are now connected to database "musicdb10" as user "user10".
musicdb10=>  create table test3(id int primary key,col1 char(20),col2 date);
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test3_pkey" for table "test3"
musicdb10=> CREATE TABLE
 insert into test3 values(1,'Hello World! 11','2022-11-28');
INSERT 0 1
musicdb10=>  insert into test3 values(2,'Hello World! 22',sysdate);
INSERT 0 1
musicdb10=> select * from test3 order by id;
 id |         col1         |    col2    
----+----------------------+------------
  1 | Hello World! 11      | 2022-11-28
  2 | Hello World! 22      | 2022-11-28
(2 rows)

musicdb10=> 

猜你喜欢

转载自blog.csdn.net/qq_40220309/article/details/128088042