你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:通过 Azure CLI 与 Azure Database for PostgreSQL - 灵活服务器进行连接和查询

适用于:Azure Database for PostgreSQL 灵活服务器

本快速入门演示如何使用 Azure CLI 通过 az postgres flexible-server connect 命令连接到 Azure Database for PostgreSQL 灵活服务器实例并执行单个查询或 sql 文件 az postgres flexible-server execute 。 通过此命令可测试与数据库服务器的连接并运行查询。 还可以使用交互模式运行多个查询。

先决条件

  • 一个 Azure 帐户。 如果没有帐户,可获取一个免费试用帐户
  • 安装 Azure CLI 最新版本(2.20.0 或更高版本)
  • 通过 az login 命令使用 Azure CLI 登录
  • 使用 az config param-persist on 启用参数持久性。 参数持久性将帮助你使用本地上下文,而无需重复大量参数,如资源组或位置。

创建 Azure Database for PostgreSQL 灵活服务器实例

创建的第一件事是托管的 Azure Database for PostgreSQL 灵活服务器实例。 在 Azure Cloud Shell 中,运行以下脚本,并记下该命令生成的“服务器名称”、“用户名”和“密码”。

az postgres flexible-server create --public-access <your-ip-address>

你可以为此命令提供更多参数来自定义它。 查看 az postgres flexible-server create 的所有参数。

查看所有参数

可以通过 --help 参数查看此命令的所有参数。

az postgres flexible-server connect --help

测试数据库服务器连接

可以使用命令在开发环境中测试和验证与数据库的连接。

az postgres flexible-server connect -n <servername> -u <username> -p "<password>" -d <databasename>

示例:

az postgres flexible-server connect -n postgresdemoserver -u dbuser -p "dbpassword" -d postgres

如果连接成功,则会显示输出。

Command group 'postgres flexible-server' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
Successfully connected to postgresdemoserver.
Local context is turned on. Its information is saved in working directory C:\mydir. You can run `az local-context off` to turn it off.
Your preference of  are now saved to local context. To learn more, type in `az local-context --help`

如果连接失败,请尝试以下解决方案:

  • 检查是否已在客户端计算机上打开端口 5432。
  • 如果服务器管理员用户名和密码正确
  • 如果已为客户端计算机配置防火墙规则
  • 如果已在虚拟网络中为服务器配置了专用访问权限,请确保客户端计算机位于同一虚拟网络中。

使用交互模式运行多个查询

可以使用“交互”模式运行多个查询。 若要启用交互模式,请运行以下命令

az postgres flexible-server connect -n <servername> -u <username> -p "<password>" -d <databasename>

示例:

az postgres flexible-server connect -n postgresdemoserver -u dbuser -p "dbpassword" -d flexibleserverdb --interactive

你将看到 psql shell 体验,如下所示:

Command group 'postgres flexible-server' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
Password for earthyTurtle7:
Server: PostgreSQL 12.5
Version: 3.0.0
Chat: https://gitter.im/dbcli/pgcli
Home: http://pgcli.com
postgres> create database pollsdb;
CREATE DATABASE
Time: 0.308s
postgres> exit
Goodbye!
Local context is turned on. Its information is saved in working directory C:\sunitha. You can run `az local-context off` to turn it off.
Your preference of  are now saved to local context. To learn more, type in `az local-context --help`

示例:

az postgres flexible-server execute -n postgresdemoserver -u dbuser -p "dbpassword" -d flexibleserverdb -q "select * from table1;" --output table

你将看到如下所示的输出:

Command group 'postgres flexible-server' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
Successfully connected to postgresdemoserver.
Ran Database Query: 'select * from table1;'
Retrieving first 30 rows of query output, if applicable.
Closed the connection postgresdemoserver.
Local context is turned on. Its information is saved in working directory C:\mydir. You can run `az local-context off` to turn it off.
Your preference of  are now saved to local context. To learn more, type in `az local-context --help`
Txt    Val
-----  -----
test   200
test   200
test   200
test   200
test   200
test   200
test   200

运行 SQL 文件

可以通过 --file-path 参数 -f 使用命令执行 SQL 文件。

az postgres flexible-server execute -n <server-name> -u <username> -p "<password>" -d <database-name> --file-path "<file-path>"

示例:

az postgres flexible-server execute -n postgresdemoserver -u dbuser -p "dbpassword" -d flexibleserverdb -f "./test.sql"

你将看到如下所示的输出:

Command group 'postgres flexible-server' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
Running sql file '.\test.sql'...
Successfully executed the file.
Closed the connection to postgresdemoserver.

后续步骤