跳轉到內容

PostgreSQL/下載和安裝

來自 Wikibooks,開放世界中的開放書籍


在下載 PostgreSQL 之前,您需要做出兩個重要的決定。首先,決定是從原始碼編譯安裝 PostgreSQL 還是從預構建的二進位制檔案安裝。其次(如果您想使用任何二進位制檔案),您必須知道需要軟體的哪個作業系統。PostgreSQL 支援大多數基於 UNIX 的系統(包括 macOS)以及 Windows。

做出這些決定後,您可以下載並使用完整的原始碼、安裝程式、Bitnami 基礎設施堆疊或純二進位制檔案。

從原始碼開始

[編輯 | 編輯原始碼]

原始碼可作為單個壓縮檔案 [1] 或 git 儲存庫 [2] 提供。要從原始碼安裝,您必須將其下載到本地計算機,並使用 C 編譯器(至少符合 C99 標準,大多數情況下人們使用 GCC)將其編譯為計算機的二進位制格式。有關要求 [3]、下載過程和編譯步驟 [4] 的詳細資訊可在 PostgreSQL 文件中找到。

使用原始碼的優勢在於您可以閱讀和研究它、修改它或在奇特的平臺上編譯它。但您必須具備一些預先知識和經驗才能處理作業系統的特定任務,例如:在 shell 中工作、安裝其他程式等。

PostgreSQL 文件在以下章節中描述了從原始碼安裝的所有細節:

藉助預構建程式開始

[編輯 | 編輯原始碼]

與從原始碼開始相反,使用預構建程式或指令碼之一相對容易。對於初學者來說,這是首選方法。您可以從以下幾種選項中進行選擇:

  • 安裝程式 [5]:這是在本地計算機上下載和安裝 PostgreSQL 最舒適的方法。安裝程式不僅會引導您完成安裝步驟,還會提供安裝有用的其他工具和驅動程式的選項。並非所有作業系統的版本都提供安裝程式。
  • Bitnami 基礎設施堆疊 [6]:這些堆疊(WAPP、MAPP、LAPP 等)提供了完整的基礎設施(PostgreSQL、Apache Web 伺服器、PHP)在 Windows、macOS 或 Linux 上執行 Web 應用程式。
  • 純二進位制檔案 [7]:這是作業系統特定命令的列表,它將引導您完成二進位制檔案的下載和安裝過程。

安裝 Linux(Ubuntu)的二進位制檔案 "PostgreSQL Apt 儲存庫". 檢索於 2021 年 11 月 13 日.

# Create the file repository configuration:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

# Import the repository signing key:
wget --quiet -O - https://postgres.tw/media/keys/ACCC4CF8.asc | sudo apt-key add -

# Update the package lists:
sudo apt-get update

# Install the latest version of PostgreSQL.
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
sudo apt-get -y install PostgreSQL

啟動和停止

sudo /etc/init.d/postgresql start
sudo /etc/init.d/postgresql stop

Windows

預設情況下,PostgreSQL 會在每次重啟時啟動,因此它可能會佔用大量資源。為了避免這種情況,只需執行 services.msc 並將 PostgreSQL 服務更改為手動啟動。然後,建立一個包含以下內容的檔案 postgresql.cmd

net start postgresql-x64-9.5
pause
net stop postgresql-x64-9.5

只要以管理員身份啟動此指令碼,叢集及其所有資料庫都可用。只需按一個鍵即可關閉服務。

更多資訊

[編輯 | 編輯原始碼]

PostgreSQL wiki 提供了有關安裝步驟的更多資訊和提示。

成功安裝後,您將擁有

  • 磁碟上的 PostgreSQL 二進位制檔案。
  • 磁碟上名為 data 的第一個 叢集。叢集包含一個名為 postgres 的空 資料庫(以及兩個模板資料庫)和一個名為 postgres使用者角色
  • 一組 Unix 程式或 Windows 服務正在您的計算機上執行。這些程式/服務處理叢集及其所有資料庫。

預設情況下,PostgreSQL 偵聽 5432。您可能需要配置防火牆以反映這種情況。

連線到資料庫

[編輯 | 編輯原始碼]

成功安裝後,您將擁有一個 data 叢集、一個 postgres 資料庫、資料庫超級使用者 postgres 以及一個新的作業系統使用者 postgres。使用新的作業系統使用者在作業系統級別登入。在 shell 中,您可以透過經常使用的程式 psql 連線到新資料庫。psql 是一個類似於 shell 的行模式程式,允許您將 SQL 命令傳送到資料庫。

