1

I'm trying to export a postgres database table as a csv file in my filesystem in Python, but I'm having trouble running the copy query.

import psycopg2
import Config

class postgres_to_s3():
    def __init__(self):
        app_config = Config.Config
        self.pg_conn = app_config.pg_conn
        self.pg_cur = app_config.pg_cur

    def unload_database_to_CSV(self):
        query1 = '\COPY source_checksum TO /Users/Will/Downloads/output.csv WITH (FORMAT CSV, HEADER);'

        with open ('/Users/Will/Downloads/output.csv', 'w') as f:
            self.pg_cur.copy_expert(query1, f)

s1 = postgres_to_s3()
s1.unload_database_to_CSV()

I get the error:

psycopg2.ProgrammingError: syntax error at or near "\"
LINE 1: \COPY source_checksum TO /Users/Will/Downloads/output.csv
        ^

I was able to execute the query fine on the psql console. I tried using double backslash but I still get the same error.

EDIT: following this thread I removed the backslash, but now I get the error:

psycopg2.ProgrammingError: syntax error at or near "/"
LINE 1: COPY source_checksum TO /Users/Will/Downloads/output.csv
                                ^

0

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.