0

現在、プロジェクトの PostgreSQL バックアップおよび復元機能に取り組んでいます。私はこのhttp://www.codeproject.com/Articles/37154/PostgreSQL-PostGis-Operationsの記事を読み、 そのアプローチに従ってこれを行いました。正常に動作していますが、最近、PostgreSQL 認証方法をファイル内に変更しpasswordました。したがって、、、およびpg_hba.conを実行するたびにパスワードの入力を求められるようになりました。プロジェクトを通じてパスワードを提供するために、「RedirectStandardInput」メソッドを使用しました。しかし、それは機能せず、パスワードの入力を求められました。ただし、「RedirectStandardOutput」とエラー メソッドは正常に動作しています。psql.exepg_dump.exepg_restore.exepsqlpg_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;
}

psqlここで、パスワードを C# コードに、またはpg_dumpC# コード経由で送信する方法を教えてください。

4

1 に答える 1

0

これはアプリケーションに対してローカルであるため、最善の方法は %PGPASSWORD% を設定することです。そうすれば、psql はパスワードを要求しなくなります。.pgpass は、パスワードの提供をまったく避けたい場合に使用できます。一般に、環境変数は他のユーザーに表示される可能性があるため、コマンド ラインから環境変数を指定することは望ましくありませんが、ここでは問題になりません。

于 2013-03-25T09:25:47.303 に答える