Search code examples
pythonpostgresqlpsycopg2

Psycopg2 copy_to not fully executing


def backup_action():
    # read connection parameters
    conn = psycopg2.connect(clicked.get())
    # connect to the PostgreSQL server
    print('Connecting to the PostgreSQL database...')
    cursor = conn.cursor()
    f = open(cur_path+"/"+"kopia"+".csv", 'w')
    cursor.copy_to(f, 'mtr', sep=",")
    cursor.close()

I have a problem with copy_to executing only partialy. I have 2 databases, one for testing with almost embpy small tables and one big database with actual data. When I execute this for the smaller one it works just fine but when I try to do it for the bigger one, it modifies the csv file but leaves it empty.

I've had a similar problem once with doing an actual backup in pyodbc and I resolved it by making delaying closing the connection. I have no idea if that's actually the problem here and I don't really know if psycopg2 offers a similar solution.

Please help.


Solution

  • I don't know the exact cause of the problem, but using the copy_expert worked for me. I'd post the code but for some reason the page doesn't let me to.