如何将密码标准输入传递给PG_DUMP?

发布于 2025-02-07 01:39:59 字数 675 浏览 1 评论 0原文

我正在创建流程以在C#中运行PG_DUMP.EXE

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
};

,它将运行PG_DUMP.EXE,它将显示提示传递密码,但是StreamWriter似乎由于某种原因不起作用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫思念不要吵 2025-02-14 01:39:59

您可以使用此字符串将您的身份验证信息直接放在参数列表中

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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文