クイックスタート: Azure CLI から Azure Database for PostgreSQL - フレキシブル サーバーに接続してクエリを実行する

適用対象: Azure Database for PostgreSQL - フレキシブル サーバー

このクイック スタートでは、Azure CLI を使用して Azure Database for PostgreSQL フレキシブル サーバー インスタンスに接続し、コマンドを az postgres flexible-server connect 使用して単一のクエリまたは SQL ファイルを az postgres flexible-server execute 実行する方法について説明します。 このコマンドを使用すると、データベース サーバーとの接続をテストしたり、クエリを実行したりすることができます。 対話モードを使用して、複数のクエリを実行することもできます。

前提条件

  • Azure アカウント。 所有していない場合は、無料試用版を入手してください。
  • Azure CLI の最新バージョン (2.20.0 以降) をインストールする
  • Azure CLI を使用して az login コマンドでログインする
  • 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 が開放されているかどうかを確認します。
  • サーバー管理者のユーザー名とパスワードが正しいかどうかを確認します。
  • クライアント マシンにファイアウォール規則が構成されているかどうかを確認します。
  • 仮想ネットワークにプライベート アクセスを使用してサーバーを構成した場合、クライアント マシンが同じ仮想ネットワークに存在することを確認します。

対話モードを使用して複数のクエリを実行する

対話 (interactive) モードを使用して、複数のクエリを実行することができます。 対話モードを有効にするには、次のコマンドを実行します。

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 シェルのエクスペリエンスが表示されます:

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 ファイルを実行する

SQL ファイルを実行するには、コマンドで --file-path 引数 (-f) を使用します。

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.

次の手順