#Requires -Version 5.1 <# .SYNOPSIS RasterizedDB: copy a live PostgreSQL database (schema, rows, then keys and constraints) into a new RasterizedDB database, then exit. Run it while no RasterizedDB server is using the data directory. .DESCRIPTION The source password is read from $env:PGPASSWORD, never from the command line. .EXAMPLE $env:PGPASSWORD = '…' .\setup.ps1 -Into shop -PgHost pg.internal -PgUser app -PgDatabase shop #> [CmdletBinding()] param( [string]$RdbBin = $(if ($env:RDB_BIN) { $env:RDB_BIN } else { 'rasterizeddb.exe' }), # The RasterizedDB data directory. [string]$Data = $(if ($env:RDB_DATA) { $env:RDB_DATA } else { '.\data' }), # Name of the new database. [string]$Into = $env:RDB_IMPORT_INTO, [string]$PgHost = $(if ($env:PGHOST) { $env:PGHOST } else { 'localhost' }), [int]$PgPort = $(if ($env:PGPORT) { [int]$env:PGPORT } else { 5432 }), [string]$PgUser = $(if ($env:PGUSER) { $env:PGUSER } else { 'postgres' }), [string]$PgDatabase = $env:PGDATABASE, [ValidateSet('disable', 'prefer', 'require', 'verify-full')] [string]$SslMode = $(if ($env:RDB_IMPORT_SSLMODE) { $env:RDB_IMPORT_SSLMODE } else { 'prefer' }), [string]$Schema = $(if ($env:RDB_IMPORT_SCHEMA) { $env:RDB_IMPORT_SCHEMA } else { 'public' }), # Tables to import (default: all) and to leave out. [string[]]$Only = @(), [string[]]$Exclude = @(), [ValidateSet('auto', 'text', 'native')] [string]$Json = $(if ($env:RDB_IMPORT_JSON) { $env:RDB_IMPORT_JSON } else { 'auto' }), [ValidateSet('fail', 'text')] [string]$WideNumeric = $(if ($env:RDB_IMPORT_WIDE_NUMERIC) { $env:RDB_IMPORT_WIDE_NUMERIC } else { 'fail' }), [int]$BatchRows = $(if ($env:RDB_IMPORT_BATCH_ROWS) { [int]$env:RDB_IMPORT_BATCH_ROWS } else { 5000 }), # Skip primary keys, unique constraints and foreign keys. [switch]$WithoutConstraints, # Report what would come across; create nothing. [switch]$DryRun ) $ErrorActionPreference = 'Stop' $cmd = Get-Command $RdbBin -ErrorAction SilentlyContinue if (-not $cmd) { throw "'$RdbBin' not found; pass -RdbBin with the path of rasterizeddb.exe." } if (-not $Into) { throw 'Pass -Into with the name of the new database.' } if (-not $PgDatabase) { throw 'Pass -PgDatabase (or set $env:PGDATABASE) with the source database.' } if (-not $env:PGPASSWORD) { Write-Warning 'PGPASSWORD is not set; only a source that needs no password will work.' } # The new database starts with the roles of the directory's default database, # so the directory must have been started once (with its first role) already. if (-not (Test-Path -LiteralPath $Data) -or -not (Get-ChildItem -LiteralPath $Data -Force | Select-Object -First 1)) { throw "$Data is empty: start a server on it once first, so the role you will log in with exists." } $Data = (Resolve-Path -LiteralPath $Data).Path $serverArgs = @('--location', $Data, '--pg-import-into', $Into, '--pg-import-host', $PgHost, '--pg-import-port', "$PgPort", '--pg-import-user', $PgUser, '--pg-import-database', $PgDatabase, '--pg-import-sslmode', $SslMode, '--pg-import-schema', $Schema, '--pg-import-json', $Json, '--pg-import-wide-numeric', $WideNumeric, '--pg-import-batch-rows', "$BatchRows") if ($Only) { $serverArgs += @('--pg-import-only-tables', ($Only -join ',')) } if ($Exclude) { $serverArgs += @('--pg-import-exclude-tables', ($Exclude -join ',')) } if ($WithoutConstraints) { $serverArgs += '--pg-import-without-constraints' } if ($DryRun) { $serverArgs += '--pg-import-dry-run' } Write-Host "Importing $PgDatabase from ${PgHost}:$PgPort (schema $Schema) into $Into at $Data" & $cmd.Source @serverArgs if ($LASTEXITCODE -ne 0) { throw "The import failed (exit $LASTEXITCODE); the message is above. If a server is running on $Data, stop it first." } if ($DryRun) { Write-Host 'Dry run: nothing was created.' } else { Write-Host '' Write-Host "Done. Start the server on $Data as usual, then connect to the new database:" Write-Host " psql `"postgresql://rasterized@127.0.0.1:5432/${Into}?sslmode=require`"" }