<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/postgresql-adapter-for-python-7mss4n5wr" -->

---
title: PostgreSQL adapter for Python | daily.dev
description: Server-side cursors in Psycopg let you stream PostgreSQL query results row by row instead of loading an entire result set into memory, keeping memory usage...
canonical: https://daily.dev/posts/postgresql-adapter-for-python-7mss4n5wr
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: PostgreSQL adapter for Python | daily.dev
og:description: Server-side cursors in Psycopg let you stream PostgreSQL query results row by row instead of loading an entire result set into memory, keeping memory usage...
og:url: https://daily.dev/posts/postgresql-adapter-for-python-7mss4n5wr
og:image: https://api.daily.dev/og/posts/7mSs4n5wR.png
og:image:alt: PostgreSQL adapter for Python
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# PostgreSQL adapter for Python

**[Psycopg](https://daily.dev/sources/psycopg)** · 4 min read · 0 upvotes · 0 comments

## Summary

Server-side cursors in Psycopg let you stream PostgreSQL query results row by row instead of loading an entire result set into memory, keeping memory usage small and predictable for large datasets. Naming a cursor tells Psycopg to use a server-side cursor; the itersize attribute (default 100 in Psycopg 3) controls batch size when iterating with for loops, though fetchone() and fetchmany(n) fetch exact counts regardless. Async support mirrors the sync API. A practical CSV export example shows streaming millions of rows to disk using minimal memory, and a caveat notes that server-side cursors require an open transaction for their lifetime.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.psycopg.org/articles/2026/08/12/server-side-cursors>

## Questions this post answers

### How do I stream large PostgreSQL query results in Python without loading everything into memory?

Use a server-side cursor in Psycopg by giving the cursor a name, e.g. conn.cursor(name="my_cursor"), which tells Psycopg to keep the result set on the PostgreSQL server and send rows in batches instead of all at once. This keeps the Python process's memory footprint small and predictable regardless of result set size, and works with both Psycopg 2 and Psycopg 3.

_daily.dev surfaces practical patterns like this for developers optimizing memory use in data-heavy Python services._

### What does the itersize attribute control in a Psycopg 3 server-side cursor?

Itersize controls how many rows are fetched per round-trip when iterating over a server-side cursor with a for loop, defaulting to 100 rows in Psycopg 3. Larger values reduce database round-trips but increase memory usage per batch; it does not affect fetchone() or fetchmany(n), which always fetch exactly the requested number of rows.

_Developers tuning database throughput can track configuration details like this via daily.dev._

### Do server-side cursors work with async Psycopg 3 connections?

Yes, Psycopg 3's async support extends naturally to server-side cursors, allowing large result sets to be streamed in an async context without blocking. The API mirrors the synchronous version exactly, just adding the await keyword before execute and using async for to iterate over rows.

_Async Python and database streaming questions like this come up often for daily.dev readers building non-blocking services._

## Similar posts on daily.dev

- [Introducing Apache Arrow Support in mssql-python](https://daily.dev/posts/introducing-apache-arrow-support-in-mssql-python-yq9micn57) · Planet Python · 0 upvotes · 0 comments
- [PostgreSQL: Psycopg 3.3 released](https://daily.dev/posts/postgresql-psycopg-3-3-released-ewfhnirnz) · PostgreSQL · 1 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#database](https://daily.dev/tags/database), [#postgresql](https://daily.dev/tags/postgresql)

[View this post on daily.dev](https://daily.dev/posts/postgresql-adapter-for-python-7mss4n5wr)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"PostgreSQL adapter for Python","url":"https://daily.dev/posts/postgresql-adapter-for-python-7mss4n5wr","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/postgresql-adapter-for-python-7mss4n5wr"},"datePublished":"2026-08-23T12:21:30.458Z","dateModified":"2026-09-13T21:47:45.677Z","description":"Server-side cursors in Psycopg let you stream PostgreSQL query results row by row instead of loading an entire result set into memory, keeping memory usage...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/b0cbd2ac8948f96ab39fa6a90db7464f?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/b0cbd2ac8948f96ab39fa6a90db7464f?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Psycopg","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Psycopg","logo":"https://media.daily.dev/image/upload/s--5N-KNNWk--/f_auto,q_auto/v1787487485/logos/psycopg?_a=BAMAMicg0","url":"https://daily.dev/sources/psycopg"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/postgresql-adapter-for-python-7mss4n5wr","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,database,postgresql","timeRequired":"PT4M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Psycopg","item":"https://daily.dev/sources/psycopg"},{"@type":"ListItem","position":3,"name":"PostgreSQL adapter for Python"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/postgresql-adapter-for-python-7mss4n5wr#faq","mainEntity":[{"@type":"Question","name":"How do I stream large PostgreSQL query results in Python without loading everything into memory?","acceptedAnswer":{"@type":"Answer","text":"Use a server-side cursor in Psycopg by giving the cursor a name, e.g. conn.cursor(name=\"my_cursor\"), which tells Psycopg to keep the result set on the PostgreSQL server and send rows in batches instead of all at once. This keeps the Python process's memory footprint small and predictable regardless of result set size, and works with both Psycopg 2 and Psycopg 3. daily.dev surfaces practical patterns like this for developers optimizing memory use in data-heavy Python services."}},{"@type":"Question","name":"What does the itersize attribute control in a Psycopg 3 server-side cursor?","acceptedAnswer":{"@type":"Answer","text":"Itersize controls how many rows are fetched per round-trip when iterating over a server-side cursor with a for loop, defaulting to 100 rows in Psycopg 3. Larger values reduce database round-trips but increase memory usage per batch; it does not affect fetchone() or fetchmany(n), which always fetch exactly the requested number of rows. Developers tuning database throughput can track configuration details like this via daily.dev."}},{"@type":"Question","name":"Do server-side cursors work with async Psycopg 3 connections?","acceptedAnswer":{"@type":"Answer","text":"Yes, Psycopg 3's async support extends naturally to server-side cursors, allowing large result sets to be streamed in an async context without blocking. The API mirrors the synchronous version exactly, just adding the await keyword before execute and using async for to iterate over rows. Async Python and database streaming questions like this come up often for daily.dev readers building non-blocking services."}}]}
```

