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
To use the connector: import the connector and SQLAlchemy by including the following statements at the top of your Python file:
53
-
```Python
54
-
from google.cloud.sql.connector import connector
55
-
import sqlalchemy
56
-
```
52
+
To connect to Cloud SQL using the connector, inititalize a `Connector`
53
+
object and call it's `connect` method with the proper input parameters.
57
54
58
-
The connector itself creates connection objects by calling its `connect` method but does not manage database connection pooling. For this reason, it is recommended to use the connector alongside a library that can create connection pools, such as [SQLAlchemy](https://www.sqlalchemy.org/). This will allow for connections to remain open and be reused, reducing connection overhead and the number of connections needed.
55
+
The `Connector` itself creates connection objects by calling its `connect` method but does not manage database connection pooling. For this reason, it is recommended to use the connector alongside a library that can create connection pools, such as [SQLAlchemy](https://www.sqlalchemy.org/). This will allow for connections to remain open and be reused, reducing connection overhead and the number of connections needed.
59
56
60
-
In the connector's `connect` method below, 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.
57
+
In the Connector's `connect` method below, 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.
61
58
62
59
To use this connector with SQLAlchemy, use the `creator` argument for `sqlalchemy.create_engine`:
@@ -96,15 +101,21 @@ with pool.connect() as db_conn:
96
101
print(row)
97
102
```
98
103
104
+
To close the `Connector` object's background resources, call it's `close()` method as follows:
105
+
106
+
```python
107
+
connector.close()
108
+
```
109
+
99
110
**Note**: For more examples of using SQLAlchemy to manage connection pooling with the connector, please see [Cloud SQL SQLAlchemy Samples](https://cloud.google.com/sql/docs/postgres/connect-connectors#python_1).
100
111
101
112
**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).
102
113
103
-
### Custom Connector Object
114
+
### Configuring the Connector
104
115
105
116
If you need to customize something about the connector, or want to specify
106
-
defaults for each connection to make, you can initialize a custom
107
-
`Connector` object directly:
117
+
defaults for each connection to make, you can initialize a
118
+
`Connector` object as follows:
108
119
109
120
```python
110
121
from google.cloud.sql.connector import Connector, IPTypes
@@ -118,27 +129,6 @@ connector = Connector(
118
129
)
119
130
```
120
131
121
-
You can then call the Connector object's `connect` method as you
122
-
would the default `connector.connect`:
123
-
124
-
```python
125
-
defgetconn() -> pymysql.connections.Connection:
126
-
conn = connector.connect(
127
-
"project:region:instance",
128
-
"pymysql",
129
-
user="root",
130
-
password="shhh",
131
-
db="your-db-name"
132
-
)
133
-
return conn
134
-
```
135
-
136
-
To close the `Connector` object's background resources, call it's `close()` method as follows:
137
-
138
-
```python
139
-
connector.close()
140
-
```
141
-
142
132
### Using Connector as a Context Manager
143
133
144
134
The `Connector` object can also be used as a context manager in order to
0 commit comments