By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,649 Members | 696 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,649 IT Pros & Developers. It's quick & easy.

PostgreSQL Database backup from a C# application

P: 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.
Sep 15 '08 #1
Share this Question
Share on Google+
7 Replies

P: 13,264
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.
Sep 30 '08 #2

P: 24
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.
Oct 1 '08 #3

P: 13,264
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.
Oct 1 '08 #4

P: 24
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.
Nov 10 '08 #5

P: 13,264
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.
Nov 11 '08 #6

P: 1
Can you send me your program for backup and restore postgres db in C#.

Mar 19 '09 #7

P: 1
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

Mohammed Thabet Zaky
GIS developer
Jun 28 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.