1. 程式人生 > >PostgreSQL即學即用:庫、表空間、角色、SCHEMA

PostgreSQL即學即用:庫、表空間、角色、SCHEMA

前置

sudo su - postgres // 切換到postgres使用者 1、PostgreSQL會在安裝階段預設建立一個超級使用者角色和一個database,均是postgres, a、修改PostgreSQL資料庫預設使用者postgres的密碼 sudo  passwd -d postgres //刪除使用者postgres的密碼 sudo -u postgres passwd //設定使用者postgres的密碼 // 系統提示輸入新的密碼 New password: Retype new password: passwd: password updated successfully

 PG中可以採用分庫的方式,即不同的模組建立不同的庫;也可以使用同一個庫下建立多個SCHEMA的方式;但一個數據連線只能訪問一個庫,不能跨庫訪問,即不同資料庫之間不能關聯查詢,參考: https://www.postgresql.org/docs/9.6/static/ddl-schemas.html        A PostgreSQL database cluster contains one or more named databases. Users and groups of users are shared across the entire cluster, but no other data is shared across databases. Any given client connection to the server can access only the data in a single database, the one specified in the connection request.       建議採用同一個庫下建立不同SCHEMA的方式

表空間

建表時,可以指定該表存放在哪個表空間上, 如:create table tbl(id int, name text) tablespace mytablespace 如果不指定,建立在預設的表空間上; PG中可以為某個庫設定預設表空間,也可以為某個使用者設定預設表空間; 如:ALTER USER mydb_role SET default_tablespace='mytablespace'

注意:

  • PostgreSQL中建立表空間時,需要指定的是路徑,不是檔案,且不需要設定表空間的最大大小(Oracle需要根據表空間的最大大小計算需要生成多少個最大4G的資料檔案)

         PG中建立表空間的方式如:           mkdir /usr/local/pgdata           chown postgres:postgres /usr/local/pgdata/           create tablespace tbs_test owner postgres location '/usr/local/pgdata';    

  • 資料庫中不建議使用雙引號,最好還是在建表和查詢時都不要使用雙引號,不推薦在DDL裡用雙引號的方式建立區分大小寫的物件。預設會把不帶雙引號的表名轉成小寫表名去進行操作。

角色

// CREATE ROLE 角色術語來表示使用者賬戶的概念, CREATE USER (可登入角色)和 CREATE GROUP(組角色)不建議使用。 CREATE ROLE mydb_admin login password 'abc123'; drop DATABASE if exists mydb; CREATE DATEBASE mydb WITH owner = mydb_admin;  // 這樣就可以用mydb_admin登入建立schema和表等,否則如下 drop schema if exists mydb_schema cascade; drop tablespace if exists mydb_dbname; drop user if exists mydb_role; --userCREATE USER mydb_role WITH     LOGIN     NOSUPERUSER     CREATEDB     NOCREATEROLE     INHERIT     REPLICATION     CONNECTION LIMIT -1     PASSWORD '%$MY-PWD$%';

--create tablespace CREATE TABLESPACE mydb_dbname OWNER mydb_role LOCATION mydb_dbpath; --default_tablespace ALTER USER mydb_role SET default_tablespace='mydb_dbname'; --schema CREATE SCHEMA mydb_schema AUTHORIZATION mydb_role; --default schema ALTER USER mydb_role SET search_path=mydb_schema; GRANT USAGE ON SCHEMA mydb_schema TO PUBLIC; ALTER DEFAULT PRIVILEGES for user mydb_role in schema mydb_schema  GRANT SELECT ON TABLES TO public;

SCHEMA

                 CREATE SCHEMA %$MY-SCHEMA$% AUTHORIZATION %$MY-USER$%;          也可以省略schema_name,如:                 CREATE SCHEMA  AUTHORIZATION %$MY-USER$%;

省略schema_name的情況下,schema_name和user相同,且通過該user連線上來時,預設就在該schema下,參考: https://www.postgresql.org/docs/9.6/static/ddl-schemas.html You can even omit the schema name, in which case the schema name will be the same as the user name. Recall that the default search path starts with $user, which resolves to the user name. Therefore, if each user has a separate schema, they access their own schemas by default.

後置

MARK: 紅色部分很重要