You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+52-3Lines changed: 52 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,30 +53,79 @@ from google.cloud.sql.connector import connector
53
53
Use the connector to create a connection object by calling the connect method. Input your connection string as the first positional argument and the name of the database driver for the second positional argument. Insert the rest of your connection keyword arguments like user, password and database. You can also set the optional `timeout` or `ip_type` keyword arguments.
54
54
55
55
```
56
-
connector.connect(
57
-
"your:connection:string:",
56
+
conn = connector.connect(
57
+
"project:region:instance",
58
58
"pymysql",
59
59
user="root",
60
60
password="shhh",
61
61
db="your-db-name"
62
62
... insert other kwargs ...
63
63
)
64
64
```
65
+
66
+
The returned DB-API 2.0 compliant connection object can then be used to query and modify the database:
67
+
```
68
+
# Execute a query
69
+
cursor = conn.cursor()
70
+
cursor.execute("SELECT * from my_table")
71
+
72
+
# Fetch the results
73
+
result = cursor.fetchall()
74
+
75
+
# Do something with the results
76
+
for row in result:
77
+
print(row)
78
+
```
79
+
80
+
To use this connector with SQLAlchemy, use the `creator` argument for `sqlalchemy.create_engine`:
**Note for SQL Server users**: If your SQL Server instance requires SSL, you need to download the CA certificate for your instance and include `cafile={path to downloaded certificate}` and `validate_host=False`. This is a workaround for a [known issue](https://issuetracker.google.com/184867147).
66
99
67
100
### Specifying Public or Private IP
68
101
The Cloud SQL Connector for Python can be used to connect to Cloud SQL instances using both public and private IP addresses. To specify which IP address to use to connect, set the `ip_type` keyword argument Possible values are `IPTypes.PUBLIC` and `IPTypes.PRIVATE`.
69
102
Example:
70
103
```
71
104
connector.connect(
72
-
"your:connection:string:",
105
+
"project:region:instance",
73
106
"pymysql",
74
107
ip_types=IPTypes.PRIVATE # Prefer private IP
75
108
... insert other kwargs ...
76
109
)
77
110
```
78
111
79
112
Note: If specifying Private IP, your application must already be in the same VPC network as your Cloud SQL Instance.
113
+
114
+
### IAM Authentication
115
+
Connections using [IAM database authentication](https://cloud.google.com/sql/docs/postgres/iam-logins) are supported when using the Postgres driver.
116
+
First, make sure to [configure your Cloud SQL Instance to allow IAM authentication](https://cloud.google.com/sql/docs/postgres/create-edit-iam-instances#configure-iam-db-instance) and [add an IAM database user](https://cloud.google.com/sql/docs/postgres/create-manage-iam-users#creating-a-database-user).
117
+
Now, you can connect using user or service account credentials instead of a password.
118
+
In the call to connect, set the `enable_iam_auth` keyword argument to true and `user` to the email address associated with your IAM user.
0 commit comments