I making Backup and Restore to system.
For make Backup im using this code:
textInformacao.Text = "";
Environment.SetEnvironmentVariable("PGPASSWORD", ConfiguracaoSistema.Password);
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = @Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\backup\\pg_dump.exe";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = false;
psi.CreateNoWindow = true;
psi.Arguments = string.Format(@"-h {0} -p {1} -U {2} -F c -b -v -f {3} {4}", caminhoServidor, ConfiguracaoSistema.Port, ConfiguracaoSistema.UserName, arquivoSaida, ConfiguracaoSistema.NomeDataBase);
psi.UseShellExecute = false;
p = Process.Start(psi);
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.EnableRaisingEvents = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.OutputDataReceived += new DataReceivedEventHandler((s, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += e.Data + "\r\n"; textInformacao.ScrollToEnd(); })); });
p.ErrorDataReceived += new DataReceivedEventHandler((s, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += e.Data + "\r\n"; textInformacao.ScrollToEnd(); })); });
p.Exited += (sender, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += "Processo Finalizado!"; })); };
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
If I get this file and open pgAdmin, all will do right, database will be restored.
for make restore im using this code:
textInformacao.Text = "";
Environment.SetEnvironmentVariable("PGPASSWORD", ConfiguracaoSistema.Password);
string caminhoArquivo = @Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\testesigep_vs.backup";
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = @Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\backup\\pg_restore.exe";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = false;
psi.CreateNoWindow = true;
psi.Arguments = string.Format(@"-h {0} -p {1} -U {2} -c -d {3} -v {4}", caminhoServidor, ConfiguracaoSistema.Port, ConfiguracaoSistema.UserName, ConfiguracaoSistema.NomeDataBase, caminhoArquivo);
psi.UseShellExecute = false;
p = Process.Start(psi);
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.EnableRaisingEvents = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.OutputDataReceived += new DataReceivedEventHandler((s, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += e.Data + "\r\n"; textInformacao.ScrollToEnd(); })); });
p.ErrorDataReceived += new DataReceivedEventHandler((s, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += e.Data + "\r\n"; textInformacao.ScrollToEnd(); })); });
p.Exited += (sender, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += "Processo Finalizado!"; })); };
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
when I run this code, I have a "big problem" all lines are duplicate. look my database restored:
look picture, look ID column have 2 values same.
How can I solve this problem? Why in PgAdmin Restore Have no Problem, and in c# do this problem?
EDIT:::::::::::::::::::::::::::::::::::::::::::::::::::: Start Restore
pg_restore: connecting to database for restore pg_restore: dropping DATABASE sigep_vs pg_restore: [archiver (db)] Error while PROCESSING TOC: pg_restore: [archiver (db)] Error from TOC entry 2949; 1262 158041 DATABASE sigep_vs sigep pg_restore: [archiver (db)] could not execute query: ERROR: cannot drop the currently open database Command was: DROP DATABASE sigep_vs;
pg_restore: creating DATABASE "sigep_vs" pg_restore: [archiver (db)] could not execute query: ERROR: database "sigep_vs" already exists Command was: CREATE DATABASE sigep_vs WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C' LC_CTYPE = 'C';
Environment.SetEnvironmentVariable("PGPASSWORD", ConfiguracaoSistema.Password);
string caminhoArquivo = @Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\testesigep_vs.backup";
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = @Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\backup\\pg_restore.exe";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = false;
psi.CreateNoWindow = true;
psi.Arguments = string.Format(@"-h {0} -p {1} -U {2} -d {3} -v {4}", caminhoServidor, ConfiguracaoSistema.Port, ConfiguracaoSistema.UserName, ConfiguracaoSistema.NomeDataBase, caminhoArquivo);
psi.UseShellExecute = false;
p = new Process { StartInfo = psi };
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.EnableRaisingEvents = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.OutputDataReceived += new DataReceivedEventHandler((s, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += e.Data + "\r\n"; textInformacao.ScrollToEnd(); })); });
p.ErrorDataReceived += new DataReceivedEventHandler((s, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += e.Data + "\r\n"; textInformacao.ScrollToEnd(); })); });
p.Exited += (sender, e) => { Dispatcher.BeginInvoke(new Action(() => { textInformacao.Text += "Processo Finalizado!"; })); };
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
pg_dump
andpg_restore
?pgAdmin
is a different tool. If you used the wrong parameters, saying thatpgAdmin
worked doesn't say anything – Panagiotis Kanavos-c
forpg_restore
, so it should work... Try to capture the standard output frompg_restore
, that will shed light on the matter. – Laurenz Albe