#Connect to the cluster import redshift_connector conn = redshift_connector.connect( host='redshift_host', database='database', port=5439, user='usename', password='password' ) output_file = r'/path/table_ddl_output.sql' # Create a Cursor object cursor = conn.cursor() # Query a table using the Cursor res = cursor.execute(""" SELECT DISTINCT SCHEMAname ||'.'||tablename FROM pg_tables WHERE schemaname NOT IN ('pg_catalog','information_schema') limit 10""").fetchall() for value in res: print(value[0]) res2 = cursor.execute("""show table """ + value[0] + """ """).fetchone() # Write the result to the file with open(output_file, 'a') as f: f.write('-- Table: ' + value[0] + '\n') f.write(str(res2[0]) + '\n\n')