@@ -24,6 +24,7 @@ The Cloud SQL Python Connector is a package to be used alongside a database driv
24
24
Currently supported drivers are:
25
25
- [ ` pymysql ` ] ( https://github.com/PyMySQL/PyMySQL ) (MySQL)
26
26
- [ ` pg8000 ` ] ( https://github.com/tlocke/pg8000 ) (PostgreSQL)
27
+ - [ ` asyncpg ` ] ( https://github.com/MagicStack/asyncpg ) (PostgreSQL)
27
28
- [ ` pytds ` ] ( https://github.com/denisenkom/pytds ) (SQL Server)
28
29
29
30
@@ -37,9 +38,16 @@ based on your database dialect.
37
38
pip install "cloud-sql-python-connector[pymysql]"
38
39
```
39
40
### Postgres
41
+ There are two different database drivers that are supported for the Postgres dialect:
42
+
43
+ #### pg8000
40
44
```
41
45
pip install "cloud-sql-python-connector[pg8000]"
42
46
```
47
+ #### asyncpg
48
+ ```
49
+ pip install "cloud-sql-python-connector[asyncpg]"
50
+ ```
43
51
### SQL Server
44
52
```
45
53
pip install "cloud-sql-python-connector[pytds]"
@@ -111,9 +119,9 @@ def getconn() -> pymysql.connections.Connection:
111
119
conn: pymysql.connections.Connection = connector.connect(
112
120
" project:region:instance" ,
113
121
" pymysql" ,
114
- user = " root " ,
115
- password = " shhh " ,
116
- db = " your -db-name"
122
+ user = " my-user " ,
123
+ password = " my-password " ,
124
+ db = " my -db-name"
117
125
)
118
126
return conn
119
127
@@ -188,9 +196,9 @@ def getconn() -> pymysql.connections.Connection:
188
196
conn = connector.connect(
189
197
" project:region:instance" ,
190
198
" pymysql" ,
191
- user = " root " ,
192
- password = " shhh " ,
193
- db = " your -db-name"
199
+ user = " my-user " ,
200
+ password = " my-password " ,
201
+ db = " my -db-name"
194
202
)
195
203
return conn
196
204
@@ -245,7 +253,7 @@ connector.connect(
245
253
" project:region:instance" ,
246
254
" pg8000" ,
247
255
248
- db = " my_database " ,
256
+ db = " my-db-name " ,
249
257
enable_iam_auth = True ,
250
258
)
251
259
```
@@ -258,7 +266,7 @@ Once you have followed the steps linked above, you can run the following code to
258
266
connector.connect(
259
267
" project:region:instance" ,
260
268
" pytds" ,
261
- db = " my_database " ,
269
+ db = " my-db-name " ,
262
270
active_directory_auth = True ,
263
271
server_name = " public.[instance].[location].[project].cloudsql.[domain]" ,
264
272
)
@@ -268,13 +276,111 @@ Or, if using Private IP:
268
276
connector.connect(
269
277
" project:region:instance" ,
270
278
" pytds" ,
271
- db = " my_database " ,
279
+ db = " my-db-name " ,
272
280
active_directory_auth = True ,
273
281
server_name = " private.[instance].[location].[project].cloudsql.[domain]" ,
274
282
ip_type = IPTypes.PRIVATE
275
283
)
276
284
```
277
285
286
+ ### Async Driver Usage
287
+ The Cloud SQL Connector is compatible with
288
+ [ asyncio] ( https://docs.python.org/3/library/asyncio.html ) to improve the speed
289
+ and efficiency of database connections through concurrency. You can use all
290
+ non-asyncio drivers through the ` Connector.connect_async ` function, in addition
291
+ to the following asyncio database drivers:
292
+ - [ asyncpg] ( https://magicstack.github.io/asyncpg ) (Postgres)
293
+
294
+ The Cloud SQL Connector has a helper ` create_async_connector ` function that is
295
+ recommended for asyncio database connections. It returns a ` Connector `
296
+ object that uses the current thread's running event loop. This is different
297
+ than ` Connector() ` which by default initializes a new event loop in a
298
+ background thread.
299
+
300
+ The ` create_async_connector ` allows all the same input arguments as the
301
+ [ Connector] ( #configuring-the-connector ) object.
302
+
303
+ Once a ` Connector ` object is returned by ` create_async_connector ` you can call
304
+ its ` connect_async ` method, just as you would the ` connect ` method:
305
+
306
+ ``` python
307
+ import asyncpg
308
+ from google.cloud.sql.connector import create_async_connector
309
+
310
+ async def main ():
311
+ # intialize Connector object using 'create_async_connector'
312
+ connector = await create_async_connector()
313
+
314
+ # create connection to Cloud SQL database
315
+ conn: asyncpg.Connection = await connector.connect_async(
316
+ " project:region:instance" , # Cloud SQL instance connection name
317
+ " asyncpg" ,
318
+ user = " my-user" ,
319
+ password = " my-password" ,
320
+ db = " my-db-name"
321
+ # ... additional database driver args
322
+ )
323
+
324
+ # insert into Cloud SQL database (example)
325
+ await conn.execute(" INSERT INTO ratings (title, genre, rating) VALUES ('Batman', 'Action', 8.2)" )
326
+
327
+ # query Cloud SQL database (example)
328
+ results = await conn.fetch(" SELECT * from ratings" )
329
+ for row in results:
330
+ # ... do something with results
331
+
332
+ # close asyncpg connection
333
+ await conn.close
334
+
335
+ # close Cloud SQL Connector
336
+ await connector.close_async()
337
+ ```
338
+
339
+ For more details on interacting with an ` asyncpg.Connection ` , please visit
340
+ the [ official documentation] ( https://magicstack.github.io/asyncpg/current/api/index.html ) .
341
+
342
+ ### Async Context Manager
343
+
344
+ An alternative to using the ` create_async_connector ` function is initializing
345
+ a ` Connector ` as an async context manager, removing the need for explicit
346
+ calls to ` connector.close_async() ` to cleanup resources.
347
+
348
+ ** Note:** This alternative requires that the running event loop be
349
+ passed in as the ` loop ` argument to ` Connector() ` .
350
+
351
+ ``` python
352
+ import asyncio
353
+ import asyncpg
354
+ from google.cloud.sql.connector import Connector
355
+
356
+ async def main ():
357
+ # get current running event loop to be used with Connector
358
+ loop = asyncio.get_running_loop()
359
+ # intialize Connector object as async context manager
360
+ async with Connector(loop = loop) as connector:
361
+
362
+ # create connection to Cloud SQL database
363
+ conn: asyncpg.Connection = await connector.connect_async(
364
+ " project:region:instance" , # Cloud SQL instance connection name
365
+ " asyncpg" ,
366
+ user = " my-user" ,
367
+ password = " my-password" ,
368
+ db = " my-db-name"
369
+ # ... additional database driver args
370
+ )
371
+
372
+ # insert into Cloud SQL database (example)
373
+ await conn.execute(" INSERT INTO ratings (title, genre, rating) VALUES ('Batman', 'Action', 8.2)" )
374
+
375
+ # query Cloud SQL database (example)
376
+ results = await conn.fetch(" SELECT * from ratings" )
377
+ for row in results:
378
+ # ... do something with results
379
+
380
+ # close asyncpg connection
381
+ await conn.close
382
+ ```
383
+
278
384
## Support policy
279
385
280
386
### Major version lifecycle
0 commit comments