PgDump 1.0.3
dotnet add package PgDump --version 1.0.3
NuGet\Install-Package PgDump -Version 1.0.3
<PackageReference Include="PgDump" Version="1.0.3" />
<PackageVersion Include="PgDump" Version="1.0.3" />
<PackageReference Include="PgDump" />
paket add PgDump --version 1.0.3
#r "nuget: PgDump, 1.0.3"
#:package PgDump@1.0.3
#addin nuget:?package=PgDump&version=1.0.3
#tool nuget:?package=PgDump&version=1.0.3
PgDump
Do you want to easily use pg_dump
from C# but still have great control over how the output is handled?
Look no further!
This library lets you use one of the existing output providers or define your own to handle the output stream in your preferred way - file, memory, network, or anything else you want.
Simple to get started (see Quick Start). Powerful when needed.
What is pg_dump?
"pg_dump is a utility for backing up a PostgreSQL database."
— PostgreSQL Official Documentation
pg_dump
is the standard PostgreSQL command-line tool used to create backups of a database.
It exports the contents of a database to a file — either as plain SQL text or as a binary archive (tar, custom, or directory formats).
These files can later be used to restore the database exactly as it was.
This library makes it easy to use pg_dump
directly from your C# applications, without manually handling command-line arguments, processes, or stream output.
Core Components of this library
PgClient
— main class to perform database dumps or list databases.DumpAsync()
— dump a database to anyIOutputProvider
.ListDatabasesAsync()
— list all databases on the server. Returns a simpleList<string>
with the names.IOutputProvider
— interface for handling output (write to file, memory, your own type).
Installation
NuGet package available here: https://www.nuget.org/packages/PgDump/ (uses .NET 8)
Quick Start
Dump a database to a file (super simple)
ConnectionOptions options = new ConnectionOptions("localhost", 5432, "postgres", "your_password", "your_database");
PgClient client = new PgClient(options);
FileOutputProvider outputProvider = new FileOutputProvider("dump.tar");
await client.DumpAsync(outputProvider, timeout: TimeSpan.FromMinutes(1));
Dump a database into a memory stream
ConnectionOptions options = new ConnectionOptions("localhost", 5432, "postgres", "your_password", "your_database");
PgClient client = new PgClient(options);
using MemoryStream memoryStream = new MemoryStream();
StreamOutputProvider outputProvider = new StreamOutputProvider(memoryStream);
await client.DumpAsync(outputProvider, timeout: TimeSpan.FromMinutes(1));
// You now have the dump data in memoryStream
Create your own custom output provider
Implement IOutputProvider
:
public class MyCustomOutputProvider : IOutputProvider
{
public async Task WriteAsync(Stream inputStream, CancellationToken cancellationToken)
{
// Example: read and process the dump data however you want
using MemoryStream buffer = new MemoryStream();
await inputStream.CopyToAsync(buffer, cancellationToken);
// Do something with buffer...
}
}
Use it:
MyCustomOutputProvider outputProvider = new MyCustomOutputProvider();
await client.DumpAsync(outputProvider, timeout: TimeSpan.FromMinutes(1));
Listing databases
You can also list all databases on the server easily:
List<string> databases = await client.ListDatabasesAsync(TimeSpan.FromSeconds(30));
foreach (string database in databases)
{
Console.WriteLine(database);
}
Restoring database dumps
This package does not have support for restoring database dump files since that is a bit more complicated than just creating the dump file. It's also something that is not done as often and even less often done by an automated service.
To restore a database dump you can do it manually with pg_restore
The following command is an example of how to do that:
pg_restore --verbose --clean --no-acl --no-owner -h [host] -U [user] -d [database_name] [dump_file_with_file_extension]
The [host]
should be replaced by the host name, that's an ip address, url or similar. Can for example be localhost
.
The [user]
is the username to login with.
The [database_name]
is the database to restore the dump file to. This database must already exist. If you for example want to restore your dumpfile my-dump.sql
to create a database my-restored-db
you need to first manually create that database. Then you can use pg_restore and put my-restored-db
as the database.
The [dump_file_with_file_extension]
is the full relative path to the dump file, inlcuding the file extension. If you are standing in the same directory as the file with the terminal you're using, then that would be just the file name.
Getting into details:
The following sections explain the parameters and features of the library in greater detail.
The Parameters of DumpAsync
Method signature:
Task DumpAsync(IOutputProvider outputProvider, TimeSpan timeout, DumpFormat format = DumpFormat.Tar, CancellationToken cancellationToken = default)
Parameters:
outputProvider
The output provider that will receive thepg_dump
stream.
You can use built-in ones likeFileOutputProvider
andStreamOutputProvider
, or create your own.timeout
The maximum allowed time for the dump operation.
If the operation exceeds this time, it will automatically cancel and throw aTimeoutException
.
(Timeout is enforced even if the providedCancellationToken
does not cancel manually.)format
The desired output format for the dump.
One of:DumpFormat.Plain
(plain SQL text)DumpFormat.Tar
(tar archive)DumpFormat.Custom
(PostgreSQL custom binary format) (not tested in the unit tests!)DumpFormat.Directory
(directory with separate files) (not tested in the unit tests!)
Default is
DumpFormat.Tar
.cancellationToken
An optional externalCancellationToken
that you can pass if you want manual control over cancellation.
If canceled, the operation will throw anOperationCanceledException
.
(This is combined with the timeout internally.)
⚡ How timeout and cancellationToken work together
- Either timeout expiration or
cancellationToken
cancellation will immediately cancel the operation. - If timeout happens first → you get a
TimeoutException
. - If cancellation token cancels first → you get an
OperationCanceledException
. - Safe and reliable in both cases.
Detailed Features
- DumpAsync — runs
pg_dump
and writes output to any stream (file, memory, network, etc.). - ListDatabasesAsync — runs
psql
and lists all database names cleanly. - Flexible output handling — built-in file and memory output providers, and you can easily create your own.
- Proper cancellation and timeout — no stuck processes or infinite hangs.
- Safe environment handling — password is passed securely through environment variables.
- No assumptions — fully configurable connection options.
- Strong nullability — supports C# nullable reference types properly.
Testing
This project comes with full unit tests and real integration tests.
- Providers are tested (file and stream).
- Client behavior is tested (timeouts, errors, success).
- Real integration tests run
pg_dump
andpsql
against a real database.
Nuget
NuGet package available here:
https://www.nuget.org/packages/PgDump/
License
MIT License — do whatever you want with it, but no warranty.
Tags
PostgreSQL · pg_dump · psql · database · backup · dump · export · C# · async · command-line wrapper · streaming · output-provider · .net
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Update readme