From fc5483972bf9bb16686a35b883129f1fa05352af Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Sat, 25 Feb 2023 23:00:46 -0800 Subject: [PATCH] Allow custom login for restore command --- postgresql/postgresql-lib.pl | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/postgresql/postgresql-lib.pl b/postgresql/postgresql-lib.pl index 1411a99be..4b3fb45b2 100755 --- a/postgresql/postgresql-lib.pl +++ b/postgresql/postgresql-lib.pl @@ -1170,25 +1170,28 @@ if ($? || $out =~ /could not|error|failed/i) { return undef; } -# restore_database(database, source-path, only-data, clear-db, [&only-tables]) +# restore_database(database, source-path, only-data, clear-db, [&only-tables], +# [login, password]) # Restores the contents of a PostgreSQL backup into the specified database. # Returns undef on success, or an error message on failure. sub restore_database { -my ($db, $path, $only, $clean, $tables) = @_; +my ($db, $path, $only, $clean, $tables, $login, $pass) = @_; my $tablesarg = join(" ", map { " -t ".quotemeta('"'.$_.'"') } @$tables); +$login ||= $postgres_login; +$pass ||= $postgres_pass; my $cmd = "e_path($config{'rstr_cmd'}). &host_port_flags(). - (!$postgres_login ? "" : - &supports_pgpass() ? " -U $postgres_login" : " -u"). + (!$login ? "" : + &supports_pgpass() ? " -U $login" : " -u"). ($only ? " -a" : ""). ($clean ? " -c" : ""). $tablesarg. " -d $db "."e_path($path); -if ($postgres_sameunix && defined(getpwnam($postgres_login))) { - $cmd = &command_as_user($postgres_login, 0, $cmd); +if ($postgres_sameunix && defined(getpwnam($login))) { + $cmd = &command_as_user($login, 0, $cmd); } -$cmd = &command_with_login($cmd); +$cmd = &command_with_login($cmd, $login, $pass); my $out = &backquote_logged("$cmd 2>&1"); if ($? || $out =~ /could not|error|failed/i) { return $out;