def primarykeys(conn, table):
    rows = query(conn, """
        SELECT information_schema.constraint_column_usage.column_name
        FROM information_schema.table_constraints
        NATURAL JOIN information_schema.constraint_column_usage
        WHERE information_schema.table_constraints.table_name=%s
          AND information_schema.table_constraints.constraint_type='PRIMARY KEY'
        ;""", table)
    return [row['column_name'] for row in rows]
Find primary keys
dataset/ETHPy150Open uwdata/termite-data-server/web2py/scripts/extract_pgsql_models.py/primarykeys