python/ pandas/ db2/ cursor/ amazon-redshift
I want to fetch data using my python code like we do with describe [tableName] statement
. I want to do that on Redshift and DB2.
I tried to do that using Pandas and cursors, I tried the following chunks of commands:
"set search_path to SCHEMA; select * from pg_table_def where schemaname = 'schema' and LOWER(tablename) = 'TableName';
describe schema.tableName
select column_name, data_type, character_manimum_length from information_schema.columns where table_schema = 'Schema' and table_name = 'TableName';
\\d or \\d+
.
import psycopg2
import numpy as np
import pandas as pd
con=psycopg2.connect(dbname= 'DBNAME', host="Host-Link",
port= '5439', user= 'username', password= 'password')
print(con)
cur = con.cursor()
query = "set search_path to Schema; select * from pg_table_def where schemaname = 'Schema' and LOWER(tablename) = 'TableName';"
cur.execute(query)
temp = cur.fetchall()
print(temp)
data_frame = pd.read_sql("set search_path to Schema; select * from pg_table_def where schemaname = 'Schema' and LOWER(tablename) = 'TableName';", con)
print(data_frame)
con.close()
I want the output as following:
COLUMN_NAME DATA_TYPE PK NULLABLE DEFAULT AUTOINCREMENT COMPUTED REMARKS POSITION
col1 varchar(10) YES NO NO NO 1
col2 varchar(50) NO NO NO NO 2
col3 smallint NO NO NO NO 3
A lot of this data is included in the SVV_COLUMNS
system view. You can query that table using the table_name
and table_schema
columns.
https://docs.aws.amazon.com/redshift/latest/dg/r_SVV_COLUMNS.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.