How can I create a restore using PostgreSql 9.4 and C #? I'm using this method in question, but when I run the process it does not work! whats wrong?
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("There was an error loading database settings");
}
The result of the various Comando 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 in all the lines "invalid command" appears. What would it take to make this code work?
How the process gets when it runs https://i.stack.imgur.com/2AUvL.png
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 "";
}
}
Apparently the backup is correct because when I generate the backup I can give restore by PostgreSql9.4 but I can not with the application. my backup file also gets some strange characters is this normal? Here is a system-generated backup
How do I get my restore to work?