Re: Testing of a fast method to bulk insert a Pandas DataFrame into Postgres

From: Rob Sargent <robjsargent(at)gmail(dot)com>
To: Shaozhong SHI <shishaozhong(at)gmail(dot)com>
Cc: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>, pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Testing of a fast method to bulk insert a Pandas DataFrame into Postgres
Date: 2021-10-04 16:25:12
Message-ID: 3D84E92F-28DF-460C-90C7-160F41BE8B11@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

> On Oct 4, 2021, at 10:20 AM, Shaozhong SHI <shishaozhong(at)gmail(dot)com> wrote:
>
> Hello, Adrian Klaver,
>
> Pandas version is 0.23.0.
>
> I used the following code:
>
> def psql_insert_copy(table, conn, keys, data_iter):
> # gets a DBAPI connection that can provide a cursor
> dbapi_conn = conn.connection
> with dbapi_conn.cursor() as cur:
> s_buf = StringIO()
> writer = csv.writer(s_buf)
> writer.writerows(data_iter)
> s_buf.seek(0)
>
> columns = ', '.join('"{}"'.format(k) for k in keys)
> if table.schema:
> table_name = '{}.{}'.format(table.schema, table.name <http://table.name/>)
> else:
> table_name = table.name <http://table.name/>
>
> sql = 'COPY {} ({}) FROM STDIN WITH CSV'.format(
> table_name, columns)
> cur.copy_expert(sql=sql, file=s_buf)
>
> engine = create_engine('postgresql+psycopg2://:5432/postgres')
> try:
> df.to_sql('test1', engine, schema='public', if_exists='append', index=False, method=psql_insert_copy)

you need to quote ‘psql_insert_copy'

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Nikhil Benesch 2021-10-04 16:29:36 DELETE ... USING LATERAL
Previous Message Israel Brewster 2021-10-04 16:22:40 Growth planning

Browse pgsql-sql by date

  From Date Subject
Next Message Adrian Klaver 2021-10-04 16:50:52 Re: Testing of a fast method to bulk insert a Pandas DataFrame into Postgres
Previous Message Shaozhong SHI 2021-10-04 16:20:42 Re: Testing of a fast method to bulk insert a Pandas DataFrame into Postgres