Python Data Access 简明教程

Python PostgreSQL - Introduction

Installation

PostgreSQL 是一个功能强大的开源对象关系数据库系统。它拥有超过 15 年的积极开发阶段,并拥有经过验证的架构,使其享有很高的可靠性、数据完整性和正确性声誉。

要使用 Python 与 PostgreSQL 进行通信,您需要安装 psycopg,一个为 python 编程提供的适配器,它的当前版本是 psycog2

psycopg2 的编写目标是体积小、速度快且稳定。它在 PIP(python 的包管理器)中可用。

Installing Psycog2 using PIP

首先,确保系统中正确安装了 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

Verification

要验证安装,请使用以下行创建一个示例 python 脚本。

import mysql.connector

如果安装成功,在执行该脚本后,不应出现任何错误:

D:\Python_PostgreSQL>import psycopg2
D:\Python_PostgreSQL>