Connecting Tech Pros Worldwide Forums | Help | Site Map

PostgreSQL Database backup from a C# application

Newbie
 
Join Date: Sep 2008
Location: London, England
Posts: 24
#1: Sep 15 '08
I am developing a C# application and could not find any details on backing up the database using C#. I would like to provide the users of the application ability to save a backup of the database in case of any data lose or any other type of failures such as Hard disk failure and so on.

I would really appreciate if someone could provide information on this issue. The specification of the system I am using is:

OS: Windows XP Pro/Windows Vista
DB: PostgreSQL
.Net Connection for C#: Npgsql

I would really appreciate if someone could help please, and also an example in C# would be brilliant as I am kind of new in C#, my background is mainly from Java.

Thanks in advance and I look for a solution.

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Sep 30 '08

re: PostgreSQL Database backup from a C# application


One way is to use the pg_dump utility.
To invoke that (or any command) from C# you would use the System.Diagnostics.Process class in a similar way that you used Runtime.exec in Java.
Newbie
 
Join Date: Sep 2008
Location: London, England
Posts: 24
#3: Oct 1 '08

re: PostgreSQL Database backup from a C# application


Thanks, I have not tried this but it seems logical and should be the way forward.

Thanks for the suggestion, I was thingking the same thing as well.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Oct 1 '08

re: PostgreSQL Database backup from a C# application


Quote:

Originally Posted by harvindersingh

Thanks, I have not tried this but it seems logical and should be the way forward.

Thanks for the suggestion, I was thingking the same thing as well.

Let us know how it goes.
Newbie
 
Join Date: Sep 2008
Location: London, England
Posts: 24
#5: Nov 10 '08

re: PostgreSQL Database backup from a C# application


Hello guys,

I have followed the same path as mentioned, just called cmd.exe and than psql.exe/pg_dump.exe with the necessary arguments for the backup and restore. Turns out that PostgreSQL is not using the simple input streams for the password, which gets asked after you give the command line all the arguments for the process. This way it is not very easy to just use inputStream to insert the password. Also I found that input streams should only be used with command line applications as Windows Forms applications support for input stream is not very good.

So to overcome these issues I followed the pgpass.conf file creation method. I just create a file with all the details about the database and the command prompt does not ask for the password. There is also another way to achieve atomatic password recognition. This is done by setting an environment variable, I think it is called PASSWORD but please check documentation for this. So once the environment variable is set you can access the database and the utility wont ask for a password.

The approach I took was creating a file and then clearing its content so that people are asked to provide password normally. One of the main challenges, was to work with Threads to perform operations. But finally I was able to perform the tasks with BackgroundWorker Thread and things are working great. I am able to get errors that might occur during execution of the process in the background worker. Actually exceptions do not follow back to the UI threads quite a lot of times, or may be I was doing something incorrect. However to overcome this issue I just used a string that was filled whenever an excetion occured or an error occured, thi was just to catch cases that are not being caught up by the BackgroundWorker thread.

I think C# and VS is a mindblowing combination for faster development and when it comes to working with other technologies there is possibly nothing faster like this, in terms developing a full blown software from scratch in very little time and ofcourse with a lot of features.

I hope the explanation helped, I will try and post some code later if someone wants it.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#6: Nov 11 '08

re: PostgreSQL Database backup from a C# application


Great work. Running the backup from a background thread is certainly the correct way of doing it because long tasks should not be queued on the interface updating thread. The exceptions didn't propagate to the UI because they were thrown from the BackgroundWorker thread rather than the UI interface thread(they are always different threads). All interface updates are (should be) done from one thread. We (in Java) call that the EDT, not sure what it's called in C#.
Thanks for posting the information.
Newbie
 
Join Date: Mar 2009
Posts: 1
#7: Mar 19 '09

re: PostgreSQL Database backup from a C# application


Can you send me your program for backup and restore postgres db in C#.
you can send to REMOVED EMAIL ADDRESS

Thanks.
Newbie
 
Join Date: Mar 2009
Posts: 1
#8: Jun 28 '09

re: PostgreSQL Database backup from a C# application


Hi all,

You can check my post @ CodeProject that enable you to create backup or restore DB and also it make some operations at PostGIS

http://www.codeproject.com/KB/databa...eSQLTasks.aspx

yours..,
Mohammed Thabet Zaky
GIS developer
Cairo,Egypt
http://thabettech.blogspot.com/
Reply