Python PostgreSQL - 簡介



安裝

PostgreSQL 是一款功能強大的開源物件關係資料庫系統。它擁有超過 15 年的活躍開發階段,以及久經考驗的架構,使其贏得了可靠性、資料完整性和正確性的良好聲譽。

要使用 Python 與 PostgreSQL 通訊,您需要安裝 psycopg,這是一個為 Python 程式設計提供的介面卡,當前版本為 psycog2

psycopg2 的編寫目標是體積小、速度快,並且穩定可靠。它可以透過 PIP(Python 的包管理器)獲得。

使用 PIP 安裝 Psycog2

首先,確保 Python 和 PIP 已正確安裝在您的系統中,並且 PIP 已更新到最新版本。

要升級 PIP,請開啟命令提示符並執行以下命令:

C:\Users\Tutorialspoint>python -m pip install --upgrade pip
Collecting pip
Using cached https://files.pythonhosted.org/packages/8d/07/f7d7ced2f97ca3098c16565efbe6b15fafcba53e8d9bdb431e09140514b0/pip-19.2.2-py2.py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 19.0.3
Uninstalling pip-19.0.3:
Successfully uninstalled pip-19.0.3
Successfully installed pip-19.2.2

然後,以管理員身份開啟命令提示符並執行 pip install psycopg2-binary 命令,如下所示:

C:\WINDOWS\system32>pip install psycopg2-binary
Collecting psycopg2-binary
Using cached https://files.pythonhosted.org/packages/80/79/d0d13ce4c2f1addf4786f4a2ded802c2df66ddf3c1b1a982ed8d4cb9fc6d/psycopg2_binary-2.8.3-cp37-cp37m-win32.whl
Installing collected packages: psycopg2-binary
Successfully installed psycopg2-binary-2.8.3

驗證

要驗證安裝,請建立一個包含以下行的示例 Python 指令碼。

import mysql.connector

如果安裝成功,當您執行它時,您不應該收到任何錯誤:

D:\Python_PostgreSQL>import psycopg2
D:\Python_PostgreSQL>
廣告