在 PostgreSQL 中使用資料庫
 
本文演示了在 PostgreSQL 中連線資料庫、建立新資料庫以及建立表。
PostgreSQL 中的可用資料庫
你可以在開啟 Postgres 命令列後執行以下命令,以檢視所有存在和連線的可用資料庫。
postgres=# \l
                                                 List of databases
   Name    |  Owner   | Encoding |          Collate           |           Ctype            |   Access privileges
-----------+----------+----------+----------------------------+----------------------------+-----------------------
 postgres  | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 template0 | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres          +
           |          |          |                            |                            | postgres=CTc/postgres
 template1 | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres          +
           |          |          |                            |                            | postgres=CTc/postgres
(3 rows)
在這裡,你可以看到 3 行,這意味著我們在 Postgres 中有三個資料庫。你需要了解資料庫並不意味著表。
一個資料庫裡面可以有多個表。此外,該表可能與其他表相關。
讓我們看看如何連線到資料庫。
當你第一次安裝 Postgres 時,你會發現預設為你建立的這三個資料庫。如果你不連線任何資料庫,預設情況下,你稍後建立的所有表都將轉到名為 Postgres 的資料庫。
連線到 PostgreSQL 中的資料庫
你需要開啟 psql shell 或從終端開啟 psql 以連線到資料庫。然後使用憑據,登入 Postgres;之後,使用以下命令。
postgres=# \c postgres
You are now connected to database "postgres" as user "postgres".
所以,終端說你現在連線到一個名為 Postgres 的資料庫。讓我們看看我們在這個資料庫中有哪些表。
要檢視資料庫中可用的表列表,我們需要編寫命令\dt <database_name>。例如:
postgres=# \c postgres
You are now connected to database "postgres" as user "postgres".
postgres=# \dt
          List of relations
 Schema |   Name   | Type  |  Owner
--------+----------+-------+----------
 public | accounts | table | postgres
(1 row)
在 PostgreSQL 中建立一個新資料庫
假設你需要一個自己的資料庫,並且你將在那裡管理一些表。建立資料庫的基本語法是 CREATE DATABASE <database_name>。
建立後,讓我們建立一個名為 titan 的資料庫並檢視可用資料庫列表。
postgres=# CREATE DATABASE TITAN;
CREATE DATABASE
postgres=# \l
                                                 List of databases
   Name    |  Owner   | Encoding |          Collate           |           Ctype            |   Access privileges
-----------+----------+----------+----------------------------+----------------------------+-----------------------
 postgres  | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 template0 | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres          +
           |          |          |                            |                            | postgres=CTc/postgres
 template1 | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres          +
           |          |          |                            |                            | postgres=CTc/postgres
 titan     | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
(4 rows)
postgres=# \c titan
You are now connected to database "titan" as user "postgres".
此外,還有另一種建立資料庫的方法。格式為 createdb [options...] [database_name [description of database]]。
這是 [PostgreSQL] 中可用選項的列表(https://postgrespro.com/docs/postgresql/9.6/app-createdb)。
| 選項 | 說明 | 
|---|---|
| -D | Default tablespace for the database | 
| -h | hostname of the machine where the server is sunning | 
| -e | Echo the commands that createdbgenerates and sends to the server | 
| -E | Specifies the character encoding scheme to be used in this database | 
這裡是 PostgreSQL 官方文件中的完整選項列表。
在 PostgreSQL 的連線資料庫中建立表
當我們連線到名為 titan 的資料庫時,讓我們看看是否存在任何表。
postgres=# \c titan
You are now connected to database "titan" as user "postgres".
titan=# \dt
Did not find any relations.
如你所見,資料庫中沒有表。此外,如果你注意到,這裡有一些小的變化。
當我們連線到 titan 資料庫時,該行以 titan=# 開頭,這意味著控制檯在 titan 資料庫上執行。
讓我們在這裡建立一個表,如下所示。
CREATE TABLE Colossal (
	titan_id serial PRIMARY KEY,
	titan_name VARCHAR ( 50 ) NOT NULL,
	strength_level INT NOT NULL
);
CREATE TABLE
titan=# \dt
          List of relations
 Schema |   Name   | Type  |  Owner
--------+----------+-------+----------
 public | colossal | table | postgres
(1 row)
titan=#
現在,你可以在表中執行 CRUD 操作。我們可以看到 colossal 表現在在 titan 資料庫中可用。
有關資料庫建立和設定的更多資訊,請遵循官方文件。
