Skip to content

Instantly share code, notes, and snippets.

@peetw
Last active January 24, 2024 16:01
Show Gist options
  • Save peetw/10f49281423753d068f62bf47ba4642f to your computer and use it in GitHub Desktop.
Save peetw/10f49281423753d068f62bf47ba4642f to your computer and use it in GitHub Desktop.
Dockerfile for PostgreSQL 9.6 server on Windows
# This Dockerfile sets up a PostgreSQL 9.6 server
# Adapted from: https://github.com/StefanScherer/dockerfiles-windows/blob/master/postgres/Dockerfile
FROM microsoft/windowsservercore@sha256:c06b4bfaf634215ea194e6005450740f3a230b27c510cf8facab1e9c678f3a99
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN [Net.ServicePointManager]::SecurityProtocol = 'Tls12, Tls11, Tls' ; \
Invoke-WebRequest -UseBasicParsing -Uri 'https://get.enterprisedb.com/postgresql/postgresql-9.6.10-2-windows-x64.exe' -OutFile 'postgresql-installer.exe' ; \
Start-Process postgresql-installer.exe -ArgumentList '--mode unattended --superpassword password' -Wait ; \
Remove-Item postgresql-installer.exe -Force
RUN Invoke-WebRequest -UseBasicParsing -Uri 'https://dotnetbinaries.blob.core.windows.net/servicemonitor/2.0.1.3/ServiceMonitor.exe' -OutFile 'ServiceMonitor.exe'
SHELL ["cmd", "/S", "/C"]
RUN setx /M PATH "C:\\Program Files\\PostgreSQL\\9.6\\bin;%PATH%" && \
setx /M DATA_DIR "C:\\Program Files\\PostgreSQL\\9.6\\data" && \
setx /M PGPASSWORD "password"
RUN powershell -Command "Do { pg_isready -q } Until ($?)" && \
echo listen_addresses = '*' >> "%DATA_DIR%\\postgresql.conf" && \
echo host all all 0.0.0.0/0 trust >> "%DATA_DIR%\\pg_hba.conf" && \
echo host all all ::0/0 trust >> "%DATA_DIR%\\pg_hba.conf" && \
net stop postgresql-x64-9.6
EXPOSE 5432
CMD ["ServiceMonitor.exe", "postgresql-x64-9.6"]
@ukreddy-erwin
Copy link

THanks for the dockerfile... It really helped me.
I tried many files but none of them are working.

And coming to yours.
When I try to build, I am getting error as \ not recognised.So, I replaced them with individual RUN commands.

And one more thing, this database doesn't support external connections, for which we need to add a line in data\pg_hba.conf at the end with line:
host all all 0.0.0.0/0 md5
To support all connections. Can you please add that too.Or suggest how to do that.

@ukreddy-erwin
Copy link

I tried with docker file changing the postgres URL to latest version 12.x, I am getting this error.

pg_isready : The term 'pg_isready' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At line:1 char:6
+ Do { pg_isready -q } Until ($?)
+      ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (pg_isready:String) [], CommandN
   otFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

@Serg046
Copy link

Serg046 commented Sep 29, 2020

@ukreddy-erwin you forgot to change the folder names, it should be setx /M PATH "C:\\Program Files\\PostgreSQL\\12\\bin;%PATH%" etc

@ukreddy-erwin
Copy link

yeah I did that, but still same issue

@Serg046
Copy link

Serg046 commented Sep 30, 2020

@ukreddy-erwin

This works

FROM mcr.microsoft.com/windows/servercore:ltsc2019

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

RUN [Net.ServicePointManager]::SecurityProtocol = 'Tls12, Tls11, Tls' ; \
    Invoke-WebRequest -UseBasicParsing -Uri 'https://get.enterprisedb.com/postgresql/postgresql-12.4-1-windows-x64.exe' -OutFile 'postgresql-installer.exe' ; \
    Start-Process postgresql-installer.exe -ArgumentList '--mode unattended --superpassword password' -Wait ; \
    Remove-Item postgresql-installer.exe -Force

RUN Invoke-WebRequest -UseBasicParsing -Uri 'https://dotnetbinaries.blob.core.windows.net/servicemonitor/2.0.1.3/ServiceMonitor.exe' -OutFile 'ServiceMonitor.exe'

SHELL ["cmd", "/S", "/C"]

RUN setx /M PATH "C:\\Program Files\\PostgreSQL\\12\\bin;%PATH%" && \
    setx /M DATA_DIR "C:\\Program Files\\PostgreSQL\\12\\data" && \
    setx /M PGPASSWORD "password"
    
RUN powershell -Command "Do { pg_isready -q } Until ($?)" && \
    echo listen_addresses = '*' >> "%DATA_DIR%\\postgresql.conf" && \
    echo host  all  all  0.0.0.0/0  trust >> "%DATA_DIR%\\pg_hba.conf" && \
    echo host  all  all  ::0/0      trust >> "%DATA_DIR%\\pg_hba.conf" && \
    net stop postgresql-x64-12

EXPOSE 5432

CMD ["ServiceMonitor.exe", "postgresql-x64-12"]

@ashuvashistha
Copy link

ashuvashistha commented Jan 25, 2021

I am using above docker file but i am not able to attach volume to the container can anyone tell me how can I attach volume to it

I am using docker run -it -p 5478:5432 -v C:/database-data:C:/Program Files/PostgreSQL/12/data but it is not working

@Gator8
Copy link

Gator8 commented Dec 23, 2023

--mount type=bind,source="LOCAL_DIRECTORY\",target="PG_LOCATION_IN_CONTAINER\"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment