Click here to Skip to main content
15,861,168 members
Articles / Programming Languages / C#
Tip/Trick

Backup/Restore PostgreSQL databases

Rate me:
Please Sign up or sign in to vote.
4.50/5 (5 votes)
5 Mar 2013CPOL1 min read 32.8K   4.5K   10   10
Allow your applications the ability to backup and restore your PostgreSQL databases.

Image 1

Introduction

This article provides developers the possibility to quick implement « Backup/Restore database » functionality in their applications. This library can be used from your application, and the backup operation runs in the local machine. It is not necessary to run backup or restore on the server.

Background

This library use some PostgreSQL files:

  • pg_dump.exe
  • pg_restore.exe
  • libeay32.dll
  • libiconv-2.dll
  • libintl-8.dll
  • libpq.dll
  • Npgsql.dll
  • ssleay32.dll
  • zlib1.dll

In our case this solution is tested on PostgreSQL 9.1. So if you have to use a different version, may be you need to replace files with the correct version which could be found on your PostgreSQL installation directory.

The backup/restore operation is launched in NoShell mode and the result of the execution is intercepted in an Output event which provides an output string.

Using the code

To launch Backup or Restore operation, you have to fill a config object that contains the host, username, port, and password. All public methods are written in a static class named Control.

To start backup, use the method below:

C#
//
void BackUp()
{
    pgstore.Config cnf = new Config();
    cnf.ServerName = txtHost.Text;
    if (lstDB.Items.Count > 0 && lstDB.SelectedItem!=null)
        cnf.DataBase = lstDB.SelectedItem.ToString();
    cnf.UserName = txtUserName.Text;
    cnf.Password = txtPass.Text;
    cnf.Port = Convert.ToInt16(txtPort.Text);
    pgstore.Control.CurrentConfig = cnf;
    string FileName = pgstore.Control.CurrentConfig.DataBase + "_" + DateTime.Now.Day.ToString() + 
      "_" + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString() + ".backup";
    pgstore.Control.Backup("D:\\backup" , FileName, true);
}//

To start restore, use the method below:

C#
//
void Restore(string path)
{
    pgstore.Config cnf = new Config();
    cnf.ServerName = txtHost.Text;
    if (lstDB.Items.Count > 0 && lstDB.SelectedItem!=null)
        cnf.DataBase = lstDB.SelectedItem.ToString();
    cnf.UserName = txtUserName.Text;
    cnf.Password = txtPass.Text;
    cnf.Port = Convert.ToInt16(txtPort.Text);
    pgstore.Control.CurrentConfig = cnf;
    pgstore.Control.Restaure(path, false);
}

If you have a specific option, the right way is to change it in the StoreParameters object in your CurrentConfig.Parametres.

Don’t forget to add the required PostgreSQL files to your setup project.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) B&B
Tunisia Tunisia
Microsoft DOTNET developer
VOIP Technologies
ERP Petales

http://www.declaration.tn/

Comments and Discussions

 
Praisemerci c'est intérissant ce sujet Pin
Member 1412623022-Jan-19 6:16
Member 1412623022-Jan-19 6:16 
QuestionGood Stuff, couple of fixes for newer PG's Pin
mick_lennon13-Nov-18 4:22
mick_lennon13-Nov-18 4:22 
QuestionError on conect Pin
sam suite casa de software valencia25-Feb-18 1:50
sam suite casa de software valencia25-Feb-18 1:50 
AnswerRe: Error on conect Pin
victoria19506-Feb-23 2:19
victoria19506-Feb-23 2:19 
QuestionError While Restoring Pin
bunzitop12-Oct-15 22:12
bunzitop12-Oct-15 22:12 
QuestionError Pin
Guilherme Walter2-Oct-14 17:29
Guilherme Walter2-Oct-14 17:29 
Questionthanks Pin
Member 110013288-Aug-14 11:14
Member 110013288-Aug-14 11:14 
Questionits very useful for me Pin
itzvivek20-Mar-14 23:36
itzvivek20-Mar-14 23:36 
GeneralMy vote of 4 Pin
AnOldog12-Aug-13 17:01
AnOldog12-Aug-13 17:01 
Generalthank for your article Pin
benjamin9x18-Apr-13 4:57
benjamin9x18-Apr-13 4:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.