Writing JSON column to Postgres using Pandas .to_sql

前端 未结 3 1592
半阙折子戏
半阙折子戏 2021-02-07 05:51

During an ETL process I needed to extract and load a JSON column from one Postgres database to another. We use Pandas for this since it has so many ways to read and write data f

3条回答
  •  青春惊慌失措
    2021-02-07 06:14

    I've been searching the web for a solution but couldn't find any so here is what we came up with (there might be better ways but at least this is a start if someone else runs into this).

    Specify the dtype parameter in to_sql.

    We went from:df.to_sql(table_name, analytics_db) to df.to_sql(table_name, analytics_db, dtype={'name_of_json_column_in_source_table': sqlalchemy.types.JSON}) and it just works.

提交回复
热议问题