Skip to content
Advertisement

Pickeling results of postgresql query via psycopg2

I’m querying the PostgreSQL database via the psycopg2 library. The response of the query this way is cursor object file. Because of the size of the query, I’m trying to avoid re-query it and instead – save the query result as a pickle.

Unfortunately, when I trying to execute code for that:

An error massage raised:

What is a straight forward way to save the results of SQL query via python for future use?

Note: I am not obligated to use the pickle. It just seemed like an optimal solution to me.

Advertisement

Answer

I think you need cur.fetchall() after cur.execute() and its variations. https://psycopg.org/docs/cursor.html for more details.

For example:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement