Skip to content

Instantly share code, notes, and snippets.

@ledger123
Created July 22, 2024 08:12
Show Gist options
  • Select an option

  • Save ledger123/0afd9c74bc59ea03ac8a2f2e40872f9a to your computer and use it in GitHub Desktop.

Select an option

Save ledger123/0afd9c74bc59ea03ac8a2f2e40872f9a to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
my $username = 'postgres';
my $sql_file = 'dbchanges.sql';
my @databases = `psql -U $username -l -t | cut -d '|' -f 1`;
foreach my $db (@databases) {
$db =~ s/^\s+|\s+$//g;
}
foreach my $db (@databases) {
next if $db eq 'template0' || $db eq 'template1'; # Skipping system databases
my $command = "psql -U $username $db < $sql_file";
system($command) == 0 or die "Failed to execute command: $command\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment