4

I'm trying to save in a local .csv the results of query performed in python towards a PostgreSQL database (using psycopg2).

I'm able to print the result of the query in the console, but fail to export it to a csv file.

I've tried using the copy_to function, but even with the documentation I can't figure it out:

    # Retrieve the records from the database with query
    cursor.execute("SELECT col1 FROM myDB.myTable WHERE col1 > 2")
    records = cursor.fetchall()

    # Save to csv with copy_to
    io = open('copy_to.csv', 'w')
    cursor.copy_to(io, 'records', ',')
    print("Copied records from query into file object using sep = ,")
    io.close()

This provokes the error "psycopg2.ProgrammingError: relation "records" does not exist".

Is there a better way to store the query's result in a local table which could be passed in copy_to? Thanks for any tips!

1
  • 1
    copy_to is really meant to work on entire tables, not queries. Consider using python's csv library or pandas to write the csv file. Commented Jan 26, 2017 at 22:55

3 Answers 3

5

I did some more research and here is another solution that might be more likely to work:

``` python
import psycopg2

#note the lack of trailing semi-colon in the query string, as per the Postgres documentation
s = "'SELECT col1 FROM myDB.myTable WHERE col1 > 2'"

conn = psycopg2.connect...
db_cursor = conn.cursor()

SQL_for_file_output = "COPY ({0}) TO STDOUT WITH CSV HEADER".format(s)

WITH Open(filepath/name, 'w') as f_output:
    cur.copy_expert(SQL_for_file_output, f_output)

conn.close()
```
Sign up to request clarification or add additional context in comments.

Comments

2

Hopefully this helps:

cursor.copy_to(COPY table TO file syntax)

So in your case:

cursor.copy_to(COPY records TO io)

Comments

0

This does work for me:

csv_file = open('myFile.csv', 'w')

cursor = finbot.conn.cursor()
cursor.copy_to(csv_file, 'table_name', sep=",")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.