1. 程式人生 > >Greenplum中定義數據庫對象之創建與管理模式

Greenplum中定義數據庫對象之創建與管理模式

定義數據 多個 sql master help valid border man -m

創建與管理模式

  • 概述:DB內組織對象的一種邏輯結構。一個DB內能夠有多個模式。在未指定模式時默認放置在public中。能夠通過”\dn”方式查看數據庫中現有模式。

testdw=# \dn

List of schemas

Name | Owner

--------------------+---------

gp_toolkit | gpadmin

information_schema | gpadmin

pg_aoseg | gpadmin

pg_bitmapindex | gpadmin

pg_catalog | gpadmin

pg_toast | gpadmin

public | gpadmin

(7 rows)

  • 創建模式:使用CREATESCHEMA命令。通過查看幫助例如以下所看到的:

testdw=# \h CREATE SCHEMA

Command: CREATE SCHEMA

Description: define a new schema

Syntax:

CREATE SCHEMA schemaname [ AUTHORIZATION username ] [ schema_element [ ... ] ]

將全部者設置為其它角色通過AUTHORIZTION

CREATE SCHEMA AUTHORIZATION username [ schema_element [ ... ] ]

  • 訪問模式的對象:schema.table

testdw=# CREATE SCHEMA sc01;

CREATE SCHEMA

testdw=# \dn

List of schemas

Name | Owner

--------------------+---------

gp_toolkit | gpadmin

information_schema | gpadmin

pg_aoseg | gpadmin

pg_bitmapindex | gpadmin

pg_catalog | gpadmin

pg_toast | gpadmin

public | gpadmin

sc01 | gpadmin

(8 rows)

testdw=# create schema sc02 authorization mavshuang;

ERROR: permission denied for database testdw (seg1 slave2:40000 pid=5424) 提示testdw數據庫中權限拒絕

testdw=# grant all on database testdw to mavshuang; testdw數據庫的全部權限賦給mavshuang

GRANT

testdw=# create schema sc02 authorization mavshuang;

CREATE SCHEMA

testdw=# \dn

List of schemas

Name | Owner

--------------------+-----------

gp_toolkit | gpadmin

information_schema | gpadmin

pg_aoseg | gpadmin

pg_bitmapindex | gpadmin

pg_catalog | gpadmin

pg_toast | gpadmin

public | gpadmin

sc01 | gpadmin

sc02 | mavshuang 此時用戶是mavshuang

(9 rows)

  • 模式搜索路徑:若不想通過指定模式名稱的方式來搜索須要的對象。能夠通過設置search_path的方式來實現。第一個模式為缺省。

testdw=# show search_path;

search_path

----------------

"$user",public

(1 row)

  • 通過ALTERDATABASE改動DB的模式搜索路徑

testdw-# \h alter database

Command: ALTER DATABASE

Description: change a database

Syntax:

ALTER DATABASE name [ [ WITH ] option [ ... ] ]

where option can be:

CONNECTION LIMIT connlimit

ALTER DATABASE name SET parameter { TO | = } { value | DEFAULT } 通過此命令來改動DB的模式搜索路徑

ALTER DATABASE name RESET parameter

ALTER DATABASE name RENAME TO newname

ALTER DATABASE name OWNER TO new_owner

testdw=# alter database testdw set search_path to sc01,public,pg_catalog; 設置testdw數據庫的搜索路徑為sc01,public,pg_catalog;

ALTER DATABASE

testdw=# \q 改動完畢後通過\q退出testdw數據庫後又一次登錄

[[email protected] ~]$ psql -d testdw

psql (8.2.15)

Type "help" for help.

testdw=# show search_path;

search_path

--------------------------

sc01, public, pg_catalog

(1 row)

  • 通過ALTER ROLE改動ROLE(User)的模式搜索路徑:

testdw-# \h alter role

Command: ALTER ROLE

Description: change a database role

Syntax:

ALTER ROLE name RENAME TO newname

ALTER ROLE name SET config_parameter {TO | =} {value | DEFAULT}

ALTER ROLE name RESET config_parameter

