Get column names from PostgreSQL table using Psycopg2 Last Updated : 14 Sep, 2021 Comments Improve Suggest changes Like Article Like Report This article is an illustration of how to extract column names from PostgreSQL table using psycopg2 and Python. Used table for demonstration: Example 1: First, we connect the PostgreSQL database using psycopg2.connect() method, then we create a cursor using cursor() method, after that we use the cursor() then extract the first element from it using slicing. Python3 import psycopg2 conn = psycopg2.connect( database="geeks", user='postgres', password='pass', host='localhost', port= '5432' ) conn.autocommit = True cursor = conn.cursor() sql = '''SELECT * FROM products''' cursor.execute(sql) column_names = [desc[0] for desc in cursor.description] for i in column_names: print(i) conn.commit() conn.close() Output: product_no name price Example 2: In the second approach, we execute the following SQL command in the cursor.execute() method. "select COLUMN_NAME from information_schema.columns where table_schema = 'SCHEMA_NAME' and table_name='TABLE_NAME'" Python3 import psycopg2 conn = psycopg2.connect( database="geeks", user='postgres', password='root', host='localhost', port='5432' ) conn.autocommit = True with conn: with conn.cursor() as cursor: cursor.execute( "select COLUMN_NAME from information_schema.columns\ where table_name='products'") column_names = [row[0] for row in cursor] print("Column names:\n") for i in column_names: print(i) Output: Column names: product_no name price Comment More infoAdvertise with us Next Article Get column names from PostgreSQL table using Psycopg2 I isitapol2002 Follow Improve Article Tags : Python Python PostgreSQL Python Pyscopg2 Practice Tags : python Similar Reads PostgreSQL - Create table using Python Creating tables in a PostgreSQL database using Python is a common task for developers working with databases. This process involves defining the structure of your data and ensuring that your database is optimized for efficient storage and retrieval. In this article, we will walk through the steps of 3 min read Python Select from PostgreSQL Table using Psycopg2 This article will introduce you to the use of the psycopg2 module which is used to connect to a PostgreSQL database from Python. We will look over how to establish a connection to a database, create a cursor object to execute DBMS SQL statements, execute a SELECT statement to retrieve the data from 7 min read Migration of table from CSVto postgres using Python In this article, we will learn to read data from a CSV file and insert it into a PostgreSQL database table using Python. PrerequisitesPython installed on your machine.PostgreSQL server and database setup.psycopg2 library to connect to PostgreSQL from Python.pandas library to handle CSV data.Let's in 2 min read Do loop in Postgresql Using Psycopg2 Python In this article, we use psycopg2 to loop through all data points using psycopg2 function in Python. We will first connect our PostgreSQL database using psycopg2.connect method, and pass the connection parameters such as the host, database, user, and password. Then we will create a cursor using the c 4 min read Python PostgreSQL Connection Pooling Using Psycopg2 In this article, We will cover the basics of connection pooling using connection pooling in Python applications, and provide step-by-step instructions on how to implement connection pooling using Psycopg2. Whether you are building a small-scale application or a large-scale enterprise application, un 6 min read Like