Get Column name and Column type with Python psycopg2
Last Updated :
02 Sep, 2024
When working with PostgreSQL databases in Python, one common task is to retrieve metadata about database tables, such as column names and their types. This information can be crucial for dynamically generating queries, validating data, or understanding the structure of our database. The psycopg2 library provides a powerful way to interact with PostgreSQL databases from Python, including retrieving metadata about tables.
Prerequisites
Before diving into the examples, ensure we have the following prerequisites:
- PostgreSQL Installed: We need a running PostgreSQL server.
- Python Environment: Python should be installed on our system.
- psycopg2 Library: Install the psycopg2 library if it is not already installed. This library is used to connect to PostgreSQL from Python.
Install psycopg2 using pip:
pip install psycopg2
Get Column name and Column type with Python psycopg2
Let's us see a few examples of how we can get column name and column type with Python psycopg2.
Example 1: Create a Table and Retrieve Column Information
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. Finally, it closes the cursor and connection again.
Python
import psycopg2
# Connect to your PostgreSQL database
conn = psycopg2.connect(
dbname="your_database_name",
user="your_username",
password="your_password",
host="localhost",
port="5432"
)
# Create a cursor object
cur = conn.cursor()
# Create a table
create_table_query = """
CREATE TABLE employee (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
age INT,
hire_date DATE
)
"""
cur.execute(create_table_query)
conn.commit()
print("Table created successfully")
# Close the cursor and connection
cur.close()
conn.close()
Output
Step 2: Get Column Names and Column Types
The information_schema
is a standardized set of read-only views that provide metadata about the objects (such as tables, columns, and constraints) in a relational database.
The information_schema
acts as a database within the database, allowing users to query metadata just like they would query regular data.
Here, we will use this information_schema
database to fetch column name and column type of the table we created.
Python
import psycopg2
# Connect to your PostgreSQL database
conn = psycopg2.connect(
dbname="your_database_name",
user="your_username",
password="your_password",
host="localhost",
port="5432"
)
# Create a cursor object
cur = conn.cursor()
# Query to get column names and types
table_name = 'employee'
cur.execute(f"SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '{table_name}'")
# Fetch all results
columns = cur.fetchall()
# Display column names and types
print("Column Name\t\tColumn Type")
print("-" * 40)
for column in columns:
print(f"{column[0]}\t\t{column[1]}")
# Close the cursor and connection
cur.close()
conn.close()
Output
Fetch table metadata using python psycopg2Example 2: Create a Table with Different Data Types and Retrieve Metadata
Below code connects to the PostgreSQL database, retrieves column names and data types from the information_schema.columns for a specified table, and prints them. It then closes the cursor and database connection.
Step 1: Create a Table with Different Data Types
Python
import psycopg2
# Connect to your PostgreSQL database
conn = psycopg2.connect(
dbname="your_database_name",
user="your_username",
password="your_password",
host="localhost",
port="5432"
)
# Create a cursor object
cur = conn.cursor()
# Create a table with various data types
create_table_query = """
CREATE TABLE product (
product_id SERIAL PRIMARY KEY,
product_name VARCHAR(255),
price DECIMAL(10, 2),
stock_quantity INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
"""
cur.execute(create_table_query)
conn.commit()
print("Table created successfully")
# Close the cursor and connection
cur.close()
conn.close()
Output
Step 2: Get Column Names and Types
Python
import psycopg2
# Connect to your PostgreSQL database
conn = psycopg2.connect(
dbname="your_database_name",
user="your_username",
password="your_password",
host="localhost",
port="5432"
)
# Create a cursor object
cur = conn.cursor()
# Query to get column names and types
table_name = 'product'
cur.execute(f"SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '{table_name}'")
# Fetch all results
columns = cur.fetchall()
# Display column names and types
print("Column Name\t\tColumn Type")
print("-" * 40)
for column in columns:
print(f"{column[0]}\t\t{column[1]}")
# Close the cursor and connection
cur.close()
conn.close()
Output
Fetch Column name and Column type using Python Psycopg2Use Cases
- Dynamic Query Generation: Retrieving column names and types allows us to dynamically generate queries based on the table schema, which is useful in applications where the database schema may change frequently.
- Data Validation: Knowing the data types of columns helps in validating data before insertion or modification to ensure it conforms to the expected format.
- Schema Exploration: Useful in data analysis and reporting tools to understand the structure of the database and its tables without needing prior knowledge of the schema.
Conclusion
Retrieving column names and types from a PostgreSQL database using Python and psycopg2 is a straightforward process that can be extremely valuable in various scenarios, from dynamic query generation to data validation. By following the examples provided, we can efficiently interact with PostgreSQL to obtain the metadata we need for our applications.
Similar Reads
Python Psycopg2 - Concatenate columns to new column In this article, we are going to see how to concatenate multiple columns of a table in a PostgreSQL database into one column. To concatenate two or more columns into one, PostgreSQL provides us with the concat() function. Table for demonstration: In the below code, first, a connection is formed to t
2 min read
Get column names from CSV using Python CSV (Comma Separated Values) files store tabular data as plain text, with values separated by commas. They are widely used in data analysis, machine learning and statistical modeling. In Python, you can work with CSV files using built-in libraries like csv or higher-level libraries like pandas. In t
2 min read
Get column names from PostgreSQL table using Psycopg2 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 cu
1 min read
Executing SQL query with Psycopg2 in Python In this article, we are going to see how to execute SQL queries in PostgreSQL using Psycopg2 in Python. Psycopg2 is a PostgreSQL database driver, it is used to perform operations on PostgreSQL using python, it is designed for multi-threaded applications. SQL queries are executed with psycopg2 with t
2 min read
How to Fetch All Rows with psycopg2.fetchall in Python PostgreSQL, commonly known as Postgres, is a highly capable, open-source relational database management system (RDBMS). Renowned for its robustness, scalability, and adherence to SQL standards, PostgreSQL is used in a variety of applications, from small projects to large-scale enterprise systems, du
9 min read
Introduction to Psycopg2 module in Python Psycopg is the most popular PostgreSQL adapter used in  Python.  Its works on the principle of the whole implementation of Python DB API 2.0 along with the thread safety (the same connection is shared by multiple threads). It is designed to perform heavily multi-threaded applications that usually cr
4 min read