@@ -32,6 +32,7 @@ async def create_sqlalchemy_engine(
3232    user : str ,
3333    password : str ,
3434    db : str ,
35+     ip_type : str  =  "public" ,
3536    refresh_strategy : str  =  "background" ,
3637    resolver : Union [type [DefaultResolver ], type [DnsResolver ]] =  DefaultResolver ,
3738) ->  tuple [sqlalchemy .ext .asyncio .engine .AsyncEngine , Connector ]:
@@ -63,6 +64,9 @@ async def create_sqlalchemy_engine(
6364            The database user's password, e.g., secret-password 
6465        db (str): 
6566            The name of the database, e.g., mydb 
67+         ip_type (str): 
68+             The IP type of the Cloud SQL instance to connect to. Can be one 
69+             of "public", "private", or "psc". 
6670        refresh_strategy (Optional[str]): 
6771            Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" 
6872            or "background". For serverless environments use "lazy" to avoid 
@@ -87,7 +91,7 @@ async def create_sqlalchemy_engine(
8791            user = user ,
8892            password = password ,
8993            db = db ,
90-             ip_type = "public" ,  # can also be  "private" or "psc" 
94+             ip_type = ip_type ,  # can be "public",  "private" or "psc" 
9195        ),
9296        execution_options = {"isolation_level" : "AUTOCOMMIT" },
9397    )
@@ -99,6 +103,7 @@ async def create_asyncpg_pool(
99103    user : str ,
100104    password : str ,
101105    db : str ,
106+     ip_type : str  =  "public" ,
102107    refresh_strategy : str  =  "background" ,
103108) ->  tuple [asyncpg .Pool , Connector ]:
104109    """Creates a native asyncpg connection pool for a Cloud SQL instance and 
@@ -128,6 +133,9 @@ async def create_asyncpg_pool(
128133            The database user's password, e.g., secret-password 
129134        db (str): 
130135            The name of the database, e.g., mydb 
136+         ip_type (str): 
137+             The IP type of the Cloud SQL instance to connect to. Can be one 
138+             of "public", "private", or "psc". 
131139        refresh_strategy (Optional[str]): 
132140            Refresh strategy for the Cloud SQL Connector. Can be one of "lazy" 
133141            or "background". For serverless environments use "lazy" to avoid 
@@ -145,7 +153,7 @@ async def getconn(
145153            user = user ,
146154            password = password ,
147155            db = db ,
148-             ip_type = "public" ,  # can also  be "private" or "psc",  
156+             ip_type = ip_type ,  # can be "public", " private" or "psc" 
149157            ** kwargs ,
150158        )
151159        return  conn 
@@ -161,8 +169,11 @@ async def test_sqlalchemy_connection_with_asyncpg() -> None:
161169    user  =  os .environ ["POSTGRES_USER" ]
162170    password  =  os .environ ["POSTGRES_PASS" ]
163171    db  =  os .environ ["POSTGRES_DB" ]
172+     ip_type  =  os .environ .get ("IP_TYPE" , "public" )
164173
165-     pool , connector  =  await  create_sqlalchemy_engine (inst_conn_name , user , password , db )
174+     pool , connector  =  await  create_sqlalchemy_engine (
175+         inst_conn_name , user , password , db , ip_type 
176+     )
166177
167178    async  with  pool .connect () as  conn :
168179        res  =  (await  conn .execute (sqlalchemy .text ("SELECT 1" ))).fetchone ()
@@ -177,9 +188,10 @@ async def test_lazy_sqlalchemy_connection_with_asyncpg() -> None:
177188    user  =  os .environ ["POSTGRES_USER" ]
178189    password  =  os .environ ["POSTGRES_PASS" ]
179190    db  =  os .environ ["POSTGRES_DB" ]
191+     ip_type  =  os .environ .get ("IP_TYPE" , "public" )
180192
181193    pool , connector  =  await  create_sqlalchemy_engine (
182-         inst_conn_name , user , password , db , "lazy" 
194+         inst_conn_name , user , password , db , ip_type ,  "lazy" 
183195    )
184196
185197    async  with  pool .connect () as  conn :
@@ -195,9 +207,10 @@ async def test_custom_SAN_with_dns_sqlalchemy_connection_with_asyncpg() -> None:
195207    user  =  os .environ ["POSTGRES_USER" ]
196208    password  =  os .environ ["POSTGRES_CUSTOMER_CAS_PASS" ]
197209    db  =  os .environ ["POSTGRES_DB" ]
210+     ip_type  =  os .environ .get ("IP_TYPE" , "public" )
198211
199212    pool , connector  =  await  create_sqlalchemy_engine (
200-         inst_conn_name , user , password , db , resolver = DnsResolver 
213+         inst_conn_name , user , password , db , ip_type ,  resolver = DnsResolver 
201214    )
202215
203216    async  with  pool .connect () as  conn :
@@ -213,8 +226,11 @@ async def test_connection_with_asyncpg() -> None:
213226    user  =  os .environ ["POSTGRES_USER" ]
214227    password  =  os .environ ["POSTGRES_PASS" ]
215228    db  =  os .environ ["POSTGRES_DB" ]
229+     ip_type  =  os .environ .get ("IP_TYPE" , "public" )
216230
217-     pool , connector  =  await  create_asyncpg_pool (inst_conn_name , user , password , db )
231+     pool , connector  =  await  create_asyncpg_pool (
232+         inst_conn_name , user , password , db , ip_type 
233+     )
218234
219235    async  with  pool .acquire () as  conn :
220236        res  =  await  conn .fetch ("SELECT 1" )
@@ -229,9 +245,10 @@ async def test_lazy_connection_with_asyncpg() -> None:
229245    user  =  os .environ ["POSTGRES_USER" ]
230246    password  =  os .environ ["POSTGRES_PASS" ]
231247    db  =  os .environ ["POSTGRES_DB" ]
248+     ip_type  =  os .environ .get ("IP_TYPE" , "public" )
232249
233250    pool , connector  =  await  create_asyncpg_pool (
234-         inst_conn_name , user , password , db , "lazy" 
251+         inst_conn_name , user , password , db , ip_type ,  "lazy" 
235252    )
236253
237254    async  with  pool .acquire () as  conn :
0 commit comments