Skip to content

Instantly share code, notes, and snippets.

@yolabingo
Last active October 30, 2020 06:24
Show Gist options
  • Save yolabingo/62401318f526a48bcf59c103b9cd8327 to your computer and use it in GitHub Desktop.
Save yolabingo/62401318f526a48bcf59c103b9cd8327 to your computer and use it in GitHub Desktop.
separate a pg_dump file into individual tables
# first `mkdir tables`
dump_file = 'DUMPFILE.sql'
with open(dump_file, 'r') as sql:
table = None
for line in sql.readlines():
if line.startswith('COPY public.'):
t = line.split()[1]
print(f"opening {t}")
table = open(f"tables/{t}", 'w')
if table:
table.write(line)
if line.strip() == '\.':
print('closing table')
table.close()
table = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment