diff --git a/images/windows/scripts/build/Install-PostgreSQL.ps1 b/images/windows/scripts/build/Install-PostgreSQL.ps1 index aff382ed7..bd1737282 100644 --- a/images/windows/scripts/build/Install-PostgreSQL.ps1 +++ b/images/windows/scripts/build/Install-PostgreSQL.ps1 @@ -5,7 +5,10 @@ # Define user and password for PostgreSQL database $pgUser = "postgres" -$pgPwd = "root" +$pgPwd = "root" + +# Save current value of ErrorActionPreference and set it to Stop +$errorActionOldValue = $ErrorActionPreference # Prepare environment variable for validation [Environment]::SetEnvironmentVariable("PGUSER", $pgUser, "Machine") @@ -35,7 +38,6 @@ if ($null -ne ($toolsetVersion | Select-String -Pattern '\d+\.\d+\.\d+')) { $targetMinorVersions = ($minorVersions | Sort-Object)[-1] # In order to get rid of error messages (we know we will have them), force ErrorAction to SilentlyContinue - $errorActionOldValue = $ErrorActionPreference $ErrorActionPreference = 'SilentlyContinue' # Install latest PostgreSQL @@ -58,10 +60,27 @@ if ($null -ne ($toolsetVersion | Select-String -Pattern '\d+\.\d+\.\d+')) { } while (!$response) } - # Return the previous value of ErrorAction and invoke Install-Binary function $ErrorActionPreference = $errorActionOldValue -$installerArgs = @("--install_runtimes 0", "--superpassword root", "--enable_acledit 1", "--unattendedmodeui none", "--mode unattended") + +# Define new data directory for PostgreSQL and create it +if ($installerUrl -match 'postgresql-(\d+)') { + $pgMajorVersion = $matches[1] +} +$pgData = "C:\PostgreSQL\$pgMajorVersion\data" +if (-Not (Test-Path -Path $pgData)) { + New-Item -ItemType Directory -Path $pgData | Out-Null +} + +# Define silent install arguments for PostgreSQL +$installerArgs = @( + "--install_runtimes 0", + "--superpassword root", + "--enable_acledit 1", + "--unattendedmodeui none", + "--mode unattended", + "--datadir `"$pgData`"" +) Install-Binary ` -Url $installerUrl ` @@ -74,8 +93,7 @@ $pgPath = (Get-CimInstance Win32_Service -Filter "Name LIKE 'postgresql-%'").Pat # Parse output of command above to obtain pure path $pgBin = Split-Path -Path $pgPath.split('"')[1] -$pgRoot = Split-Path -Path $pgPath.split('"')[5] -$pgData = Join-Path $pgRoot "data" +$pgRoot = Split-Path $pgBin -Parent # Validate PostgreSQL installation $pgReadyPath = Join-Path $pgBin "pg_isready.exe"