현재 내 프로젝트의 PostgreSQL 백업 및 복원 기능을 수행 중입니다. 이 http://www.codeproject.com/Articles/37154/PostgreSQL-PostGis-Operations 기사를 읽었으며이를 수행하는 방법을 따랐습니다. 잘 작동하지만 최근에 PostgreSQL 인증 방법을 pg_hba.con
파일의 password
으로 변경했습니다. 따라서 psql.exe
, pg_dump.exe
및 pg_restore.exe
을 실행할 때마다 암호를 묻는 메시지가 나타납니다. 내 프로젝트를 통해 암호를 제공하기 위해 "RedirectStandardInput"메서드를 사용했습니다. 그러나 작동하지 않고 psql
또는 pg_dump
은 여전히 암호를 묻습니다. 그러나 "RedirectStandardOutput"및 오류 메서드가 제대로 작동합니다.Windows의 프로그램에서 psql 암호를 전달하는 방법
나는 PostgreSQL 소스 코드를 살펴본 결과 GetConsoleMode
과 SetConsoleMode
이 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
에 암호를 전송하는 방법, 여기 제발 도와주세요 암호를 프롬프트합니다.
운영 체제는 무엇입니까? – Daniel
PGPASSWORD라는 환경 변수에 전달할 수 있습니다.이 경우 psql 또는 pg_dump는 묻지 않습니다. –
[.pgpass] (http://www.postgresql.org/docs/current/static/libpq-pgpass.html)가 필요합니다. –