Skip to content

Is there a way to use asyncpg's "copy_from_table" or psycopg2's "copy_from" in databases? #305

Answered by elivin
ohmeow asked this question in Q&A
Discussion options

You must be logged in to vote

Hi

I found one way to do it. There is a raw_connection property that returns the driver's current connection. It can be used like this:

db = Database("postgresql+asyncpg://...")
# ...

async with db.connection() as connection:
    await connection.raw_connection.copy_records_to_table(
        "table_name",
        columns=["id", "name"],
        records=[
            (1, "Record 1"),
            (2, "Record 2"),
            (3, "Record 3"),
        ],
    )

In this sample connection.raw_connection is an asyncpg.Connection object, so we can use all of its methods.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by tomchristie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #288 on March 24, 2021 09:46.