2012-02-17 3 views
0

私のプロジェクトのPostgreSQLのバックアップとリストアの機能に取り組んでいます。私はこのhttp://www.codeproject.com/Articles/37154/PostgreSQL-PostGis-Operationsの記事を読んで、これを行う方法に従った。うまくいきましたが、最近、pg_hba.conファイルのPostgreSQL認証方法をpasswordに変更しました。したがって、私はpsql.exepg_dump.exe、およびpg_restore.exeを実行するたびにパスワードの入力を求めました。私のプロジェクトを通してパスワードを提供するために、私は "RedirectStandardInput"メソッドを使いました。しかし、それは動作しませんでした。psqlまたはpg_dumpはまだパスワードを要求します。しかし、 "RedirectStandardOutput"とエラーメソッドは正常に動作しています。Windowsのプログラムからpsqlのパスワードを渡す方法

私はPostgreSQLのソースコードを調べたところ、GetConsoleModeSetConsoleModeがエコーを除去するのに使われていることがわかりました。私はそれが理由である可能性があります(わからない)ことを願っています。そのため、入力をリダイレクトできません。パスワード

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

を促す

PostgreSQLのソースコードC#のコードを経由してpsqlまたはpg_dumpにパスワードを送信する方法を、ここで私を助けてください。

+0

どのオペレーティングシステムですか? – Daniel

+1

PGPASSWORDという名前の環境変数に渡すことができます。その場合、psqlまたはpg_dumpはそれを要求しません。 –

+2

[.pgpass](http://www.postgresql.org/docs/current/static/libpq-pgpass.html)が必要です。 –

答えて

0

これはアプリケーションにとってローカルなので、実行するには%PGPASSWORD%を設定してから、psqlはパスワードを要求しません。 .pgpassは、パスワードの入力をまったく避けたい場合に使用できます。一般的には、コマンドラインから環境変数を指定する必要はありません。他のユーザーには表示される可能性があるからですが、ここではそれは重要ではありません。

関連する問題