| 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.
|