2525import mysql .connector
2626import psycopg2
2727import psycopg2 .extras
28+ import psycopg
29+ from psycopg import sql
2830
2931# Configure logging
3032logging .basicConfig (
@@ -93,6 +95,29 @@ def connect(self, driver_type: str, timezone: Optional[str] = None):
9395 with self .conn .cursor () as cursor :
9496 cursor .execute (f"SET TIME ZONE = '{ timezone } '" )
9597 self .conn .commit ()
98+
99+ elif driver_type == "postgresql3" :
100+ host = self ._get_env ("POSTGRES_HOST" , "127.0.0.1" )
101+ port = int (self ._get_env ("POSTGRES_PORT" , "4003" ))
102+ db = self ._get_env ("DB_NAME" , "public" )
103+
104+ logger .info (
105+ f"Connecting to PostgreSQL (psycopg3): { host } :{ port } /{ db } (timezone={ timezone } )"
106+ )
107+ self .conn = psycopg .connect (
108+ host = host ,
109+ port = port ,
110+ dbname = db ,
111+ user = username ,
112+ password = password ,
113+ )
114+
115+ if timezone :
116+ with self .conn .cursor () as cursor :
117+ cursor .execute (
118+ sql .SQL ("SET TIME ZONE = {}" ).format (sql .Literal (timezone ))
119+ )
120+ self .conn .commit ()
96121 else :
97122 raise ValueError (f"Unknown driver: { driver_type } " )
98123
@@ -146,15 +171,16 @@ def format_timestamp_as_utc(self, ts) -> str:
146171
147172 def get_cursor (self , prepared = False ):
148173 """Get cursor. Use prepared=True for MySQL parameterized queries."""
149- return (
150- self .conn .cursor (prepared = True )
151- if self .driver == "mysql" and prepared
152- else self .conn .cursor ()
153- )
174+ if self .driver == "mysql" and prepared :
175+ return self .conn .cursor (prepared = True )
176+ elif self .driver == "postgresql3" :
177+ return self .conn .cursor ()
178+ else :
179+ return self .conn .cursor ()
154180
155181 def parse_binary_result (self , value , driver ):
156182 """Parse binary data. NOTE: PostgreSQL returns hex string format."""
157- if driver == "postgresql" :
183+ if driver in ( "postgresql" , "postgresql3" ) :
158184 if isinstance (value , memoryview ):
159185 value = bytes (value )
160186 if isinstance (value , bytes ):
@@ -176,7 +202,7 @@ def test_instance():
176202 instance .teardown ()
177203
178204
179- @pytest .mark .parametrize ("driver" , ["mysql" , "postgresql" ])
205+ @pytest .mark .parametrize ("driver" , ["mysql" , "postgresql" , "postgresql3" ])
180206def test_crud_operations (test_instance , driver ):
181207 """
182208 Test comprehensive CRUD operations on a single table with all supported GreptimeDB data types.
@@ -530,7 +556,7 @@ def test_timezone_insert_and_select(test_instance, driver):
530556 raise
531557
532558
533- @pytest .mark .parametrize ("driver" , ["mysql" , "postgresql" ])
559+ @pytest .mark .parametrize ("driver" , ["mysql" , "postgresql" , "postgresql3" ])
534560def test_batch_insert (test_instance , driver ):
535561 """
536562 Test batch insert using executemany().
0 commit comments