You need to use RealDictCursor, then you can access the results like a dictionary:

import psycopg2
from psycopg2.extras import RealDictCursor
connection = psycopg2.connect(user="...",
                              password="...",
                              host="...",
                              port="...",
                              database="...",
                              cursor_factory=RealDictCursor)
cursor = connection.cursor()

cursor.execute("SELECT * FROM user")
users = cursor.fetchall()

print(users)
print(users[0]['user'])

Output:

[RealDictRow([('user', 'dbAdmin')])]
dbAdmin
Answer from Maurice Meyer on Stack Overflow
Discussions

postgresql - Python psycopg2 postgres select columns including field names - Stack Overflow
Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... New site design and philosophy for Stack Overflow: Starting February 24, 2026... I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s... 4 How to get column attributes from a query using PostgreSQL? 23 Pass column name as parameter to PostgreSQL using psycopg2 · 1 PostgreSQL SELECT column names programmatically in python · 2 retrieve values ... More on stackoverflow.com
🌐 stackoverflow.com
June 29, 2016
postgresql - Get column name and column type in the same query in the correct order using psycopg2 - Database Administrators Stack Exchange
I have a postgresql database and I am querying it using psycopg2 and I need to get the data in the correct order as in table I have found different ways to get the column names from table and column More on dba.stackexchange.com
🌐 dba.stackexchange.com
In Psycopg2 unable to pass table name as a string in my command. Cursor executes if table name is not passed but put in directly.
Dude, I don't know why you deleted your last post with the almost exact same issue. But you really need to go ask these questions in a python community or check stackoverflow or something. This is a python question. And once again the answer is in the same link I sent before. https://www.psycopg.org/docs/sql.html The problem is now you are sending an identifier when you want to send a string literal. So you need to not use '{}', nor the Identifier method. In this case you just use %s in the string and your variable in the format method. And the reason not to delete your post is so that others can learn from your questions. More on reddit.com
🌐 r/PostgreSQL
11
0
July 18, 2023
python - Fetch column as a list of values with Psycopg2 - Stack Overflow
Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... 2 How to use psycopg2 to retrieve a certain key's value from a postgres table which has key-value pairs · 11 psycopg2 use column names instead of column number to get row data More on stackoverflow.com
🌐 stackoverflow.com
June 23, 2020
🌐
PostgreSQL Wiki
wiki.postgresql.org › wiki › Using_psycopg2_with_PostgreSQL
Using psycopg2 with PostgreSQL - PostgreSQL wiki
cursor.execute('SHOW work_mem') # Call fetchone - which will fetch the first row returned from the # database. memory = cursor.fetchone() # access the column by numeric index: # even though we enabled columns by name I'm showing you this to # show that you can still access columns by index and iterate over them. print "Value: ", memory[0] # print the entire row print "Row: ", memory if __name__ == "__main__": main()
🌐
GeeksforGeeks
geeksforgeeks.org › python › get-column-name-and-column-type-with-python-psycopg2
Get Column name and Column type with Python psycopg2 - GeeksforGeeks
July 23, 2025 - Let's us see a few examples of how we can get column name and column type with Python psycopg2. Below code connects to a PostgreSQL database, creates a table with specified columns, and then closes the connection. After reconnecting, it retrieves the column names and data types for the created table by querying the information_schema.columns, and then prints the results.
🌐
Human Asia
thehumanasia.com › aphria-toronto-wgq › psycopg2-get-value-by-column-name-4486b8
psycopg2 get value by column name
December 25, 2020 - Adapter will be used, only logical replication requires name of the columns.! Python to the cursor or modify the object responsible to cast arrays, if available class the. Was trying in another way but could n't get perfect result: Now, we the! Convert Python dict objects to psycopg2 get value by column name from the database on conn_or_curs to the!
🌐
GeeksforGeeks
geeksforgeeks.org › get-column-names-from-postgresql-table-using-psycopg2
Get column names from PostgreSQL table using Psycopg2 | GeeksforGeeks
September 14, 2021 - This article is an illustration of how to extract column names from PostgreSQL table using psycopg2 and Python.
Find elsewhere
🌐
Safe Community
community.safe.com › home › forums › fme form › authoring › generate list of column names from database table
Generate list of column names from database table | Community
October 18, 2017 - The attached workspace (get-cols.fmw) have a Python Scripted parameter returning a string being a comma separated list of the column names. ... import psycopg2 colnames = [] conn = psycopg2.connect(host="%s"%FME_MacroValues['HOST'], port=FME_MacroValues['PORT'], dbname="%s"%FME_MacroValues['DATABASE'], user="%s"%FME_MacroValues['USER'], password="%s"%FME_MacroValues['PASSWORD']) cur=conn.cursor() cur.execute("select column_name, data_type from INFORMATION_SCHEMA.COLUMNS where table_name = '%s';"%FME_MacroValues['TABLE_NAME']) row = cur.fetchone() while row != None: colnames.append(row[0]) row = cur.fetchone() return ",".join(colnames)
🌐
PYnative
pynative.com › home › python › databases › python select from postgresql table using psycopg2
Python Select from PostgreSQL Table using psycopg2
March 9, 2021 - After successfully executing a Select operation, Use the fetchall() method of a cursor object to get all rows from a query result. it returns a list of rows. ... Iterate a row list using a for loop and access each row individually (Access each row’s column data using a column name or index number.) Close the cursor object and database connection object · use cursor.clsoe() and connection.clsoe() method to close open connections after your work completes. Let see the example now. import psycopg2 try: connection = psycopg2.connect(user="sysadmin", password="pynative@#29", host="127.0.0.1", po
🌐
ZetCode
zetcode.com › python › psycopg2
Python PostgreSQL proramming with psycopg2 module
January 29, 2024 - ... #!/usr/bin/python import psycopg2 ...ras.DictCursor) cursor.execute("SELECT * FROM cars") rows = cursor.fetchall() for row in rows: print(f"{row['id']} {row['name']} {row['price']}") In this example, we print the contents of the cars table using the dictionary cursor...
🌐
Reddit
reddit.com › r/postgresql › in psycopg2 unable to pass table name as a string in my command. cursor executes if table name is not passed but put in directly.
r/PostgreSQL on Reddit: In Psycopg2 unable to pass table name as a string in my command. Cursor executes if table name is not passed but put in directly.
July 18, 2023 -

This returns the columns and allows my cursor to fetch them as a list if I don't pass the table name. Otherwise it gives me an error saying no column 'delete_9' exists. I am passing a table name not column name?
🌐
Python
mail.python.org › pipermail › tutor › 2010-April › 075587.html
[Tutor] accessing Postgres db results by column name
April 10, 2010 - > > I'd like to be able to do something like below: > > cur.execute('select id, name from mytable') > data = cur.fetchall() > for row in data: > print row['id'], row['name'] > > The functionality I have in mind is built into sqlite3: > > > http://docs.python.org/py3k/library/sqlite3.html#accessing-columns-by-name-instead-of-by-index > > And there are a few Python recipes that let you mimic this behavior: > > > http://code.activestate.com/recipes/81252-using-dtuple-for-flexible-query-result-access/ > > http://code.activestate.com/recipes/52293-generate-field-name-to-column-number-dictionary/ >
🌐
TutorialsPoint
tutorialspoint.com › python_data_access › python_postgresql_select_data.htm
Python PostgreSQL - Select Data
Following SELECT query retrieves the values of the columns FIRST_NAME, LAST_NAME and, COUNTRY from the CRICKETERS table.
🌐
GeeksforGeeks
geeksforgeeks.org › python-select-from-postgresql-table-using-psycopg2
Python Select from PostgreSQL Table using Psycopg2 - GeeksforGeeks
April 28, 2025 - To fetch data from the database we have a function in the psycopg2 library called fetchall() by using this particular function we can have access to the data in the table. With the help of this select operation, we can get access to every single ...
🌐
Hackers and Slackers
hackersandslackers.com › psycopg2-postgres-python
Psycopg2: PostgreSQL & Python the Old Fashioned Way
January 15, 2019 - In addition to the vanilla functionality we'd expect, Psycopg2 has a lot of useful hidden features in a submodule called psycopg2.extras. We're going to dig into this toolbox and pull out something called DictCursor: a flavor of database cursor that will return rows as a list of key/value pairs as opposed to only values · My personal favorite of these extras is the DictCursor, which renders the rows being returned by our query as Python dictionaries as opposed to lists. When using a DictCursor, the key is always the column name, and the value is the value of that column in that particular row.
🌐
Psycopg
psycopg.org › psycopg3 › docs › api › rows.html
rows – row factory implementations - psycopg 3.3.5.dev1 documentation
This way it can inspect the cursor ... object. For instance the dict_row() RowFactory uses the names of the column to define the dictionary key and returns a RowMaker function which would use the values to create a dictionary for each record....