title: How to pass in psql password from program on Windows
body: <p>I am currently working on PostgreSQL backup and restore functionality for my project. I have read this <a href="http://www.codeproject.com/Articles/37154/PostgreSQL-PostGis-Operations" rel="nofollow">http://www.codeproject.com/Articles/37154/PostgreSQL-PostGis-Operations</a>  article and followed that approach to do this. It is working fine but recently I have changed the PostgreSQL authentication method to <code>password</code> in the <code>pg_hba.con</code> file. Hence it started prompting for the password whenever I execute <code>psql.exe</code>, <code>pg_dump.exe</code>, and <code>pg_restore.exe</code>. To provide the password through my project, I have used the "RedirectStandardInput" method. But it did not work and <code>psql</code> or <code>pg_dump</code> still prompt for the password. However "RedirectStandardOutput" and error methods are working fine. </p>
  
  <p>I went through the PostgreSQL source code and found that <code>GetConsoleMode</code> and <code>SetConsoleMode</code> are used the remove the echo. I hope ( not sure ) it could be the reason, which is why I am unable to redirect the input.</p>
  
  <p>PostgreSQL source code to prompt the password </p>
  
  <pre><code>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") &amp;&amp; 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), &amp;t);
          t_orig = t;
          t.c_lflag &amp;= ~ECHO;
          tcsetattr(fileno(termin), TCSAFLUSH, &amp;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 &gt; 0 &amp;&amp; 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 &gt; 0 &amp;&amp; buf[buflen - 1] != '\n');
      }
  
      if (length &gt; 0 &amp;&amp; destination[length - 1] == '\n')
          /* remove trailing newline */
          destination[length - 1] = '\0';
  #ifdef HAVE_TERMIOS_H
      if (!echo)
      {
          tcsetattr(fileno(termin), TCSAFLUSH, &amp;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;
  }
  </code></pre>
  
  <p>Please help me here, how to send the password to <code>psql</code> or <code>pg_dump</code> via C# code.</p>