2012-02-17 2 views
0

현재 내 프로젝트의 PostgreSQL 백업 및 복원 기능을 수행 중입니다. 이 http://www.codeproject.com/Articles/37154/PostgreSQL-PostGis-Operations 기사를 읽었으며이를 수행하는 방법을 따랐습니다. 잘 작동하지만 최근에 PostgreSQL 인증 방법을 pg_hba.con 파일의 password으로 변경했습니다. 따라서 psql.exe, pg_dump.exepg_restore.exe을 실행할 때마다 암호를 묻는 메시지가 나타납니다. 내 프로젝트를 통해 암호를 제공하기 위해 "RedirectStandardInput"메서드를 사용했습니다. 그러나 작동하지 않고 psql 또는 pg_dump은 여전히 ​​암호를 묻습니다. 그러나 "RedirectStandardOutput"및 오류 메서드가 제대로 작동합니다.Windows의 프로그램에서 psql 암호를 전달하는 방법

나는 PostgreSQL 소스 코드를 살펴본 결과 GetConsoleModeSetConsoleMode이 echo를 제거하는 데 사용된다는 것을 알게되었습니다. 이유가 무엇인지 알기를 바랍니다. 이유는 입력을 리디렉션 할 수없는 이유입니다.

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# 코드를 통해 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는 암호를 전혀 제공하지 않으려는 경우 사용할 수 있습니다. 일반적으로 커맨드 라인에서 환경 변수를 지정하고 싶지는 않습니다. 왜냐하면 다른 사용자에게 표시 될 수 있기 때문입니다. 그러나 여기에서는 걱정할 필요가 없습니다.

관련 문제