从 python 批量插入 postgres table

Bulk insert into postgres table from python

我想将 insert/update 来自 pandas 数据帧的值转换为 postgres table。 我在 postgres table 中有一个独特的元组 (a,b)。如果元组已经存在我只想更新第三个值c,如果元组不存在我想创建一个三元组(a,b,c)。

最有效的方法是什么?我猜想是某种批量插入,但我不太确定具体如何。

您可以将数据帧转换为 CTE https://www.postgresql.org/docs/current/queries-with.html,然后将 CTE 中的数据插入 table。像这样:

def convert_df_to_cte(df):
    vals = ', \n'.join([f"{tuple([f'$str${e}$str$' for e in row])}" for row in df.values])
    vals = vals.replace("'$str$", "$str$")
    vals = vals.replace("$str$'", "$str$")
    vals = vals.replace('"$str$', "$str$")
    vals = vals.replace('$str$"', "$str$")
    vals = vals.replace('$str$nan$str$', 'NULL')

    columns = ', \n'.join(df.columns)

    sql = f"""
    WITH vals AS (
        SELECT 
            {columns}
        FROM 
            (VALUES {vals}) AS t ({columns})
    )
    """
    return sql


df = pd.DataFrame([[1, 2, 3]], columns=['col_1', 'col_2', 'col_3'])

cte_sql = convert_df_to_cte(df)
sql_to_insert = f"""
{cte_sql}

INSERT INTO schema.table (col_1, col_2, col_3)
SELECT 
    col_1::integer, -- don't forget to cast to right type to avoid errors
    col_2::integer, -- don't forget to cast to right type to avoid errors
    col_3::character varying
FROM 
    vals
ON CONFLICT (col_1, col_2) DO UPDATE SET
    col_3 = excluded.col_3;
"""

run_sql(sql)