Question

I'm building an application using Lazarus Pascal + Postgres on Ubuntu 12.04.

The following line of code works fine form the terminal:

/usr/bin/psql --host localhost --port 5432 --username "postgres" --quiet "dbHRS" < "t2"

However, it doesn't seem to run from within my Pascal program.

The restore lines are stored in a text file like this:

/usr/bin/psql
--host localhost --port 5432 --username "postgres" --quiet "dbHRS" < "%s"

My Pascal code takes the two lines and replaces the %s with the actual file name and runs it like this:

    AssignFile(RPath, 'restore.inf'); //Name of text file
    Reset(RPath);
    xx:= 0; //just a dummy value. If it changes due to an error, it's easy to know

    Readln(RPath, FileString);
    ExecProgram:= FileString;

    Readln(RPath, FileString);
    ExecParams:= Format(FileString, [txtOpenName.Text]);

    CloseFile(RPath);
    ShowMessage('"' + ExecProgram + '"'); //shows correctly
    ShowMessage('"' + ExecParams + '"'); //shows correctly
    try
      ExecStatus := SysUtils.ExecuteProcess(UTF8ToSys(ExecProgram), ExecParams, []);

    Except
      on EOSError do xx:= ExecStatus; //This never occurs
    end;

ExecStatus is 0 and xx is is still 5. So I assume there are no errors.

What could be wrong? Why is the data not coming back into the database? The tables are empty, which means that the restore didn't happen.

BTW, I also have a back-up script in a similar way and it works fine. The data is correct. This is how I created the backup file.

Any assistants on this is greatly values. Thanks!

ADDITIONAL INFO: I can run the following command using ExecuteProcess:

/usr/bin/pg_dump

--host localhost --port 5432 --username "postgres" --role "mizk" --no-password  --format plain --data-only --inserts --column-inserts --verbose --file "abc" "dbHRS"

The above 2 lines indicate the ExecProgram and ExecParams used. So this is why I'm puzzled by the issue at hand.

Was it helpful?

Solution

Executeprocess executes using execve(2), and thus only runs a program + parameters, not a full shell with redirection.

So the pipe-in construct won't work, and probably the quoting is also wrong. (since these quotes won't be stripped and will be passed to the receiving program).

For unix specific shell use, use unix.fpsystem() which is system(3) alike.

Alternately, you could do something like

    s:=ExecProgram+' ' +ExecParams;
    ExecStatus:=Executeprocess ('/bin/sh',['-c',s]);

It's been a while since I wrote executeprocess (2004-2005?), but afaik it does not raise exceptions as a part of normal usage.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top