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 - 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
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
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 - How to use psycopg2 to retrieve a certain key's value from a postgres table which has key-value pairs - Stack Overflow
I'm new to python, trying to use psycopg2 to read Postgres. I am reading data from a database table called deployment and trying to handle a value from a table with three fields id, Key and value. ... More on stackoverflow.com
🌐 stackoverflow.com
March 27, 2019
🌐
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()
🌐
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?
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › get-column-name-and-column-type-with-python-psycopg2
Get Column name and Column type with Python psycopg2 | GeeksforGeeks
September 2, 2024 - 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!
🌐
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 - This method creates a new psycopg2.extensions.cursor object. ... Execute the select query using the cursor.execute() method. ... 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.)
🌐
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/ >
🌐
GitHub
github.com › psycopg › psycopg2 › issues › 438
Why table and column names should be interpolated using string formating · Issue #438 · psycopg/psycopg2
June 10, 2016 - Hello, psycopg2 documentation states that I should interpolate table and column names using python string formating - but does not explain why. So I'm wondering what is the reason for that?
Author   psycopg
🌐
ZetCode
zetcode.com › python › psycopg2
Python PostgreSQL proramming with psycopg2 module
January 29, 2024 - We can then refer to the data by their column names. ... #!/usr/bin/python import psycopg2 import psycopg2.extras con = psycopg2.connect(database='testdb', user='postgres', password='s$cret') with con: cursor = con.cursor(cursor_factory=psycopg2.extras.DictCursor) cursor.execute("SELECT * FROM cars") rows = cursor.fetchall() for row in rows: print(f"{row['id']} {row['name']} {row['price']}")
🌐
YouTube
youtube.com › how to fix your computer
PYTHON : How do I get a list of column names from a psycopg2 cursor? - YouTube
PYTHON : How do I get a list of column names from a psycopg2 cursor? [ Gift : Animated Search Engine : https://www.hows.tech/p/recommended.html ] PYTHON : H...
Published   December 6, 2021
Views   121
🌐
Nabble
python.6.x6.nabble.com › accessing-Postgres-db-results-by-column-name-td1741940.html
Python - tutor - accessing Postgres db results by column name
April 9, 2010 - Python › General › Python - tutor · accessing Postgres db results by column name · ‹ Previous Topic Next Topic › · Locked 8 messages · Serdar Tumgoren · Reply | Threaded · Open this post in threaded view
🌐
Itecnote
itecnote.com › tecnote › python-psycopg2-postgres-select-columns-including-field-names
Python psycopg2 postgres select columns including field ...
온라인 포커 추천 사이트 TOP10을 레이크백·보너스·입출금 기준으로 직접 비교했습니다. 최대 60% 레이크백부터 노디포짓 보너스까지, 2026 년 검증된 해외 포커 플랫폼만 정리했습니다.