Recent

Author Topic: TProcess.Input.Write and Postgressql 13 pg_dump utility  (Read 905 times)

noszone

  • New Member
  • *
  • Posts: 46
TProcess.Input.Write and Postgressql 13 pg_dump utility
« on: April 12, 2021, 10:14:43 am »
I've tried several ways to get the desired result, but can't find any working case. Probably more expirienced people can give an advise?

I'm trying to pass a Postgres pass to the input as following:

Code: Pascal  [Select][+][-]
  1. lStr :=TStringStream.Create('');
  2.  
  3.       lProc :=TProcess.Create(nil);
  4.       lProc :=TProcess.Create(Self);
  5.        lProc.Options:=[poUsePipes];
  6.        //lProc.ShowWindow:=swoHide;
  7.         lProc.Executable:='C:\Program Files\PostgreSQL\13\bin\pg_dump.exe';
  8.         lProc.Parameters.Add('-U');
  9.   lProc.Parameters.Add('postgres');
  10.   lProc.Parameters.Add('-h');
  11.   lProc.Parameters.Add('localhost');
  12.   lProc.Parameters.Add('-p');
  13.   lProc.Parameters.Add('5432');
  14.   lProc.Parameters.Add('-F');
  15.   lProc.Parameters.Add('custom');
  16.   lProc.Parameters.Add('-b');
  17.   lProc.Parameters.Add('-v');
  18.   lProc.Parameters.Add('-f');
  19.   lProc.Parameters.Add(mydb.backup');
  20.  lProc.Parameters.Add('mydb');
  21.  //lProc.Environment.Append('PGPASSWORD=superuser');
  22.  //lProc.CommandLine :='SET PGPASSWORD="superuser"';
  23.  
  24.  lProc.Execute;
  25.  //lpass1:='PGPASSWORD="superuser"'+LineEnding;
  26.  //lProc.Input.Write(lPass1[1],Length(lPass1));
  27.   //st:=lProc.Output.ReadAnsiString;
  28.  //showmessage(st);
  29.  lPass :='superuser'+LineEnding;
  30.  lProc.Input.Write(lPass[1],Length(lPass));
  31.  
  32.  
  33.  repeat
  34.    lBytesRead :=lProc.Stderr.Read(lBuffer,BUF_SIZE);
  35.    lStr.Write(lBuffer,lBytesRead);
  36.    Memo1.Text:=lStr.DataString;
  37.    Application.ProcessMessages;
  38.  
  39.  until lBytesRead=0;
  40.  
  41.  if lProc.ExitStatus=0 then
  42.     ShowMessage('Proses OK')
  43.  else
  44.    ShowMessage('Proses Fail');
  45.  
  46.  lProc.Free;
  47.  lStr.Free;    

What I got is the pg_dump window asking for pass. As I understand this exaple can work fine in Linux, but my system is Win10. The idea of this is not to store a password in flat file, at least in exe file it will be protected somehow. Isn't is true?

pg_dump is a console utility for making a database backup.

Another idea as you can see commented out - is to set a pass before all other commands go in process. Any help would be appreciated, thanks.                         
« Last Edit: April 12, 2021, 01:18:52 pm by noszone »

noszone

  • New Member
  • *
  • Posts: 46
Re: TProcess.Input.Write and Postgressql 13 pg_dump utility
« Reply #1 on: April 12, 2021, 10:30:32 am »
Also posting here a full working code ready for reproduce:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   Windows, Process,FileUtil,LazUTF8;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     Memo1: TMemo;
  19.     procedure Button1Click(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25. const
  26.   BUF_SIZE = 2048; // Buffer size for reading the output in chunks
  27. var
  28.   Form1: TForm1;
  29.   lProc: TProcess;
  30.   lBytesRead    : longint;
  31.   lBuffer       : array[1..BUF_SIZE] of byte;
  32.   lStr          : TStringStream;
  33.   lPass,lPass1,st         : string;
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38. { TForm1 }
  39.  
  40. procedure TForm1.Button1Click(Sender: TObject);
  41. begin
  42.  
  43.       lStr :=TStringStream.Create('');
  44.  
  45.       lProc :=TProcess.Create(nil);
  46.       lProc :=TProcess.Create(Self);
  47.        lProc.Options:=[poUsePipes];
  48.        //lProc.ShowWindow:=swoHide;
  49.         lProc.Executable:='C:\Program Files\PostgreSQL\13\bin\pg_dump.exe';
  50.         lProc.Parameters.Add('-U');
  51.   lProc.Parameters.Add('postgres');
  52.   lProc.Parameters.Add('-h');
  53.   lProc.Parameters.Add('localhost');
  54.   lProc.Parameters.Add('-p');
  55.   lProc.Parameters.Add('5432');
  56.   lProc.Parameters.Add('-F');
  57.   lProc.Parameters.Add('custom');
  58.   lProc.Parameters.Add('-b');
  59.   lProc.Parameters.Add('-v');
  60.   lProc.Parameters.Add('-f');
  61.   lProc.Parameters.Add('mydb.backup');
  62.   lProc.Parameters.Add('mydb');
  63.   //lProc.Environment.Append('PGPASSWORD=superuser');
  64.   //lProc.CommandLine :='SET PGPASSWORD="superuser"';
  65.  
  66.   lProc.Execute;
  67.   //lpass1:='PGPASSWORD="superuser"'+LineEnding;
  68.   //lProc.Input.Write(lPass1[1],Length(lPass1));
  69.    //st:=lProc.Output.ReadAnsiString;
  70.   //showmessage(st);
  71.   lPass :='superuser'+LineEnding;
  72.  
  73.   lProc.Input.Write(lPass[1],Length(lPass));
  74.  
  75.  
  76.   repeat
  77.  
  78.     lBytesRead :=lProc.Stderr.Read(lBuffer,BUF_SIZE);
  79.  
  80.  
  81.     lStr.Write(lBuffer,lBytesRead);
  82.  
  83.  
  84.     Memo1.Text:=lStr.DataString;
  85.  
  86.     Application.ProcessMessages;
  87.  
  88.   until lBytesRead=0;
  89.  
  90.   if lProc.ExitStatus=0 then
  91.      ShowMessage('Proses OK')
  92.   else
  93.     ShowMessage('Proses Fail');
  94.  
  95.   lProc.Free;
  96.   lStr.Free;
  97.  
  98.   end;
  99. end.
  100.  
« Last Edit: April 12, 2021, 01:19:09 pm by noszone »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: TProcess.Input.Write and Postgressql 13 pg_dump utility
« Reply #2 on: April 12, 2021, 01:10:18 pm »
Please use [ code ] blocks next time to increase readability.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: TProcess.Input.Write and Postgressql 13 pg_dump utility
« Reply #3 on: April 12, 2021, 01:29:00 pm »
Use the same command to run a dummy script ("batch file" or whatever the correct terminology is) that echoes the parameters and environment variables. Check that -h etc. is still the correct form of option, and that they're still positioned correctly on the command line.

Then before ANYTHING else I suggest that you look at v13 documentation and if necessary the pg_dump source to check whether it's using some other Windows API to get the password from the user.

I can't check this myself, since I don't have pg13 and don't run Windows. But even on Linux I seem to recall that the most reliable way of doing this was to have a password etc. in the appropriate user's home directory.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018