We currently use a get_connection
functionality for our postgres via
import psycopg2
def get_db_connection(conn_str: str = '') -> psycopg2._psycopg.connection:
conn_str = conn_str if conn_str else os.environ.get(PG_CONNECTION_STRING)
return psycopg2.connect(conn_str)
Now that we're working with pandas dataframes, and we want to write them to the DB as new tables, I see in this answer and the pandas docs that you can use an sqlalchemy engine or connection.
df.to_sql(con=sqlalchemy_cnx, 'table_name')
Is there a relationship between a psycopg2 connection and an sqlalchemy connection such that I could create the latter from the former?
Something like
get_pandas_compliant_db_cnx(cnx: psycopg2._psycopg.connection) -> sqlalchemy.engine.Connection:
# this is what i'm trying to implement to avoid managing both systems