How do I get a list of column names from a psycopg2 cursor?

Setjmp picture Setjmp · Apr 20, 2012 · Viewed 92.3k times · Source

I would like a general way to generate column labels directly from the selected column names, and recall seeing that python's psycopg2 module supports this feature.

Answer

Setjmp picture Setjmp · Apr 20, 2012

From "Programming Python" by Mark Lutz:

curs.execute("Select * FROM people LIMIT 0")
colnames = [desc[0] for desc in curs.description]