簡體   English   中英

如何從Windows上的程序傳遞psql密碼

[英]How to pass in psql password from program on Windows

我目前正在為我的項目開發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不會詢問密碼。 如果您想完全避免提供密碼,可以使用.pgpass。 通常,您不想從命令行指定環境變量,因為環境變量可能會顯示給其他用戶,但這在這里並不重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM