Problems restoring a database in PostgreSQL with C #

1
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 various 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 executes but all lines appear "invalid command". What would it take to make this code work?

How the process gets when it runs

[EDIT]MethodusedtoperformBackup

publicstringBackupDatabase(stringCaminhoNome){stringserver=clsConfigBanco.SERVERNAME;stringport=clsConfigBanco.PORT;stringuser=clsConfigBanco.USERNAME;stringpassword=clsConfigBanco.PASSWORD;stringdbname=clsConfigBanco.DATABASENAME;stringbackupCommandDir=@"C:\Program Files\PostgreSQL.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 "";
    }


 }
    
asked by anonymous 14.09.2017 / 19:44

1 answer

0

I think you're using Process.Start wrongly. 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've taken the example from this site: link

Other than that, apparently I did not find anything wrong. If possible, enter more information, how the dump is, and the error messages that appear.

    
14.09.2017 / 19:51