PostgreSQL中的schema和user


postgresql中,用戶創建的所有對象都被創建在指定的schema(或namespace)中。其他用戶可能擁有、也可能不擁有訪問這些對象的權限,甚至都不可以在對應的schema中創建對象。

從上面的表可以看出,用戶(或角色)是全局對象,不是定義在數據庫中,而是定義在實例的級別。schema是用戶在指定的數據庫中創建的,其中包含數據庫對象。

 

可能有人對下面的操作感到困惑:

$ psql
psql (11.9)
Type "help" for help.

postgres=# create table t1(a int);
CREATE TABLE
postgres=# 

在這個創建表的語句中,並沒有指定schema。那這些表會在哪里呢?

每個postgresql數據庫默認都有一個public schema,如果沒有為對象顯式地指定schema,新創建的對象就是放在public schema中。有幾種方式可以查看給定的表是屬於哪個schema,但是最常用的主要有兩種:1.查看系統目錄視圖;2.使用psql短命令

postgres=# select schemaname from pg_tables where tablename ='t1';
 schemaname 
------------
 public
(1 rows)

postgres=# \d t1
                 Table "public.t1"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 a      | integer |           |          | 

postgres=# 

在postgresql中,public schema是一個特殊的schema,你要么移除該schema,要么收回在public schema上授予給public的權限。

 

當你刪除了public schema后嘗試創建一個表,會發生什么呢?

postgres=# drop schema public cascade;
NOTICE:  drop cascades to table t1
DROP SCHEMA
postgres=# create table t1 ( a int );
ERROR:  no schema has been selected to create in
LINE 1: create table t1 ( a int );
                     ^
postgres=# 

  


我們現在沒有schema了:

postgres=# \dn
List of schemas
 Name | Owner 
------+-------
(0 rows)

postgresql不知道去哪里創建表。這里也可以看出,在postgresql中schema和user是不同的了。我們是以postgres用戶登錄,但是沒有schema可以創建對象。

 

現在,我們來創建一個schema:

postgres=# create schema my_schema;
CREATE SCHEMA
postgres=# create table t1(a int);
ERROR:  no schema has been selected to create in
LINE 1: create table t1(a int);
                     ^
postgres=# 

仍然不可以創建表。這里可能會問:為什么有public schema就可以呢?在上面的演示中我們並沒有指定public schema呀!這里就需要提到參數search_path了:

postgres=# show search_path;
   search_path   
-----------------
 "$user", public
(1 row)

postgres=# 

缺省情況下,search_path包含當前用戶名和public。所以會創建失敗。

有兩種方式來解決:

postgres=# create table my_schema.t1 ( a int );
CREATE TABLE
postgres=# \d my_schema.t1
               Table "my_schema.t1"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 a      | integer |           |          | 

或者調整search_path的值:

postgres =  # set search_path = 'my_schema',"$user",public;
SET
postgres =  # show search_path ;
search_path
----------------------------
my_schema, "$user", public
(1 row)

postgres =  # create table t2 ( a int );
CREATE TABLE
postgres =  # \d t2
				Table "my_schema.t2"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 a      | integer |           |          | 

postgres =  # 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM