Reputation: 115
I want to backup/restore postgres DB from my Win app. I have a connection to postgres DB in a local or remote machine in LAN (it is setup in ini file). And I don't know in advance path to pg_dump.exe/pg_restore.exe utils, I may not have them in local machine. How can I backup/restore postgres databases? It seems the only way is to call pg_dump with parameters, but I don't know the path to it.
Upvotes: 0
Views: 1157
Reputation: 362
You can use dotConnect for PostgreSQL to dump your database.
example --
using (var connection = new PgSqlConnection("your connection string"))
{
connection.Open();
using (var backup = new PgSqlBackup(connection))
{
backup.Format = PgSqlBackupFormat.Custom;
backup.CustomOptions = "-Fc -U yourusername -W";
backup.FileName = "yourdbname.backup";
backup.Execute();
}
}
We're creating a new connection to the PostgreSQL database, then creating a new PgSqlBackup object and passing the connection object to it. Then set the backup format to custom, and select the custom options to include the username, password, and format of the backup file. We're also setting the filename and calling the execute method to perform the backup.
Upvotes: 2