Skip to content

Commit b504b29

Browse files
Merge pull request #269244 from JimacoMS4/update-postgresql-connect-python-quickstart
Remove markdown include statements from code and also minor formatting.
2 parents c4c3655 + 04c9a09 commit b504b29

File tree

1 file changed

+26
-38
lines changed

1 file changed

+26
-38
lines changed

articles/postgresql/flexible-server/connect-python.md

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@ This article assumes that you're familiar with developing using Python, but you'
2727
* Latest [pip](https://pip.pypa.io/en/stable/installing/) package installer.
2828

2929
## Preparing your client workstation
30+
3031
- If you created your Azure Database for PostgreSQL flexible server instance with *Private access (VNet Integration)*, you will need to connect to your server from a resource within the same VNet as your server. You can create a virtual machine and add it to the VNet created with your Azure Database for PostgreSQL flexible server instance. Refer to [Create and manage Azure Database for PostgreSQL - Flexible Server virtual network using Azure CLI](./how-to-manage-virtual-network-cli.md).
3132
- If you created your Azure Database for PostgreSQL flexible server instance with *Public access (allowed IP addresses)*, you can add your local IP address to the list of firewall rules on your server. Refer to [Create and manage Azure Database for PostgreSQL - Flexible Server firewall rules using the Azure CLI](./how-to-manage-firewall-cli.md).
3233

3334
## Install the Python libraries for PostgreSQL
35+
3436
The [psycopg2](https://pypi.python.org/pypi/psycopg2/) module enables connecting to and querying a PostgreSQL database, and is available as a Linux, macOS, or Windows [wheel](https://pythonwheels.com/) package. Install the binary version of the module, including all the dependencies.
3537

3638
To install `psycopg2`, open a terminal or command prompt and run the command `pip install psycopg2`.
3739

3840
## Get database connection information
41+
3942
Connecting to an Azure Database for PostgreSQL flexible server instance requires the fully qualified server name and login credentials. You can get this information from the Azure portal.
4043

4144
1. In the [Azure portal](https://portal.azure.com/), search for and select your Azure Database for PostgreSQL flexible server name.
@@ -65,41 +68,35 @@ The following code example connects to your Azure Database for PostgreSQL flexib
6568

6669
```Python
6770
import psycopg2
68-
# Update connection string information
6971

70-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
72+
# Update connection string information
7173
host = "<server-name>"
7274
dbname = "<database-name>"
7375
user = "<admin-username>"
7476
password = "<admin-password>"
7577
sslmode = "require"
76-
# Construct connection string
7778

78-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
79+
# Construct connection string
7980
conn_string = "host={0} user={1} dbname={2} password={3} sslmode={4}".format(host, user, dbname, password, sslmode)
8081
conn = psycopg2.connect(conn_string)
8182
print("Connection established")
8283
cursor = conn.cursor()
83-
# Drop previous table of same name if one exists
8484

85-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
85+
# Drop previous table of same name if one exists
8686
cursor.execute("DROP TABLE IF EXISTS inventory;")
8787
print("Finished dropping table (if existed)")
88-
# Create a table
8988

90-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
89+
# Create a table
9190
cursor.execute("CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);")
9291
print("Finished creating table")
93-
# Insert some data into the table
9492

95-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
93+
# Insert some data into the table
9694
cursor.execute("INSERT INTO inventory (name, quantity) VALUES (%s, %s);", ("banana", 150))
9795
cursor.execute("INSERT INTO inventory (name, quantity) VALUES (%s, %s);", ("orange", 154))
9896
cursor.execute("INSERT INTO inventory (name, quantity) VALUES (%s, %s);", ("apple", 100))
9997
print("Inserted 3 rows of data")
100-
# Clean up
10198

102-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
99+
# Clean up
103100
conn.commit()
104101
cursor.close()
105102
conn.close()
@@ -110,109 +107,100 @@ When the code runs successfully, it produces the following output:
110107
![Command-line output](media/connect-python/2-example-python-output.png)
111108

112109
## Read data
110+
113111
The following code example connects to your Azure Database for PostgreSQL flexible server database and uses cursor.execute with the SQL **SELECT** statement to read data. This function accepts a query and returns a result set to iterate over by using cursor.fetchall()
114112

115113
```Python
116114
import psycopg2
117-
# Update connection string information
118115

119-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
116+
# Update connection string information
120117
host = "<server-name>"
121118
dbname = "<database-name>"
122119
user = "<admin-username>"
123120
password = "<admin-password>"
124121
sslmode = "require"
125-
# Construct connection string
126122

127-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
123+
# Construct connection string
128124
conn_string = "host={0} user={1} dbname={2} password={3} sslmode={4}".format(host, user, dbname, password, sslmode)
129125
conn = psycopg2.connect(conn_string)
130126
print("Connection established")
131127
cursor = conn.cursor()
132-
# Fetch all rows from table
133128

134-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
129+
# Fetch all rows from table
135130
cursor.execute("SELECT * FROM inventory;")
136131
rows = cursor.fetchall()
137-
# Print all rows
138132

139-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
133+
# Print all rows
140134
for row in rows:
141135
print("Data row = (%s, %s, %s)" %(str(row[0]), str(row[1]), str(row[2])))
142-
# Cleanup
143136

144-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
137+
# Cleanup
145138
conn.commit()
146139
cursor.close()
147140
conn.close()
148141
```
149142

150143
## Update data
144+
151145
The following code example connects to your Azure Database for PostgreSQL flexible server database and uses cursor.execute with the SQL **UPDATE** statement to update data.
152146

153147
```Python
154148
import psycopg2
155-
# Update connection string information
156149

157-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
150+
# Update connection string information
158151
host = "<server-name>"
159152
dbname = "<database-name>"
160153
user = "<admin-username>"
161154
password = "<admin-password>"
162155
sslmode = "require"
163-
# Construct connection string
164156

165-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
157+
# Construct connection string
166158
conn_string = "host={0} user={1} dbname={2} password={3} sslmode={4}".format(host, user, dbname, password, sslmode)
167159
conn = psycopg2.connect(conn_string)
168160
print("Connection established")
169161
cursor = conn.cursor()
170-
# Update a data row in the table
171162

172-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
163+
# Update a data row in the table
173164
cursor.execute("UPDATE inventory SET quantity = %s WHERE name = %s;", (200, "banana"))
174165
print("Updated 1 row of data")
175-
# Cleanup
176166

177-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
167+
# Cleanup
178168
conn.commit()
179169
cursor.close()
180170
conn.close()
181171
```
182172

183173
## Delete data
174+
184175
The following code example connects to your Azure Database for PostgreSQL flexible server database and uses cursor.execute with the SQL **DELETE** statement to delete an inventory item that you previously inserted.
185176

186177
```Python
187178
import psycopg2
188-
# Update connection string information
189179

190-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
180+
# Update connection string information
191181
host = "<server-name>"
192182
dbname = "<database-name>"
193183
user = "<admin-username>"
194184
password = "<admin-password>"
195185
sslmode = "require"
196-
# Construct connection string
197186

198-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
187+
# Construct connection string
199188
conn_string = "host={0} user={1} dbname={2} password={3} sslmode={4}".format(host, user, dbname, password, sslmode)
200189
conn = psycopg2.connect(conn_string)
201190
print("Connection established")
202191
cursor = conn.cursor()
203-
# Delete data row from table
204192

205-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
193+
# Delete data row from table
206194
cursor.execute("DELETE FROM inventory WHERE name = %s;", ("orange",))
207195
print("Deleted 1 row of data")
208-
# Cleanup
209196

210-
[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)]
197+
# Cleanup
211198
conn.commit()
212199
cursor.close()
213200
conn.close()
214201
```
215202

216203
## Next steps
204+
217205
> [!div class="nextstepaction"]
218206
> [Migrate your database using dump and restore](../howto-migrate-using-dump-and-restore.md)

0 commit comments

Comments
 (0)