Postgresql 如何在Windows上从程序传入psql密码

Postgresql 如何在Windows上从程序传入psql密码,postgresql,winapi,psql,Postgresql,Winapi,Psql,我目前正在为我的项目开发PostgreSQL备份和恢复功能。我已经阅读了这篇文章,并遵循这种方法来做这件事。它工作正常,但最近我在pg_hba.con文件中将PostgreSQL身份验证方法更改为password。因此,每当我执行psql.exe、pg_dump.exe和pg_restore.exe时,它就会开始提示输入密码。为了通过我的项目提供密码,我使用了“RedirectStandardInput”方法。但它不起作用,psql或pg_dump仍然提示输入密码。然而,“重定向标准输出”和错误

我目前正在为我的项目开发PostgreSQL备份和恢复功能。我已经阅读了这篇文章,并遵循这种方法来做这件事。它工作正常,但最近我在
pg_hba.con
文件中将PostgreSQL身份验证方法更改为
password
。因此,每当我执行
psql.exe
pg_dump.exe
pg_restore.exe
时,它就会开始提示输入密码。为了通过我的项目提供密码,我使用了“RedirectStandardInput”方法。但它不起作用,
psql
pg_dump
仍然提示输入密码。然而,“重定向标准输出”和错误方法工作正常

我浏览了PostgreSQL源代码,发现
GetConsoleMode
SetConsoleMode
用于删除回显。我希望(不确定)这可能是原因,这就是我无法重定向输入的原因

PostgreSQL提示密码的源代码

simple_prompt(const char *prompt, int maxlen, bool echo)
{
    int         length;
    char       *destination;
    FILE       *termin,
               *termout;
#ifdef HAVE_TERMIOS_H
    struct termios t_orig,
                t;
#else
#ifdef WIN32
    HANDLE      t = NULL;
    LPDWORD     t_orig = NULL;
#endif
#endif
    destination = (char *) malloc(maxlen + 1);
    if (!destination)
        return NULL;

    /*
     * Do not try to collapse these into one "w+" mode file. Doesn't work on
     * some platforms (eg, HPUX 10.20).
     */
    termin = fopen(DEVTTY, "r");
    termout = fopen(DEVTTY, "w");
    if (!termin || !termout
#ifdef WIN32
    /* See DEVTTY comment for msys */
        || (getenv("OSTYPE") && strcmp(getenv("OSTYPE"), "msys") == 0)
#endif
        )
    {
        if (termin)
            fclose(termin);
        if (termout)
            fclose(termout);
        termin = stdin;
        termout = stderr;
    }

#ifdef HAVE_TERMIOS_H
    if (!echo)
    {
        tcgetattr(fileno(termin), &t);
        t_orig = t;
        t.c_lflag &= ~ECHO;
        tcsetattr(fileno(termin), TCSAFLUSH, &t);
    }
#else
#ifdef WIN32
    if (!echo)
    {
        /* get a new handle to turn echo off */
        t_orig = (LPDWORD) malloc(sizeof(DWORD));
        t = GetStdHandle(STD_INPUT_HANDLE);

        /* save the old configuration first */
        GetConsoleMode(t, t_orig);

        /* set to the new mode */
        SetConsoleMode(t, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
    }
#endif
#endif
    if (prompt)
    {
        fputs(_(prompt), termout);
        fflush(termout);
    }

    if (fgets(destination, maxlen + 1, termin) == NULL)
        destination[0] = '\0';

    length = strlen(destination);
    if (length > 0 && destination[length - 1] != '\n')
    {
        /* eat rest of the line */
        char        buf[128];
        int         buflen;

        do
        {
            if (fgets(buf, sizeof(buf), termin) == NULL)
                break;
            buflen = strlen(buf);
        } while (buflen > 0 && buf[buflen - 1] != '\n');
    }

    if (length > 0 && destination[length - 1] == '\n')
        /* remove trailing newline */
        destination[length - 1] = '\0';
#ifdef HAVE_TERMIOS_H
    if (!echo)
    {
        tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
        fputs("\n", termout);
        fflush(termout);
    }
#else
#ifdef WIN32
    if (!echo)
    {
        /* reset to the original console mode */
        SetConsoleMode(t, *t_orig);
        fputs("\n", termout);
        fflush(termout);
        free(t_orig);
    }
#endif
#endif
    if (termin != stdin)
    {
        fclose(termin);
        fclose(termout);
    }

    return destination;
}

请在此帮助我,如何通过C#code将密码发送到
psql
pg#u dump

由于这是应用程序的本地密码,最好设置%PGPASSWORD%,然后psql将不会要求密码。如果您想完全避免提供密码,可以使用pgpass。一般来说,您不希望从命令行指定环境变量,因为它们可能会显示给其他用户,但这里不需要考虑这一点

它可以在名为PGPASSWORD的环境变量中传递,在这种情况下,psql或pg_dump不会要求它。您需要。代码强制输入来自控制台,而不是任何重定向,以便用户(而不是代码)输入密码。这在许多提示输入密码的程序中很常见。正如其他评论者所指出的,这里有一些变通方法。谢谢Daniel,在Windows XP和Windows 7中谢谢Daniel Vérité,我在这个链接中浏览了PGPASSWORD信息,建议使用这个选项。此外,我正在本地服务帐户下运行postgres服务器服务。