Skip to content

Commit

Permalink
psycopg2 bulkloader docs update (#75)
Browse files Browse the repository at this point in the history
psycopg2 bulkloader updated
  • Loading branch information
fromm1990 authored Jan 23, 2025
1 parent 2dc282b commit 95ed630
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Unreleased
``dependson`` is now a list instead of a filter iterator. This fixes issue #72
where dependencies were only loaded in the first bulk load.

**Changed**
Changed psycopg2 bulkloader documentation to use ``copy_expert`` instead of
``copy_from``. This solves issue #74, where newer psycopg2 versions escape
table names to avoid sql injection.

Version 2.8
-----------
**Added**
Expand Down
10 changes: 6 additions & 4 deletions docs/examples/bulkloading.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ must have the following signature:

PostgreSQL
----------
For PostgreSQL the `copy_from
<http://initd.org/psycopg/docs/cursor.html#cursor.copy_from>`__ method from
For PostgreSQL the `copy_expert
<https://www.psycopg.org/docs/cursor.html#cursor.copy_expert>`__ method from
psycopg2 can be used:

.. code-block:: python
Expand All @@ -43,8 +43,10 @@ psycopg2 can be used:
def pgbulkloader(name, attributes, fieldsep, rowsep, nullval, filehandle):
global connection
cursor = connection.cursor()
cursor.copy_from(file=filehandle, table=name, sep=fieldsep, null=nullval,
columns=attributes)
cursor.copy_expert(
f"COPY {name} ({','.join(attributes)}) FROM STDIN WITH (FORMAT csv, DELIMITER '{fieldsep}', NULL '{nullval}');",
filehandle
)
If Jython is used the `copyIn
<https://jdbc.postgresql.org/documentation/publicapi/org/postgresql/copy/CopyManager.html#copyIn-java.lang.String->`__
Expand Down

0 comments on commit 95ed630

Please sign in to comment.