You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows系统下Docker环境中PostgreSQL镜像获取及创建方法咨询

在Windows Docker环境中部署PostgreSQL:获取镜像与自定义构建指南

Hey there! I see you're trying to set up PostgreSQL on Docker for Windows and having trouble finding a Windows-compatible image. Let's break this down into two clear paths: grabbing an official Windows image from Docker Hub, or building your own custom one if you need more control.


一、从Docker Hub获取官方Windows版PostgreSQL镜像

First off, official Windows-based PostgreSQL images do exist on Docker Hub—you might have missed them because they're tagged specifically for Windows container variants, and you need to make sure Docker is running in Windows Container Mode first.

Step 1: Switch Docker to Windows Container Mode

By default, Docker for Windows runs Linux containers. To access Windows images:

  • Right-click the Docker icon in your system tray.
  • Select Switch to Windows containers (wait for the switch to complete—it might take a minute).

Step 2: Find and Pull the Right Windows Image

Open PowerShell or Command Prompt, then:

  1. Search for official PostgreSQL Windows images:
    docker search postgres --filter "is-official=true"
    
    Look for tags with windowsservercore or nanoserver (these are Windows container base images). For example, postgres:16-windowsservercore-ltsc2022 or postgres:15-nanoserver-ltsc2022.
  2. Pull your preferred version (match the LTSC tag to your Windows version for best compatibility—ltsc2022 works for Windows 10 21H2+, Windows 11, and Windows Server 2022):
    docker pull postgres:16-windowsservercore-ltsc2022
    

Step 3: Run the PostgreSQL Container

Start a container with your pulled image (replace your_secure_password with a strong password):

docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=your_secure_password --name my-postgres-win postgres:16-windowsservercore-ltsc2022

Let's break down the flags:

  • -d: Runs the container in the background
  • -p 5432:5432: Maps your host's port 5432 to the container's port 5432
  • -e POSTGRES_PASSWORD: Sets the default PostgreSQL superuser password
  • --name: Gives your container a friendly name for easy management

二、自定义构建Windows版PostgreSQL镜像

If the official Windows images don't fit your needs (e.g., specific PostgreSQL version, custom configurations), you can build your own using a Dockerfile.

Example Dockerfile

Create a file named Dockerfile with the following content (adjust the PostgreSQL version and base image as needed):

# Use Windows Server Core as the base image (compatible with most Windows environments)
FROM mcr.microsoft.com/windows/servercore:ltsc2022

# Set environment variables for PostgreSQL version and default password
ENV POSTGRES_VERSION 16.3
ENV POSTGRES_PASSWORD=my_custom_password
ENV PATH=C:\Program Files\PostgreSQL\${POSTGRES_VERSION}\bin;%PATH%

# Download and install PostgreSQL silently
RUN powershell -Command \
    # Download the official Windows installer
    Invoke-WebRequest -Uri "https://get.enterprisedb.com/postgresql/postgresql-${POSTGRES_VERSION}-1-windows-x64.exe" -OutFile postgres-installer.exe; \
    # Run silent installation
    Start-Process -FilePath .\postgres-installer.exe -ArgumentList "--mode unattended --superpassword $env:POSTGRES_PASSWORD --install_dir ""C:\Program Files\PostgreSQL\$env:POSTGRES_VERSION""" -Wait; \
    # Clean up the installer file
    Remove-Item postgres-installer.exe -Force

# Initialize the PostgreSQL database
RUN initdb -D "C:\Program Files\PostgreSQL\${POSTGRES_VERSION}\data" -U postgres -W $env:POSTGRES_PASSWORD

# Expose the default PostgreSQL port
EXPOSE 5432

# Start the PostgreSQL service when the container launches
CMD ["pg_ctl", "-D", "C:\\Program Files\\PostgreSQL\\16.3\\data", "start", "-w"]

Build and Run Your Custom Image

  1. Navigate to the folder containing your Dockerfile in PowerShell/Command Prompt.
  2. Build the image:
    docker build -t my-custom-postgres-win .
    
  3. Run the container (adjust the password and name as needed):
    docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=your_secure_password --name my-custom-postgres my-custom-postgres-win
    

A Quick Note: Consider Linux Containers (If Possible)

If you don't have a hard requirement to use Windows containers (e.g., you're not pairing PostgreSQL with a Windows-only app), I'd recommend switching back to Linux container mode. The official Linux PostgreSQL images are smaller, faster, and more widely used. To use them:

  • Switch Docker back to Linux container mode via the system tray icon.
  • Pull the Linux image: docker pull postgres:16
  • Run it with the same docker run command as above (it'll work seamlessly on Windows via WSL2).

内容的提问来源于stack exchange,提问作者madee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:21:26