Hannibal
Hannibal

Reputation: 15

call postgresql pg_dump.exe from visual basic

Im trying to do a backup of my database from my application made in visual basic (visual studio 2012)

I copy the pg_dump.exe with the necessary dll files to the application root.

Test the pg_dump doing a backup from cmd window and goes ok

This is the code i use to try to call the pg_dump exe but apparently does not receive the parameters i'm trying to send.

' New ProcessStartInfo created
    Dim p As New ProcessStartInfo
    Dim args As String = "-h serverip -p 5432 -U postgres db_name > " & txtPath.Text.ToString

    ' Specify the location of the binary
    p.FileName = "pg_dump.exe"

    ' Use these arguments for the process
    p.Arguments = args

    ' Use a hidden window
    p.WindowStyle = ProcessWindowStyle.Maximized

    ' Start the process
    Process.Start(p)

When the process start i get this msg:

pd_dump: too many command-line arguments (first is ">") Try "pg_dump --help" for more information

if i type this in cmd the backup is done ok pg_dump.exe -h serverip -p 5432 -U postgres db_name > c:\db_bak.backup

But i cant make it work from visual.

Upvotes: 0

Views: 1159

Answers (1)

Craig Ringer
Craig Ringer

Reputation: 324851

First, your command is just plain wrong. You're invoking pg_dump, which wants to read from a file, so you don't want to use >. That'd write to the file and overwrite it in the process. Instead you'd want <, the "read file as standard input" operator.

That's not the immediate cause of your error though. pg_dump doesn't understand >, <, etc. These operations instruct the shell (cmd.exe) to do I/O redirection. If you're not running cmd.exe then they don't work. To get I/O redirection (>, etc) you need to run the process via the cmd shell, not invoke it directly.

In this case it's probably better to use the -f filename option to pg_dump to tell it to write to a file instead of standard output. That way you avoid I/O redirection and don't need the shell anymore. It should be as simple as:

Dim args As String = "-h serverip -p 5432 -U postgres db_name -f " & txtPath.Text.ToString

Alternately, you can use cmd /C to invoke pg_dump via the command shell. Visual Basic might offer a shortcut way to do that; I don't use it, so I can't comment specifically on the mechanics of process invocation in Visual Basic. Check the CreateProcess docs; VB likely uses CreateProcess under the hood.

Personally I recommend that you do

Upvotes: 2

Related Questions