Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

psycopg2 bulkloader docs update #75

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading