2

I'm creating Process to run pg_dump.exe in C#

var processStartInfo = new ProcessStartInfo
{
    Arguments = @"-U postgres -W -f D:\postgres\test123_dump.sql postgres",
    CreateNoWindow = true,
    FileName = @"C:\PostgreSQL\bin\pg_dump.exe",
    UseShellExecute = false,
    WindowStyle = ProcessWindowStyle.Hidden,
    RedirectStandardInput = true
};

Process process = new Process() { StartInfo = processStartInfo, EnableRaisingEvents = true };

process.Start();

using( StreamWriter sw = process.StandardInput)
{
    sw.WriteLine("123"); // test password
};

It will run pg_dump.exe, it will show prompt to pass the password, but StreamWriter seems to not work for some reason.

MTS
  • 104
  • 7

1 Answers1

4

You could use this string to put your authentication info directly in argument list

var processStartInfo = new ProcessStartInfo
    {
        Arguments = @"--dbname=postgresql://user_name:pass_word@Localhost:5432/bd_name_to_save -F c -b -f output_bd_name",
        CreateNoWindow = true,
        FileName = @"C:\PostgreSQL\bin\pg_dump.exe",
        UseShellExecute = false,
        WindowStyle = ProcessWindowStyle.Hidden,
        RedirectStandardInput = true
    };
    
    Process process = new Process() { StartInfo = processStartInfo, EnableRaisingEvents = true };
    
    process.Start();
Romylussone
  • 773
  • 1
  • 8
  • 19
  • That's working, but is there any other method like my StreamWriter to pass password? – MTS Jun 14 '22 at 09:30
  • 1
    checks here , there are another way by saving the password : https://stackoverflow.com/questions/2893954/how-to-pass-in-password-to-pg-dump – Romylussone Jun 14 '22 at 09:34