Skip to content

Commit 222c714

Browse files
authored
chore: Add better connect instructions and IAM DB authN to README (#105)
* chore: add info IAM auth and using DB-API connection object to README * Update README.md * Update README.md
1 parent 0a37afa commit 222c714

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

README.md

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,79 @@ from google.cloud.sql.connector import connector
5353
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.
5454

5555
```
56-
connector.connect(
57-
"your:connection:string:",
56+
conn = connector.connect(
57+
"project:region:instance",
5858
"pymysql",
5959
user="root",
6060
password="shhh",
6161
db="your-db-name"
6262
... insert other kwargs ...
6363
)
6464
```
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`:
81+
```
82+
def getconn() -> pymysql.connections.Connection:
83+
conn: pymysql.connections.Connection = connector.connect(
84+
"project:region:instance",
85+
"pymysql",
86+
user="root",
87+
password="shhh",
88+
db="your-db-name"
89+
)
90+
return conn
91+
92+
engine = sqlalchemy.create_engine(
93+
"mysql+pymysql://",
94+
creator=getconn,
95+
)
96+
```
97+
6598
**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).
6699

67100
### Specifying Public or Private IP
68101
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`.
69102
Example:
70103
```
71104
connector.connect(
72-
"your:connection:string:",
105+
"project:region:instance",
73106
"pymysql",
74107
ip_types=IPTypes.PRIVATE # Prefer private IP
75108
... insert other kwargs ...
76109
)
77110
```
78111

79112
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.
119+
Example:
120+
```
121+
connector.connect(
122+
"project:region:instance",
123+
"pg8000",
124+
125+
db="my_database",
126+
enable_iam_auth=True,
127+
)
128+
```
80129
### Setup for development
81130

82131
Tests can be run with `nox`. Change directory into the `cloud-sql-python-connector` and just run `nox` to run the tests.

0 commit comments

Comments
 (0)