$ # Example in Unix syntax
$ su - postgres
Password: 
$
$ # psql --help      to see a detailed explanation of psql's options
$ # psql [OPTION]... [DBNAME [USERNAME]]
$ psql postgres postgres
psql (14.1 (Ubuntu 14.1-2.pgdg20.04+1))
Type "help" for help.

postgres=# 
postgres=# \q  -- terminate psql with backslash q  or  ctrl-d
$

psql 的預設提示(每行開頭的字首)是“postgres=#”。成功啟動它後,您可以使用 SQL 命令與資料庫通訊。以下是一個建立名為“nancy”的新資料庫使用者的示例,並在之後刪除它。

postgres=# CREATE USER nancy WITH ENCRYPTED PASSWORD 'ab8sxx5F4';
CREATE ROLE
postgres=#
postgres=# DROP USER nancy;  -- delete the user
DROP ROLE
postgres=#

資料庫對每個 SQL 命令的響應表明其成功執行或錯誤。在前面的示例中,CREATE ROLE 表示已建立使用者。

關注點分離

[編輯 | 編輯原始碼]

請回顧一下您目前所擁有的一切:一個 叢集 data、一個 資料庫 postgres、一個 使用者 postgres。此外,PostgreSQL 將每個資料庫劃分為稱為 模式 的邏輯單元。大多數物件都駐留在這樣的模式中。預設模式名為 public,存在於每個資料庫中。同樣適用於儲存系統資訊的某些特殊模式。只要您沒有顯式使用模式名稱,預設情況下就會使用 public 模式。這意味著 CREATE TABLE t (column_1 INTEGER); 命令將在 public 模式下建立表 t


我們建議避免使用 public 模式儲存您的資料。因為 public 存在於每個資料庫中,一些工具會將資料儲存在那裡。建立並使用自己的模式以明確區分系統資料、工具資料和使用者資料。

其次,避免使用使用者 postgres。該使用者帳戶具有非常強大的許可權,您應該很少使用它。建立一個用作資料、檢視、函式、觸發器等的利益相關者的使用者。


以下指令碼將建立一個新使用者及其模式。

$ # start 'psql' as the original 'postgres' user with its strong priviledges
$ psql postgres postgres
postgres=# -- the owner of the new schema shall be 'finance_master'
postgres=# CREATE USER finance_master WITH CREATEROLE LOGIN ENCRYPTED PASSWORD 'xxx';
CREATE ROLE
postgres=# -- the new schema 'finance' for your data
postgres=# CREATE SCHEMA finance AUTHORIZATION finance_master;
CREATE SCHEMA
postgres=# -- change 'search_path' (description of search_path: see below)
postgres=# ALTER ROLE finance_master SET search_path = finance, public;
ALTER ROLE
postgres=# \q

使用新使用者finance_master啟動psql。我們希望他能夠在finance模式下工作,但psql和PostgreSQL之間的每個連線都在資料庫級別進行。無法為連線指定單獨的模式。因此,PostgreSQL實施了一種稱為search_path的機制。它簡化了模式之間的切換。search_path包含模式名稱列表。每當您省略模式名稱時,都會參考此列表以確定要使用的模式。對於我們的使用者finance_manager,我們在上面的ALTER ROLE命令中定義了他在finance模式下工作,並且如果他的SQL命令(例如SELECT)沒有命中,則會查詢public模式。

$ # -- first parameter of psql: database   second parameter: user   nothing for schema
$ psql postgres finance_master
postgres=# -- create a table
postgres=# CREATE TABLE t1 (column_1 INTEGER);  -- table will be in schema 'finance' because of the 'search_path' definition
CREATE TABLE
postgres=# -- you can use the schema name explicitly
postgres=# CREATE TABLE finance.t2 (column_1 INTEGER);  -- table will be in schema 'finance' as well
CREATE TABLE
postgres=# -- it's possible to overwrite 'search_path' by using the schema name explicitly
postgres=# CREATE TABLE public.t3 (column_1 INTEGER);  -- table will be in schema 'public'
CREATE TABLE
postgres=# 
postgres=# \d  -- this command lists schema, table, and owner names
             List of relations
 Schema  |  Name   | Type  |     Owner      
---------+---------+-------+----------------
 finance | t1      | table | finance_master
 finance | t2      | table | finance_master
 public  | t3      | table | finance_master
postgres=#

參考資料

[編輯 | 編輯原始碼]
  1. 單個壓縮檔案中的原始碼(透過FTP) [1]
  2. git 倉庫中的原始碼 [2]
  3. 編譯要求(Unix)[3]
  4. 從原始碼安裝 [4]
  5. 安裝程式 [5]
  6. Bitnami 棧 [6]
  7. 下載二進位制檔案 [7]


華夏公益教科書