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
pandas - how to get column names from postgres table using python psycopg2? - Stack Overflow
I am trying to get column names from my postgres sql table using psycopg2 but it is returning unordered column list not same as how columns are shown in table. This is how database table look when ... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
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!
🌐
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)
Find elsewhere
🌐
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.
🌐
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/ >
🌐
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
🌐
Psycopg
psycopg.org › psycopg3 › docs › api › rows.html
rows – row factory implementations - psycopg 3.3.5.dev1 documentation
Convert a sequence of values from the database to a finished object. ... Callable protocol taking a Cursor and returning a RowMaker. A RowFactory is typically called when a Cursor receives a result. This way it can inspect the cursor state (for instance the description attribute) and help a RowMaker to create a complete 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.
🌐
PYnative
pynative.com › home › python › databases › python select from postgresql table using psycopg2
Python Select from PostgreSQL Table using psycopg2
March 9, 2021 - import psycopg2 try: connection = psycopg2.connect(user="sysadmin", password="pynative@#29", host="127.0.0.1", port="5432", database="postgres_db") cursor = connection.cursor() postgreSQL_select_Query = "select * from mobile" cursor.execute(postgreSQL_select_Query) print("Selecting rows from mobile table using cursor.fetchall") mobile_records = cursor.fetchall() print("Print each row and it's columns values") for row in mobile_records: print("Id = ", row[0], ) print("Model = ", row[1]) print("Price = ", row[2], "\n") except (Exception, psycopg2.Error) as error: print("Error while fetching data from PostgreSQL", error) finally: # closing database connection. if connection: cursor.close() connection.close() print("PostgreSQL connection is closed") Code language: Python (python)
🌐
ZetCode
zetcode.com › python › psycopg2
Python PostgreSQL proramming with psycopg2 module
January 29, 2024 - Number of rows affected by an SQL statement is a metadata. Number of rows and columns returned in a result set belong to metadata as well. Metadata in PostgreSQL can be obtained using from the description property of the cursor object or from the information_schema table. Next we print all rows from the cars table with their column names. ... #!/usr/bin/python import psycopg2 con = psycopg2.connect(database='testdb', user='postgres', password='s$cret') with con: cur = con.cursor() cur.execute('SELECT * FROM cars') col_names = [cn[0] for cn in cur.description] rows = cur.fetchall() print(f'{col_names[0]} {col_names[1]} {col_names[2]}')
🌐
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?
🌐
sqlpey
sqlpey.com › python › top-8-methods-to-retrieve-column-names-using-psycopg2
Top 8 Methods to Retrieve Column Names Using Psycopg2 - …
November 1, 2024 - import psycopg2 DSN = "host=YOUR_DATABASE_HOST port=YOUR_DATABASE_PORT dbname=YOUR_DATABASE_NAME user=YOUR_DATABASE_USER" column_names = [] with psycopg2.connect(DSN) as connection: with connection.cursor() as cursor: cursor.execute(""" SELECT column_name FROM information_schema.columns WHERE table_schema = 'YOUR_SCHEMA_NAME' AND table_name = 'YOUR_TABLE_NAME' """) column_names = [row[0] for row in cursor] print("Column names: {}".format(column_names))
🌐
Medium
varun-verma.medium.com › use-psycopg2-to-return-dictionary-like-values-key-value-pairs-4d3047d8de1b
Use psycopg2 to return dictionary like values (key-value pairs) | by Verma Varun | Medium
April 20, 2021 - In order to use row[‘column_name’] from the result, you’ll have to use the extras module provided in the psycopg2. When initializing the cursor, pass in the cursor_factory to return the results in a dictionary format.
🌐
Itecnote
itecnote.com › tecnote › python-psycopg2-postgres-select-columns-including-field-names
Python psycopg2 postgres select columns including field ...
온라인 포커 추천 사이트 TOP10을 레이크백·보너스·입출금 기준으로 직접 비교했습니다. 최대 60% 레이크백부터 노디포짓 보너스까지, 2026 년 검증된 해외 포커 플랫폼만 정리했습니다.