0
I created a . bat to restore a database in Postgresql and it worked perfectly using the following command:
set PGPASSWORD=postgres123
C:\Progra~1\PostgreSQL\9.4\bin\pg_restore.exe -i -h localhost -p 5432
-U postgres -c -d dbrestore -v D:\bkp.backup
Now I want to run a bat to do this in C#. I’m running this function:
static void Restore()
{
Environment.SetEnvironmentVariable("PGPASSWORD", clsConfigBanco.PASSWORD);
const string exe = @"C:\Progra~1\PostgreSQL\9.4\bin\pg_restore.exe";
const string arg = "-i - h localhost - p 5432 - U postgres - c - d dbrestore - v D:\bkp.backup";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = exe;
startInfo.Arguments = arg;
try
{
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
// Log error.
}
}
The problem is that this function doesn’t even run the bat. it just blinks and disappears. how do I make a function to run this bat? and I have to generate it at runtime because not always the variables will be the same
Just one note: in this context, there is no more .bat. bat is a batch file, with multiple commands only. The correct title would be: "Restore postgresql database in c#"
– Rovann Linhalis
At the end of the args string? I just did that and it didn’t generate any.txt files for me
– Bruno Silva
Strange, when I run System.Diagnostics.Process.Start(Path+""+"Restore 2.bat") works perfectly, but when I try to create it doesn’t work
– Bruno Silva
Okay, I tried to make this method and it didn’t work. could I put a pause command will? to see the possible error. but the error should be in the execution of the process and not in the command I think
– Bruno Silva
I will try now a moment, I leave the Environment.Setenvironmentvariable("PGPASSWORD", clsConfigBanco.PASSWORD); in the first line ?
– Bruno Silva
also did not work
– Bruno Silva
checks, that in the arguments, there is no @ before the string, it may be giving problem in the file path. No error pq b is a special character
– Rovann Linhalis