Skip to content

Instantly share code, notes, and snippets.

@gitfvb
Created March 2, 2026 10:36
Show Gist options
  • Select an option

  • Save gitfvb/9935de78d40207568144395f2e87474d to your computer and use it in GitHub Desktop.

Select an option

Save gitfvb/9935de78d40207568144395f2e87474d to your computer and use it in GitHub Desktop.
Use bulk npgsql to export data pretty fast

Make sure you have installed the powershell module ImportDependency and Pwsh

Create a lib subdirectory like in this path C:\faststats\scripts\npgbulk and put the unzipped nuget packages npgsql.10.0.1 and microsoft.extensions.logging.abstractions.10.0.3 in there.

This script exports like 4m rows just in a few seconds.

SL "C:\faststats\scripts\npgbulk"

Import-Dependency -LoadWholePackageFolder -verbose

$connStr = 'database=xxx;username=xxx;host=127.0.0.1;port=1111;password=xxx'
$conn = New-Object Npgsql.NpgsqlConnection($connStr)
$conn.Open()

$exportQuery = @"
COPY (
    SELECT *
    FROM schema.transactions
    ORDER BY customer_id
    --LIMIT 100
) TO STDOUT (FORMAT CSV, HEADER)
"@

$copyReader = $conn.BeginTextExport($exportQuery)  # FORMAT CSV in query
#$streamReader = [System.IO.StreamReader]::new($copyReader)
$fileStream = [System.IO.File]::OpenWrite("C:\faststats\scripts\npgbulk\big_table.csv")
$writer = [System.IO.StreamWriter]::new($fileStream)

$i = 0
$line = $copyReader.ReadLine()
while ($line -ne $null) {
    $writer.WriteLine($line)
    $line = $copyReader.ReadLine()
    $i += 1
    if ($i % 100000 -eq 0) {
        "Done $i rows"
    }
}

$writer.Flush()
$writer.Close()
$copyReader.Close()
$conn.Close()

"Done $( $i - 1) rows"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment