banco-dados-pralog/export_csv.ps1

49 lines
1.4 KiB
PowerShell

# Database CSV Export Script using Docker
# This script exports all tables from the 'global' database to individual CSV files
$password = "fzYSD5g5NLcRvjmd3nJv"
$dbHost = "pikachu.cnsiese46wxf.sa-east-1.rds.amazonaws.com"
$user = "postgres"
$db = "global"
$tables = @(
"request_log",
"global_driver_registration_request",
"_prisma_migrations",
"Tenant",
"TenantDomain",
"File",
"GlobalDriver",
"GlobalDriverTenant",
"global_driver_bank_account",
"global_integration",
"_typeorm_migrations"
)
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
$exportDir = "export_csv_${timestamp}"
if (-Not (Test-Path $exportDir)) {
New-Item -ItemType Directory -Path $exportDir
}
foreach ($table in $tables) {
$outputFile = "${exportDir}/${table}.csv"
Write-Host "Exporting table: $table to $outputFile..."
# We use single quotes for the -c argument to avoid PowerShell escaping issues with double quotes inside SQL
$copyCmd = "COPY ""$table"" TO STDOUT WITH CSV HEADER"
docker run --rm `
-e PGPASSWORD=$password `
postgres:16-alpine `
psql -h $dbHost -U $user -d $db -c "COPY ""$table"" TO STDOUT WITH CSV HEADER" > $outputFile
if ($LASTEXITCODE -eq 0) {
Write-Host "Export of $table completed successfully."
} else {
Write-Error "Export of $table failed!"
}
}
Write-Host "CSV Export completed. Files are in: $exportDir"