Problems restoring a Postgresql database with C#

Asked

Viewed 456 times

4

private void btnRestore_Click(object sender, EventArgs e)
{
    if (clsB.ConectaBanco())
    {
        //Executo a seguinte função para limpar a base de dados, para poder dar o restore.
        clsB.ExecutarSQL("drop schema public cascade; create schema public;");

        //E executo o seguinte processo
        string Comando = CaminhoPg + @"psql -U postgres -d restore2 -f C:\Users\bruhh\Desktop\Backup\back.backup";

        Process p = new System.Diagnostics.Process();
        p.StartInfo.FileName = CaminhoPg + @"psql" ;
        p.StartInfo.Arguments = @"-U postgres -d restore2 -f C:\Users\bruhh\Desktop\Backup\back.backup";

        p.Start();

        p.WaitForExit();
        p.Close();

        MessageBox.Show(Comando);
    }
    else
        MessageBox.Show("Ocorreu um erro ao carregar as configurações do banco de dados! \nvá em Configurações\\Banco De Dados");
}

The result of the variable command is

C:\\Program Files\\PostgreSQL\\9.4\\bin\\psql -U postgres -d restore2 -f C:\\Users\\bruhh\\Desktop\\Backup\\back.backup

The process even runs but on all lines appears "invalid command". What would be necessary to make this code work?

How the process looks when it is executed Processo executado

[EDIT] Method used to perform Backup

public string BackupDatabase(string CaminhoNome)
{
    string server = clsConfigBanco.SERVERNAME;
    string port = clsConfigBanco.PORT;
    string user = clsConfigBanco.USERNAME;
    string password = clsConfigBanco.PASSWORD;
    string dbname = clsConfigBanco.DATABASENAME;
    string backupCommandDir = @"C:\Program Files\PostgreSQL\9.4\bin";
    try
    {
       Environment.SetEnvironmentVariable("PGPASSWORD", password);

       string backupFile = CaminhoNome;
       string BackupString = "-ibv -Z3 -f \"" + backupFile + "\" " +
                "-Fc -h " + server + " -U " + user + " -p " + port + " " + dbname;

       Process p = new System.Diagnostics.Process();
       p.StartInfo.FileName = CaminhoPg + "\\pg_dump.exe";
       p.StartInfo.Arguments = BackupString;

       p.Start();

       p.WaitForExit();
       p.Close();

       return backupFile;
   }
   catch
   {
      return "";
    }


 }
  • shows how your dump is, at least the beginning of it

  • What would this dump be?

  • Hi? The file generated by 'pg_dump' that would be your backup... In your case, "back."

  • You’re picking up some null and junk values. Very strange, apparently you’re having trouble with this back. but I think it’s the problem, because by pg’s Store it works, follow the link to download the https://1drv.ms/u/sfile! Aronqw2yo1tchi963aaysahjupmasg

  • There’s something wrong with this dump your... rs like it’s doing it ?

  • I updated the question with the method for performing the backup

Show 1 more comment

1 answer

1

I believe you’re using the Process.Start wrong way. See the example:

static void LaunchCommandLineApp()
{
    // For the example.
    const string exe = "C:\\Program Files\\PostgreSQL\\9.4\\bin\\psql";
    const string arg = "-U postgres -d restore2 -f C:\\Users\\bruhh\\Desktop\\Backup\\back.backup";

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

    try
    {
        // Start the process with the info we specified.
        // Call WaitForExit and then the using-statement will close.
        using (Process exeProcess = Process.Start(startInfo))
        {
            exeProcess.WaitForExit();
        }
    }
    catch
    {
        // Log error.
    }
}

I removed the example of this site: https://www.dotnetperls.com/process

other than that, apparently I didn’t find anything wrong. If possible, put more information, how is the dump and the error messages that appear.

  • Updates with an image of the process, look how it occurs when it runs, I thought the error was referring to the backup (should be in trouble) but when I use it by postgre it works normally

Browser other questions tagged

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