快速入门:使用 Python 连接到 Azure Database for PostgreSQL 灵活服务器并查询其中的数据
适用于:Azure Database for PostgreSQL 灵活服务器
在本快速入门中,你将使用 Python 连接到 Azure Database for PostgreSQL 灵活服务器实例。 然后使用 SQL 语句在 Mac、Ubuntu Linux 和 Windows 平台的数据库中查询、插入、更新和删除数据。
本文假设你熟悉如何使用 Python 进行开发,但不熟悉 Azure Database for PostgreSQL 灵活服务器的用法。
先决条件
- 具有活动订阅的 Azure 帐户。 创建试用帐户。
- Azure Database for PostgreSQL 灵活服务器实例。 要创建 Azure Database for PostgreSQL 灵活服务器实例,请参阅使用 Azure 门户创建 Azure Database for PostgreSQL - 灵活服务器实例。
- Python 2.7 或 3.6+。
- 最新 pip 包安装程序。
准备客户端工作站
- 如果使用“专用访问(VNet 集成)”创建了 Azure Database for PostgreSQL 灵活服务器实例,需要从与服务器相同的 VNet 中的资源连接到服务器。 可以创建虚拟机并将其添加到使用 Azure Database for PostgreSQL 灵活服务器实例创建的 VNet 中。 请参阅使用 Azure CLI 创建和管理 Azure Database for PostgreSQL 灵活服务器虚拟网络。
- 如果使用“公共访问(允许的 IP 地址)”创建了 Azure Database for PostgreSQL 灵活服务器实例,可以将本地 IP 地址添加到服务器上的防火墙规则列表中。 请参阅使用 Azure CLI 创建和管理 Azure Database for PostgreSQL 灵活服务器防火墙规则。
安装适用于 PostgreSQL 的 Python 库
psycopg2 模块可连接到 PostgreSQL 数据库并对其进行查询,并可作为 Linux、macOS 或 Windows 轮包提供。 安装二进制版本的模块,包括所有依赖项。
若要安装 psycopg2
,请打开终端或命令提示符并运行命令 pip install psycopg2
。
获取数据库连接信息
连接到 Azure Database for PostgreSQL 灵活服务器实例需要完全限定的服务器名称和登录凭据。 可以从 Azure 门户获取此信息。
在 Azure 门户中,搜索 Azure Database for PostgreSQL 灵活服务器名称并选择该名称。
在服务器的“概述”页上,复制完全限定的“服务器名称”和“管理员用户名” 。 完全限定的“服务器名称”始终为“<my-server-name>.postgres.database.chinacloudapi.cn”的格式。
你还需要管理员密码。 如果忘记,可以从概述页重置它。
如何运行 Python 示例
对于本文中的每个代码示例:
在文本编辑器中创建新的文件。
将代码示例添加到文件。 在代码中,进行以下替换:
<server-name>
和<admin-username>
替换为从 Azure 门户复制的值。<admin-password>
替换为服务器密码。<database-name>
替换为 Azure Database for PostgreSQL 灵活服务器数据库的名称。 创建服务器时,会自动创建一个名为 postgres 的默认数据库。 你可以重命名该数据库,或使用 SQL 命令创建新的数据库。
将文件保存在具有 .py 扩展名的项目文件夹(如 postgres-insert.py)中 。 对于 Windows,确保在保存文件时选择 UTF-8 编码。
若要运行该文件,请在命令行界面中切换到项目文件夹,然后键入
python
后跟文件名,例如python postgres-insert.py
。
创建表并插入数据
下面的代码示例使用 psycopg2.connect 函数连接到 Azure Database for PostgreSQL 灵活服务器数据库,并使用 SQL INSERT 语句加载数据。 cursor.execute 函数对数据库执行 SQL 查询。
import psycopg2
# Update connection string information
host = "<server-name>"
dbname = "<database-name>"
user = "<admin-username>"
password = "<admin-password>"
sslmode = "require"
# Construct connection string
conn_string = "host={0} user={1} dbname={2} password={3} sslmode={4}".format(host, user, dbname, password, sslmode)
conn = psycopg2.connect(conn_string)
print("Connection established")
cursor = conn.cursor()
# Drop previous table of same name if one exists
cursor.execute("DROP TABLE IF EXISTS inventory;")
print("Finished dropping table (if existed)")
# Create a table
cursor.execute("CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);")
print("Finished creating table")
# Insert some data into the table
cursor.execute("INSERT INTO inventory (name, quantity) VALUES (%s, %s);", ("banana", 150))
cursor.execute("INSERT INTO inventory (name, quantity) VALUES (%s, %s);", ("orange", 154))
cursor.execute("INSERT INTO inventory (name, quantity) VALUES (%s, %s);", ("apple", 100))
print("Inserted 3 rows of data")
# Clean up
conn.commit()
cursor.close()
conn.close()
如果代码成功运行,则会生成以下输出:
读取数据
下面的代码示例连接到 Azure Database for PostgreSQL 灵活服务器数据库,并使用 cursor.execute 和 SQL SELECT 语句来读取数据。 此函数可接受查询,并返回可使用 cursor.fetchall() 循环访问的结果集
import psycopg2
# Update connection string information
host = "<server-name>"
dbname = "<database-name>"
user = "<admin-username>"
password = "<admin-password>"
sslmode = "require"
# Construct connection string
conn_string = "host={0} user={1} dbname={2} password={3} sslmode={4}".format(host, user, dbname, password, sslmode)
conn = psycopg2.connect(conn_string)
print("Connection established")
cursor = conn.cursor()
# Fetch all rows from table
cursor.execute("SELECT * FROM inventory;")
rows = cursor.fetchall()
# Print all rows
for row in rows:
print("Data row = (%s, %s, %s)" %(str(row[0]), str(row[1]), str(row[2])))
# Cleanup
conn.commit()
cursor.close()
conn.close()
更新数据
下面的代码示例连接到 Azure Database for PostgreSQL 灵活服务器数据库,并使用 cursor.execute 和 SQL UPDATE 语句来更新数据。
import psycopg2
# Update connection string information
host = "<server-name>"
dbname = "<database-name>"
user = "<admin-username>"
password = "<admin-password>"
sslmode = "require"
# Construct connection string
conn_string = "host={0} user={1} dbname={2} password={3} sslmode={4}".format(host, user, dbname, password, sslmode)
conn = psycopg2.connect(conn_string)
print("Connection established")
cursor = conn.cursor()
# Update a data row in the table
cursor.execute("UPDATE inventory SET quantity = %s WHERE name = %s;", (200, "banana"))
print("Updated 1 row of data")
# Cleanup
conn.commit()
cursor.close()
conn.close()
删除数据
下面的代码示例连接到 Azure Database for PostgreSQL 灵活服务器数据库,并使用 cursor.execute 和 SQL DELETE 语句来删除以前插入的库存项。
import psycopg2
# Update connection string information
host = "<server-name>"
dbname = "<database-name>"
user = "<admin-username>"
password = "<admin-password>"
sslmode = "require"
# Construct connection string
conn_string = "host={0} user={1} dbname={2} password={3} sslmode={4}".format(host, user, dbname, password, sslmode)
conn = psycopg2.connect(conn_string)
print("Connection established")
cursor = conn.cursor()
# Delete data row from table
cursor.execute("DELETE FROM inventory WHERE name = %s;", ("orange",))
print("Deleted 1 row of data")
# Cleanup
conn.commit()
cursor.close()
conn.close()