【问题标题】:How to pass in psql password from program on Windows如何在 Windows 上的程序中传递 psql 密码
【发布时间】:2012-02-17 10:43:31
【问题描述】:

我目前正在为我的项目开发 PostgreSQL 备份和恢复功能。我已经阅读了这篇http://www.codeproject.com/Articles/37154/PostgreSQL-PostGis-Operations 文章并遵循了这种方法来做到这一点。它工作正常,但最近我在pg_hba.con 文件中将PostgreSQL 身份验证方法更改为password。因此,每当我执行psql.exepg_dump.exepg_restore.exe 时,它就会开始提示输入密码。为了通过我的项目提供密码,我使用了“RedirectStandardInput”方法。但它不起作用,psqlpg_dump 仍然提示输入密码。但是“RedirectStandardOutput”和错误方法工作正常。

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

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# 代码将密码发送到 psqlpg_dump

【问题讨论】:

  • 它可以在一个名为 PGPASSWORD 的环境变量中传递,在这种情况下 psql 或 pg_dump 不会要求它。
  • 你需要.pgpass
  • 代码强制输入来自控制台而不是任何重定向,以便用户(而不是代码)输入密码。这在许多提示输入密码的程序中很常见。正如其他评论者所说,有一些解决方法。
  • 感谢 Daniel,在 Windows XP 和 Windows 7 中
  • 感谢 Daniel Vérité,我浏览了此链接 postgresql.org/docs/9.1/interactive/libpq-envars.html 中的 PGPASSWORD 信息,并建议使用此选项。另外,我正在运行在 LOCAL SERVICE 帐户下运行的 postgres 服务器服务。

标签: postgresql winapi psql


【解决方案1】:

由于这是应用程序本地的,最好的办法是设置 %PGPASSWORD% 然后 psql 不会要求输入密码。如果您想完全避免提供密码,可以使用 .pgpass。通常,您不想从命令行指定环境变量,因为它们可能会显示给其他用户,但这不是问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-11
    • 2021-09-27
    • 2019-01-23
    • 1970-01-01
    • 2015-04-16
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多