Lesnic

Imigration

Apr 22nd, 2021 (edited)
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. import psycopg2
  2.  
  3. path = 'C:/im/'
  4. fields = open(path + 'field.txt', 'w')
  5. con = psycopg2.connect(
  6.     database="dvd",
  7.     user="postgres",
  8.     password="postgres",
  9.     host="127.0.0.1",
  10.     port="5432"
  11. )
  12. cursor = con.cursor()
  13. cursor.execute('SELECT table_name FROM information_schema.tables \
  14. WHERE table_schema NOT IN (\'information_schema\',\'pg_catalog\');')
  15. tables = []
  16. for row in cursor:
  17.     tables += [row[0]]
  18. for row in tables:
  19.     new_file = open(path + row + '.csv', 'w')
  20.     new_file.close()
  21.  
  22.     cursor.execute('SELECT column_name, data_type \
  23. FROM INFORMATION_SCHEMA.COLUMNS \
  24. WHERE table_name = \'' + row + '\';')
  25.     temp = [row]
  26.     for i in cursor:
  27.         temp += [str(i)]
  28.     fields.write(' '.join(temp) + '\n')
  29.  
  30.     cursor.execute('COPY (SELECT * FROM "' + row + '") TO \'' + path + row + '.csv\' CSV;')
  31. fields.close()
  32.  
Add Comment
Please, Sign In to add comment