[Python] Comments on columns in Postgres table is Null

Discussão em 'Python' iniciado por Stack, Março 29, 2025.

  1. Stack

    Stack Membro Participativo

    I added comments in my postgres table using this method, statement ran successfully.

    COMMENT ON TABLE statements IS 'Table storing bank transaction data imported from statements sheet';
    COMMENT ON COLUMN statements.id IS 'Unique identifier for each transaction';
    COMMENT ON COLUMN statements.customer_id IS 'Reference to the customer, alphanumeric ID';


    I am using this query to get comments on the table

    SELECT
    column_name,
    data_type,
    col_description(('public.' || 'statements')::regclass, ordinal_position) AS column_comment
    FROM
    information_schema.columns
    WHERE
    table_schema = 'public'
    AND table_name = 'statements';


    Here is the result [​IMG]

    What am I missing, how to add comments properly? or how to retreive info properly?

    I tried this but it didnt work, error says user_col_comments doesnt exist

    Here is my client code

    from sqlalchemy import create_engine, text
    connection_string = f"postgresql://{postgres_username}:{postgres_password}@{postgres_host}:{postgres_port}/{postgres_database}"
    engine = create_engine(connection_string)

    def execute_sql_from_file(engine, file_path):
    """
    Execute SQL queries from a file.

    Args:
    engine: SQLAlchemy engine object
    file_path: Path to the SQL file

    Returns:
    List of results from executed queries
    """
    # Read the SQL file
    with open(file_path, "r") as file:
    sql_queries = file.read()

    # Split the queries if there are multiple
    queries = sql_queries.split(";")

    # Store results
    results = []

    # Execute each query individually
    with engine.connect() as connection:
    for query in queries:
    query = query.strip()
    if query: # Ensure the query is not empty
    print(f"Executing query: {query}")
    result = connection.execute(text(query))

    # If the query returns data (SELECT), store it in results
    if result.returns_rows:
    results.append(result.fetchall())
    print("Query executed successfully")

    return results

    Continue reading...

Compartilhe esta Página