diff --git a/images/linux/scripts/installers/postgresql.sh b/images/linux/scripts/installers/postgresql.sh
index 9f2988a84..7c1c89cc0 100644
--- a/images/linux/scripts/installers/postgresql.sh
+++ b/images/linux/scripts/installers/postgresql.sh
@@ -7,10 +7,26 @@
# Source the helpers for use with the script
source $HELPER_SCRIPTS/document.sh
+#Preparing repo for PostgreSQL 12.
+wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
+echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list
+
+echo "Install PostgreSQL"
+apt update
+apt install postgresql postgresql-client
+
echo "Install libpq-dev"
apt-get install libpq-dev
-echo "Install Postgresql Client"
-apt-get install postgresql-client
+#Verify that PostgreSQL is ready for accept incoming connections.
+# exit codes:
+# ready - 0
+# reject - 1
+# connection timeout - 2
+pg_isready
-DocumentInstalledItem "$(psql -V 2>&1 | cut -d ' ' -f 1,2,3)"
\ No newline at end of file
+DocumentInstalledItem "$(psql -V 2>&1 | cut -d ' ' -f 1,2,3)"
+
+# Disable postgresql.service
+systemctl is-active --quiet postgresql.service && systemctl stop postgresql.service
+systemctl disable postgresql.service
\ No newline at end of file
diff --git a/images/win/Windows2016-Azure.json b/images/win/Windows2016-Azure.json
index 02976c08a..be10ae372 100644
--- a/images/win/Windows2016-Azure.json
+++ b/images/win/Windows2016-Azure.json
@@ -312,6 +312,12 @@
"scripts":[
"{{ template_dir }}/scripts/Installers/Install-7zip.ps1"
]
+ },
+ {
+ "type": "powershell",
+ "scripts":[
+ "{{ template_dir }}/scripts/Installers/Install-PostgreSQL.ps1"
+ ]
},
{
"type": "powershell",
@@ -519,6 +525,12 @@
"{{ template_dir }}/scripts/Installers/Install-MysqlCli.ps1"
]
},
+ {
+ "type": "powershell",
+ "scripts":[
+ "{{ template_dir }}/scripts/Installers/Validate-PostgreSQL.ps1"
+ ]
+ },
{
"type": "powershell",
"scripts":[
diff --git a/images/win/Windows2019-Azure.json b/images/win/Windows2019-Azure.json
index c65982e4e..d1c5ca019 100644
--- a/images/win/Windows2019-Azure.json
+++ b/images/win/Windows2019-Azure.json
@@ -621,6 +621,12 @@
"{{ template_dir }}/scripts/Installers/Install-Vcpkg.ps1"
]
},
+ {
+ "type": "powershell",
+ "scripts":[
+ "{{ template_dir }}/scripts/Installers/Install-PostgreSQL.ps1"
+ ]
+ },
{
"type": "powershell",
"scripts":[
@@ -944,6 +950,12 @@
"{{ template_dir }}/scripts/Installers/Validate-Vcpkg.ps1"
]
},
+ {
+ "type": "powershell",
+ "scripts":[
+ "{{ template_dir }}/scripts/Installers/Validate-PostgreSQL.ps1"
+ ]
+ },
{
"type": "powershell",
"scripts":[
diff --git a/images/win/scripts/Installers/Install-PostgreSQL.ps1 b/images/win/scripts/Installers/Install-PostgreSQL.ps1
new file mode 100644
index 000000000..2e6bc373d
--- /dev/null
+++ b/images/win/scripts/Installers/Install-PostgreSQL.ps1
@@ -0,0 +1,22 @@
+$ErrorActionPreference = "Stop"
+
+Import-Module -Name ImageHelpers
+
+#Define user and password for PostgreSQL database
+$postgresusr="postgres"
+$postgrespwd="root"
+
+#Prepare environment variable for validation
+Set-SystemVariable -SystemVariable PGUSER -Value $postgresusr
+Set-SystemVariable -SystemVariable PGPASSWORD -Value $postgrespwd
+#Install latest PostgreSQL
+
+cinst postgresql --params "/Password:$postgrespwd" --params-global --debug --verbose
+
+#Get Path to pg_ctl.exe
+$paths = (Get-CimInstance Win32_Service -Filter "Name LIKE 'postgresql-%'").PathName
+#Parse output of command above to obtain pure path
+$pgbin = $paths.split('"')[1].replace("\pg_ctl.exe", "")
+#Added PostgreSQL bin path into PATH variable.
+Add-MachinePathItem $pgbin
+
diff --git a/images/win/scripts/Installers/Validate-PostgreSQL.ps1 b/images/win/scripts/Installers/Validate-PostgreSQL.ps1
new file mode 100644
index 000000000..bfd2f0f05
--- /dev/null
+++ b/images/win/scripts/Installers/Validate-PostgreSQL.ps1
@@ -0,0 +1,35 @@
+$PGUSER="postgres"
+function Validate-PostgreSQL {
+ $pgready = Start-Process -FilePath pg_isready -Wait -PassThru
+ $exitCode = $pgready.ExitCode
+ if ($exitCode -eq 0)
+ {
+ Write-Host -Object "PostgreSQL has been successfully installed."
+ }
+ else
+ {
+ Write-Host -Object "PostgreSQL is not ready. Exitcode: $exitCode"
+ exit $exitCode
+ }
+}
+
+$paths = (Get-CimInstance Win32_Service -Filter "Name LIKE 'postgresql-%'").PathName
+$pgservice = (Get-CimInstance Win32_Service -Filter "Name LIKE 'postgresql-%'").Name
+$pgroot = $paths.split('"')[1].replace("\bin\pg_ctl.exe", "")
+$psqlVersion = pg_config --version | Out-String
+Validate-PostgreSQL
+
+# Adding description of the software to Markdown
+$SoftwareName = "PostgreSQL"
+$Description = @"
+_Version:_ $psqlVersion
+_Default Path:_ $pgroot
+_User:_ $env:PGUSER
+_Password:_ $env:PGPASSWORD
+"@
+
+Add-SoftwareDetailsToMarkdown -SoftwareName $SoftwareName -DescriptionMarkdown $Description
+
+#Stop and disable PostgreSQL service
+Stop-Service -Name $pgservice
+Set-Service $pgservice -StartupType Disabled