PostgreSQL Database backup from a C# application 
September 15th, 2008, 04:24 PM
| | Newbie | | Join Date: Sep 2008 Location: London, England
Posts: 24
| | |
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.
| 
September 30th, 2008, 02:22 PM
| | Administrator | | Join Date: Sep 2006
Posts: 12,084
| | | 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.
| 
October 1st, 2008, 09:58 AM
| | Newbie | | Join Date: Sep 2008 Location: London, England
Posts: 24
| | | 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.
| 
October 1st, 2008, 11:31 AM
| | Administrator | | Join Date: Sep 2006
Posts: 12,084
| | | 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.
| 
November 10th, 2008, 04:32 PM
| | Newbie | | Join Date: Sep 2008 Location: London, England
Posts: 24
| | | 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.
| 
November 11th, 2008, 06:48 AM
| | Administrator | | Join Date: Sep 2006
Posts: 12,084
| | | 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.
| 
March 19th, 2009, 07:26 AM
| | Newbie | | Join Date: Mar 2009
Posts: 1
| | | 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.
Last edited by eWish; March 22nd, 2009 at 05:40 PM.
Reason: Removed email address per site policy
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|