
使用 psql 指令,用 postgres su 身份連入 postgreSQL
sudo -u postgres psql
進入後可使用指令 \l 查詢資䇆庫。
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
-----------+----------+----------+---------+---------+------------+-----------------+-----------------------
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 | | libc |
template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | | libc | =c/postgres +
| | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | | libc | =c/postgres +
| | | | | | | postgres=CTc/postgres
使用 Database 指令創建新的資料庫。
CREATE DATABASE blogs;
資料庫創建完,可使用 \l 查看是否成功創建,也可使用 \c 切換資料庫
postgres=# \c blogs
You are now connected to database "blogs" as user "postgres".
使用指令建立一個使用者,並指密碼。
CREATE USER howard PASSWORD '12345678';
可以使用以下指令,將schema public 底下的所有權限指定給該使用者。
GRANT ALL ON SCHEMA public TO howard;
GRANT ALL ON ALL TABLES IN SCHEMA public TO howard;
postpreSQL中,一個資料庫叢集(database cluster)包含了一個或多個命名資料庫(named database)。角色(rule)還有 其它的手物件目類型在叢集內共享。一個用戶端的連線透過連線字串。,一次只能請求單個資料庫。
一個資料庫業集中,包函一個或多個 schema 。一個 schema 又包含多個 tables。
一個 schema 內同時還包含了多個命名物件(named object)。如:資料型別(data type)、函式(function)、運算子(operator)等等。
一個 schema 為一個命名空間。因此,在一個 schema 內,資料表的命名是不能重覆的。同時,索引(index)、視圖(view)等等都 不能重覆命名。
一些使用 schema 的理由:
可以使用以下的指令產生一個 schema
CREATE SCHEMA myschema;
CREATE SCHEMA schema_name AUTHORIZATION user_name;
schema_name。在這種狀況下,會建立一個與使用者同名的 schema。