- Basic module usage
Starting from version 2.5, psycopg2’s connections and cursors are context managers and can be used with the
- Server side cursors
When a database query is executed, the Psycopg
cursor usually fetches all the records returned by the backend, transferring them to the client process. If the query returned an huge amount of data, a proportionally large amount of memory will be allocated by the client.
If the dataset is too large to be practically handled on the client side, it is possible to create a server side cursor. Using this kind of cursor it is possible to transfer to the client only a controlled amount of data, so that a large dataset can be examined without keeping it entirely in memory.
- Using psycopg2 with PostgreSQL https://wiki.postgresql.org/wiki/Using_psycopg2_with_PostgreSQL
- PostgreSQL – Python Interface https://www.tutorialspoint.com/postgresql/postgresql_python.htm