Restore Postgresql Database in C#

Asked

Viewed 340 times

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#"

  • At the end of the args string? I just did that and it didn’t generate any.txt files for me

  • Strange, when I run System.Diagnostics.Process.Start(Path+""+"Restore 2.bat") works perfectly, but when I try to create it doesn’t work

  • 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

  • I will try now a moment, I leave the Environment.Setenvironmentvariable("PGPASSWORD", clsConfigBanco.PASSWORD); in the first line ?

  • also did not work

  • 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

Show 2 more comments

1 answer

1


In the following line:

const string arg = "-i - h localhost - p 5432 - U postgres - c - d dbrestore - v D:\bkp.backup";

was not placed @ before the string, which causes \ is a special character and the compiler did not present an error because \b is a special character.

You can solve this by putting the @ before the string, or using \\ in place of \

You also have to take the using:

Follows code:

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
  {
      Process exeProcess = Process.Start(startInfo);
      exeProcess.WaitForExit();
  }
  catch
  {
       // Log error.
  }
}

as I posted in another question, I use the psql to run Restore. Here, I made the following code and it works perfectly:

        Environment.SetEnvironmentVariable("PGPASSWORD", senha);
        const string exe = @"C:\Progra~2\PostgreSQL\9.1\bin\psql";
        const string arg = @"-U postgres -d db_banco -f D:\Backup\postgresql.dumpall";

        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = exe;
        startInfo.Arguments = arg;

        Process exeProcess = Process.Start(startInfo);

        exeProcess.WaitForExit();
  • Even if @ did not work, same error occurs :/

  • has using too. Remove it. here tested and it works

  • I changed Progra~1 to Program Files, added @ and removed using and just debugged the value of the exe variable consist of "C:\\Program Files\\PostgreSQL\\9.4\\bin\\pg_restore.exe" and in args "-i - h localhost - p 5432 - U postgres - c - d dbrestore - v D:\\bkp.backup". I added the exe value to the explorer and it opened the Restore. but it still doesn’t work. is any incompatibility with the WPF? but I guess not because I’m backing up and it’s working normally

  • I changed the answer by putting the code I used here and it works perfectly

  • 1

    plsql is not compatible with the backup type I am using. Restore only. if I use plsql it will say that the commands are invalid

  • With plsql it worked perfectly. I will try to change the type then of dumpall backup. you know if there is much difference between backup and dumpall?

  • know q has the dumpall that dump all the bases of sgdb, the dump that dump the informed base, and psql that I use to restore. about backup or pg_restore I don’t know how to talk =/

  • Can you tell me if there is any way for the user not to be able to close the process that is running?

  • let it hide... is some of the properties of ProcessStartInfo

  • 1

    Thanks for the Help

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.