AdSense

網頁

2025/6/10

Windows 11 Docker 安裝PostgreSQL資料庫

在Windows 11的Docker安裝PostgreSQL資料庫。


環境

  • Windows 11 Pro(Windows 11專業版)
  • Docker version 28.1.1

事前要求

參考「Windows 11 Pro 安裝Docker Desktop」安裝好Docker。


建立Docker Volume

在命令列執行docker volume create postgres-data建立volume postgres-data,用來存放在docker容器中運行的 postgresql資料庫的資料。

PS C:\> docker volume create postgresql-data
postgresql-data

安裝PostgreSQL

執行docker pull postgres:17下載postgres版本17的image。

PS C:\> docker pull postgres:17
17: Pulling from library/postgres
795a79fbbd42: Pull complete
d96a42584a95: Pull complete
950a4839a310: Pull complete
0386831197cb: Pull complete
8c8ec5a41a68: Pull complete
d9b0e915fe52: Pull complete
1cdee1a5933b: Pull complete
6a09dcaea704: Pull complete
cfba392596b7: Pull complete
4dca6941a9dd: Pull complete
61320b01ae5e: Pull complete
d662116474cc: Pull complete
8010caf973f8: Pull complete
856cb9d740bc: Pull complete
Digest: sha256:30a72339ce74f2621f0f82cd983a11ade307ec2e634a7998318e8813a6f6f25c
Status: Downloaded newer image for postgres:17
docker.io/library/postgres:17

執行docker run --name postgres -d -it -p 5432:5432 --restart always -v postgresql-data:/var/lib/postgresql/data -e POSTGRES_PASSWORD=12345 postgres:17來運行postgres container。

PS C:\> docker run --name postgres -d -it -p 5432:5432 --restart always -v postgresql-data:/var/lib/postgresql/data -e POSTGRES_PASSWORD=12345 postgres:17
a105b9b4dc4ae19428cc858d8866dd5989050c1e91d2d45807d2b5d117354368

執行docker ps查看運行中的postgres容器。

PS C:\>  docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS                    NAMES
a105b9b4dc4a   postgres:17   "docker-entrypoint.s…"   15 seconds ago   Up 14 seconds   0.0.0.0:5432->5432/tcp   postgres




連線

開啟資料庫管理工具,例如DBeaver,建立新連線。

  • [Host]維持預設為localhost
  • [Database]維持預設為postgres
  • [Username]維持預設為postgres
  • [Password]填入剛運行postgres容器時的POSTGRES_PASSWORD的值,即12345。

設定完後按Finish完成並連線到docker的postgres資料庫。




沒有留言:

AdSense