Skip to content

Instantly share code, notes, and snippets.

@artifactsauce
Last active March 13, 2024 13:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artifactsauce/8035314 to your computer and use it in GitHub Desktop.
Save artifactsauce/8035314 to your computer and use it in GitHub Desktop.
Convert from a pg_dump data file to TSV files for LOAD DATA INFILE.
#!/usr/bin/env perl
use strict;
use warnings;
use Path::Class;
use FindBin;
my $output_dir = dir($FindBin::Bin,"data");
$output_dir->is_dir or die "[ERROR] $output_dir is not a directory.";
my $input_file = file( $ARGV[0] );
my $ifh = $input_file->open('r') or die $!;
while ( my $line = $ifh->getline ) {
next unless $line =~/^COPY (\S+)/;
my $output_file = $output_dir->file( $1.'.txt' );
my $ofh = $output_file->open('w') or die $!;
create_data_file( $ifh, $ofh );
}
exit;
sub create_data_file {
my ( $input, $output ) = @_;
while ( my $line = $input->getline ) {
last if $line =~/^\\\.$/;
print $output $line;
}
$output->close;
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment