Skip to content

Commit f07b419

Browse files
authored
Merge pull request #274977 from AlicjaKucharczyk/main
cli update
2 parents 7eefb6e + 1d7b80a commit f07b419

File tree

1 file changed

+86
-68
lines changed

1 file changed

+86
-68
lines changed

articles/postgresql/flexible-server/connect-azure-cli.md

Lines changed: 86 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: This quickstart provides several ways to connect with Azure CLI wit
44
author: AlicjaKucharczyk
55
ms.author: alkuchar
66
ms.reviewer: maghan
7-
ms.date: 04/27/2024
7+
ms.date: 05/10/2024
88
ms.service: postgresql
99
ms.subservice: flexible-server
1010
ms.topic: quickstart
@@ -22,127 +22,145 @@ This quickstart demonstrates how to connect to an Azure Database for PostgreSQL
2222

2323

2424
## Prerequisites
25-
- An Azure account. If you don't have one, [get a free trial](https://azure.microsoft.com/free/).
26-
- Install [Azure CLI](/cli/azure/install-azure-cli) latest version (2.20.0 or above)
27-
- Log in using Azure CLI with `az login` command
28-
- Turn on parameter persistence with `az config param-persist on`. Parameter persistence will help you use local context without having to repeat numerous arguments like resource group or location.
25+
- An Azure account with an active subscription. If you don't have one, [get a free trial](https://azure.microsoft.com/free/).
26+
- Install [Azure CLI](/cli/azure/install-azure-cli) latest version.
27+
- Sign in using Azure CLI with `az login` command.
28+
- (optional) Turn on an experimental parameter persistence with `az config param-persist on`. Parameter persistence helps you use local context without having to repeat numerous arguments like resource group or location.
2929

3030
## Create Azure Database for PostgreSQL flexible server instance
3131

