1515from awswrangler ._config import apply_configs
1616
1717pg8000 = _utils .import_optional_dependency ("pg8000" )
18+ pg8000_native = _utils .import_optional_dependency ("pg8000.native" )
1819
1920_logger : logging .Logger = logging .getLogger (__name__ )
2021
@@ -29,18 +30,18 @@ def _validate_connection(con: "pg8000.Connection") -> None:
2930
3031
3132def _drop_table (cursor : "pg8000.Cursor" , schema : Optional [str ], table : str ) -> None :
32- schema_str = f'" { schema } ".' if schema else ""
33- sql = f' DROP TABLE IF EXISTS { schema_str } " { table } "'
33+ schema_str = f" { pg8000_native . identifier ( schema ) } ." if schema else ""
34+ sql = f" DROP TABLE IF EXISTS { schema_str } { pg8000_native . identifier ( table ) } "
3435 _logger .debug ("Drop table query:\n %s" , sql )
3536 cursor .execute (sql )
3637
3738
3839def _does_table_exist (cursor : "pg8000.Cursor" , schema : Optional [str ], table : str ) -> bool :
39- schema_str = f"TABLE_SCHEMA = ' { schema } ' AND" if schema else ""
40+ schema_str = f"TABLE_SCHEMA = { pg8000_native . literal ( schema ) } AND" if schema else ""
4041 cursor .execute (
4142 f"SELECT true WHERE EXISTS ("
4243 f"SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE "
43- f"{ schema_str } TABLE_NAME = ' { table } ' "
44+ f"{ schema_str } TABLE_NAME = { pg8000_native . literal ( table ) } "
4445 f");"
4546 )
4647 return len (cursor .fetchall ()) > 0
@@ -69,7 +70,7 @@ def _create_table(
6970 converter_func = _data_types .pyarrow2postgresql ,
7071 )
7172 cols_str : str = "" .join ([f'"{ k } " { v } ,\n ' for k , v in postgresql_types .items ()])[:- 2 ]
72- sql = f' CREATE TABLE IF NOT EXISTS " { schema } "." { table } " (\n { cols_str } )'
73+ sql = f" CREATE TABLE IF NOT EXISTS { pg8000_native . identifier ( schema ) } . { pg8000_native . identifier ( table ) } (\n { cols_str } )"
7374 _logger .debug ("Create table query:\n %s" , sql )
7475 cursor .execute (sql )
7576
@@ -94,12 +95,16 @@ def _iterate_server_side_cursor(
9495 """
9596 with con .cursor () as cursor :
9697 sscursor_name : str = f"c_{ uuid .uuid4 ().hex } "
97- cursor_args = _db_utils ._convert_params (f"DECLARE { sscursor_name } CURSOR FOR { sql } " , params )
98+ cursor_args = _db_utils ._convert_params (
99+ f"DECLARE { pg8000_native .identifier (sscursor_name )} CURSOR FOR { sql } " , params
100+ )
98101 cursor .execute (* cursor_args )
99102
100103 try :
101104 while True :
102- cursor .execute (f"FETCH FORWARD { chunksize } FROM { sscursor_name } " )
105+ cursor .execute (
106+ f"FETCH FORWARD { pg8000_native .literal (chunksize )} FROM { pg8000_native .identifier (sscursor_name )} "
107+ )
103108 records = cursor .fetchall ()
104109
105110 if not records :
@@ -115,7 +120,7 @@ def _iterate_server_side_cursor(
115120 dtype_backend = dtype_backend ,
116121 )
117122 finally :
118- cursor .execute (f"CLOSE { sscursor_name } " )
123+ cursor .execute (f"CLOSE { pg8000_native . identifier ( sscursor_name ) } " )
119124
120125
121126@_utils .check_optional_dependency (pg8000 , "pg8000" )
@@ -458,7 +463,11 @@ def read_sql_table(
458463 >>> con.close()
459464
460465 """
461- sql : str = f'SELECT * FROM "{ table } "' if schema is None else f'SELECT * FROM "{ schema } "."{ table } "'
466+ sql : str = (
467+ f"SELECT * FROM { pg8000_native .identifier (table )} "
468+ if schema is None
469+ else f"SELECT * FROM { pg8000_native .identifier (schema )} .{ pg8000_native .identifier (table )} "
470+ )
462471 return read_sql_query (
463472 sql = sql ,
464473 con = con ,
@@ -591,7 +600,7 @@ def to_sql(
591600 df = df , column_placeholders = column_placeholders , chunksize = chunksize
592601 )
593602 for placeholders , parameters in placeholder_parameter_pair_generator :
594- sql : str = f' INSERT INTO " { schema } "." { table } " { insertion_columns } VALUES { placeholders } { upsert_str } '
603+ sql : str = f" INSERT INTO { pg8000_native . identifier ( schema ) } . { pg8000_native . identifier ( table ) } { insertion_columns } VALUES { placeholders } { upsert_str } "
595604 _logger .debug ("sql: %s" , sql )
596605 cursor .executemany (sql , (parameters ,))
597606 con .commit ()
0 commit comments