ALTER ROLE name RESOURCE QUEUE {queue_name | NONE}

ALTER ROLE name [ [WITH] option [ ... ] ]

where option can be:

SUPERUSER | NOSUPERUSER

| CREATEDB | NOCREATEDB

| CREATEROLE | NOCREATEROLE

| CREATEEXTTABLE | NOCREATEEXTTABLE

[ ( attribute=‘value‘[, ...] ) ]

where attributes and values are:

type=‘readable‘|‘writable‘

protocol=‘gpfdist‘|‘http‘|‘gphdfs‘

| INHERIT | NOINHERIT

| LOGIN | NOLOGIN

| CONNECTION LIMIT connlimit

| [ENCRYPTED | UNENCRYPTED] PASSWORD ‘password‘

| VALID UNTIL ‘timestamp‘

testdw=# select * from pg_roles; 查詢pg_roles字典表

rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcatupdate | rolcanlogin | rolconnlimit | rolpassword | rolvaliduntil | rolconfig | rolresqueue | oid | rolcreaterextgpfd | rolcreaterexthttp | rolcreatewextgpfd | rolcreaterexthdfs |

olcreatewexthdfs

-----------+----------+------------+---------------+-------------+--------------+-------------+--------------+-------------+---------------+-----------+-------------+-------+-------------------+-------------------+-------------------+-------------------+-

-----------------

mavshuang | f | t | f | f | f | t | -1 | ******** | | | 6055 | 16384 | f | f | f | f |

admin | f | t | t | t | f | f | -1 | ******** | | | 6055 | 16385 | f | f | f | f |

gpadmin | t | t | t | t | t | t | -1 | ******** | | | 6055 | 10 | t | t | t | t |

(3 rows)

testdw=# alter role mavshuang set search_path to public,sc01,pg_catalog; 改動mavshuang角色的搜索路徑為public,sc01,pg_catalog;

ALTER ROLE

testdw=# select * from pg_roles; 再次查詢顯示

rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcatupdate | rolcanlogin | rolconnlimit | rolpassword | rolvaliduntil | rolconfig | rolresqueue | oid | rolcreaterextgpfd | rolcreaterexthttp | rolcreate

extgpfd | rolcreaterexthdfs | rolcreatewexthdfs

-----------+----------+------------+---------------+-------------+--------------+-------------+--------------+-------------+---------------+------------------------------------------+-------------+-------+-------------------+-------------------+----------

--------+-------------------+-------------------

admin | f | t | t | t | f | f | -1 | ******** | | | 6055 | 16385 | f | f | f

| f | f

gpadmin | t | t | t | t | t | t | -1 | ******** | | | 6055 | 10 | t | t | t

| t | t

mavshuang | f | t | f | f | f | t | -1 | ******** | |{"search_path=public, sc01, pg_catalog"}| 6055 | 16384 | f | f | f

| f | f

(3 rows)

  • 查看當前的模式:通過current_schema()函數或者SHOW命令來查看:

testdw=# select current_schema(); 僅僅能顯示一個模式

current_schema

----------------

sc01

(1 row)

testdw=# show search_path; 顯示當前數據庫全部的模式

search_path

--------------------------

sc01, public, pg_catalog

(1 row)

  • 刪除模式:使用DROPSCHEMA命令(空模式)

testdw=# \h drop schema

Command: DROP SCHEMA

Description: remove a schema

Syntax:

DROP SCHEMA [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ] 當該模式下有對象時能夠使用CASCADE命令

testdw=# drop schema sc01;

DROP SCHEMA

  • 系統模式

pg_catalog模式存儲系統日誌表.內置類型.函數和運算符。

Information_schem模式由一個標準化視圖構成。當中包括DB中對象的信息。

pg_toast模式是存儲大對象(系統內部使用)

pg_bitmapindex模式存儲bitmap index對象(系統內部使用)

pg_aoseg存儲append-only表(系統內部使用)

gp_toolkit是管理用的模式,能夠查看和檢索系統日誌文件和其它系統信息。

Greenplum中定義數據庫對象之創建與管理模式