32-
The first thing to create is a managed Azure Database for PostgreSQL flexible server instance. In [Azure Cloud Shell](https://shell.azure.com/), run the following script and make a note of the **server name**, **username** and **password** generated from this command.
32+
The first thing to create is a managed Azure Database for PostgreSQL flexible server instance. In [Azure Cloud Shell](https://shell.azure.com/), run the following script and make a note of the **server name**, **username, and **password** generated from this command.
3333

34-
```azurecli
34+
```azurecli-interactive
3535
az postgres flexible-server create --public-access <your-ip-address>
3636
```
3737
You can provide more arguments for this command to customize it. See all arguments for [az postgres flexible-server create](/cli/azure/postgres/flexible-server#az-postgres-flexible-server-create).
3838

3939
## View all the arguments
4040
You can view all the arguments for this command with `--help` argument.
4141

42-
```azurecli
42+
```azurecli-interactive
4343
az postgres flexible-server connect --help
4444
```
4545

4646
## Test database server connection
47-
You can test and validate the connection to the database from your development environment using the command.
47+
You can test and validate the connection to the database from your development environment using the [az postgres flexible-server connect](/cli/azure/postgres/flexible-server#az-postgres-flexible-server-connect) command.
4848

49-
```azurecli
50-
az postgres flexible-server connect -n <servername> -u <username> -p "<password>" -d <databasename>
49+
```azurecli-interactive
50+
az postgres flexible-server connect \
51+
-n <servername> -u <username> -p "<password>" -d <databasename>
5152
```
5253
**Example:**
53-
```azurecli
54-
az postgres flexible-server connect -n postgresdemoserver -u dbuser -p "dbpassword" -d postgres
54+
```azurecli-interactive
55+
az postgres flexible-server connect \
56+
-n server372060240 -u starchylapwing9 -p "dbpassword" -d postgres
5557
```
56-
You'll see the output if the connection was successful.
58+
You see similar output if the connection was successful.
59+
5760
```output
58-
Command group 'postgres flexible-server' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
59-
Successfully connected to postgresdemoserver.
60-
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.
61-
Your preference of are now saved to local context. To learn more, type in `az local-context --help`
61+
Successfully connected to server372060240.
6262
```
6363

64-
If the connection failed, try these solutions:
65-
- Check if port 5432 is open on your client machine.
64+
If the connection failed, check the following points:
6665
- if your server administrator user name and password are correct
67-
- if you have configured firewall rule for your client machine
68-
- if you've configured your server with private access in virtual networking, make sure your client machine is in the same virtual network.
66+
- if you configured firewall rule for your client machine
67+
- if your server is configured with private access with virtual networking, make sure your client machine is in the same virtual network.
6968

7069
## Run multiple queries using interactive mode
71-
You can run multiple queries using the **interactive** mode. To enable interactive mode, run the following command
70+
You can run multiple queries using the **interactive** mode. To enable interactive mode, run the following command.
7271

73-
```azurecli
74-
az postgres flexible-server connect -n <servername> -u <username> -p "<password>" -d <databasename>
72+
```azurecli-interactive
73+
az postgres flexible-server connect \
74+
-n <servername> -u <username> -p "<password>" -d <databasename> \
75+
--interactive
7576
```
7677

7778
**Example:**
7879

79-
```azurecli
80-
az postgres flexible-server connect -n postgresdemoserver -u dbuser -p "dbpassword" -d flexibleserverdb --interactive
80+
```azurecli-interactive
81+
az postgres flexible-server connect \
82+
-n server372060240 -u starchylapwing9 -p "dbpassword" -d postgres --interactive
8183
```
8284

83-
You'll see the **psql** shell experience as shown below:
85+
You see the **psql** shell experience as shown here:
8486

85-
```bash
86-
Command group 'postgres flexible-server' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
87-
Password for earthyTurtle7:
88-
Server: PostgreSQL 12.5
89-
Version: 3.0.0
90-
Chat: https://gitter.im/dbcli/pgcli
87+
```output
88+
Password for starchylapwing9:
89+
Server: PostgreSQL 13.14
90+
Version: 4.0.1
9191
Home: http://pgcli.com
92-
postgres> create database pollsdb;
93-
CREATE DATABASE
94-
Time: 0.308s
95-
postgres> exit
96-
Goodbye!
97-
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.
98-
Your preference of are now saved to local context. To learn more, type in `az local-context --help`
92+
postgres> SELECT 1;
93+
+----------+
94+
| ?column? |
95+
|----------|
96+
| 1 |
97+
+----------+
98+
SELECT 1
99+
Time: 0.167s
100+
postgres>
101+
```
102+
103+
## Execute single queries
104+
You can run single queries against Postgres database using [az postgres flexible-server execute](/cli/azure/postgres/flexible-server#az-postgres-flexible-server-execute).
105+
106+
```azurecli-interactive
107+
az postgres flexible-server execute \
108+
-n <servername> -u <username> -p "<password>" -d <databasename> \
109+
-q <querytext> --output table
99110
```
100111

101112
**Example:**
102-
```azurecli
103-
az postgres flexible-server execute -n postgresdemoserver -u dbuser -p "dbpassword" -d flexibleserverdb -q "select * from table1;" --output table
113+
```azurecli-interactive
114+
az postgres flexible-server execute \
115+
-n server372060240 -u starchylapwing9 -p "dbpassword" -d postgres \
116+
-q "SELECT 1" --output table
104117
```
105118

106-
You'll see an output as shown below:
119+
You see an output as shown here:
107120

108121
```output
109-
Command group 'postgres flexible-server' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
110-
Successfully connected to postgresdemoserver.
111-
Ran Database Query: 'select * from table1;'
122+
Successfully connected to server372060240.
123+
Ran Database Query: 'SELECT 1'
112124
Retrieving first 30 rows of query output, if applicable.
113-
Closed the connection postgresdemoserver.
114-
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.
115-
Your preference of are now saved to local context. To learn more, type in `az local-context --help`
116-
Txt Val
117-
----- -----
118-
test 200
119-
test 200
120-
test 200
121-
test 200
122-
test 200
123-
test 200
124-
test 200
125+
Closed the connection to server372060240
126+
?column?
127+
----------
128+
1
125129
```
126130

127131
## Run SQL File
128-
You can execute a sql file with the command using `--file-path` argument, `-f`.
132+
You can execute a sql file with the [az postgres flexible-server execute](/cli/azure/postgres/flexible-server#az-postgres-flexible-server-execute) command using `--file-path` argument, `-f`.
129133

130-
```azurecli
131-
az postgres flexible-server execute -n <server-name> -u <username> -p "<password>" -d <database-name> --file-path "<file-path>"
134+
```azurecli-interactive
135+
az postgres flexible-server execute \
136+
-n <server-name> -u <username> -p "<password>" -d <database-name> \
137+
--file-path "<file-path>"
132138
```
133139

134140
**Example:**
135-
```azurecli
136-
az postgres flexible-server execute -n postgresdemoserver -u dbuser -p "dbpassword" -d flexibleserverdb -f "./test.sql"
141+
Prepare a `test.sql` file. You can use the following test script with simple `SELECT` queries:
142+
143+
```sql
144+
SELECT 1;
145+
SELECT 2;
146+
SELECT 3;
147+
```
148+
149+
Save the content to the `test.sql` file in the current directory and execute using following command.
150+
151+
152+
```azurecli-interactive
153+
az postgres flexible-server execute \
154+
-n server372060240 -u starchylapwing9 -p "dbpassword" -d postgres \
155+
-f "test.sql"
137156
```
138157

139-
You'll see an output as shown below:
158+
You see an output as shown here:
140159

141160
```output
142-
Command group 'postgres flexible-server' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
143-
Running sql file '.\test.sql'...
161+
Running sql file 'test.sql'...
144162
Successfully executed the file.
145-
Closed the connection to postgresdemoserver.
163+
Closed the connection to server372060240
146164
```
147165

148166
## Next Steps

0 commit comments

Comments
 (0)