Skip to content

Instantly share code, notes, and snippets.

@rhanka
Last active January 25, 2018 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhanka/2f11a235482e3f9bb81dbbd4157461f6 to your computer and use it in GitHub Desktop.
Save rhanka/2f11a235482e3f9bb81dbbd4157461f6 to your computer and use it in GitHub Desktop.
Dirty pg_restore + perl conversion
#!/bin/sh
#get all schemas of the dump
schemas=`cat $1 | docker exec -i postgres pg_restore -l | grep SCHEMA | sed 's/^.*\s//' | sort | uniq`
for schema in $schemas
do echo $schema
for table in `cat $1 | docker exec -i postgres pg_restore -l | egrep "TABLE $schema" |sed "s/.*TABLE $schema //;s/ $schema//' | sort | uniq `
do echo $table
cat $1 | docker exec -i postgres pg_restore -t $table | perl -e 'while(<>){if (/^\\.\s*$/) {$dump=0} if ($dump==1) {s/\\N//g;print} if (/COPY/){$dump=1;s/.*\((.*)\).*/$1/;s/,\s*/\t/g;print}}' > $table.csv
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment