前面是指南,后面是實際工作日志。
1. 創建?docker-compose.yml
?文件
yaml
復制
下載
version: '3.9' services:postgres:image: postgres:16container_name: postgres-16environment:POSTGRES_USER: your_username # 替換為你的用戶名POSTGRES_PASSWORD: your_password # 替換為你的強密碼POSTGRES_DB: your_database # 替換為默認數據庫名volumes:- postgres_data:/var/lib/postgresql/dataports:- "5432:5432"restart: alwaysvolumes:postgres_data:
2. 啟動容器
bash
復制
下載
docker-compose up -d
3. 驗證安裝
bash
復制
下載
docker ps -a | grep postgres-16
輸出應顯示容器狀態為?Up
關鍵配置說明
-
數據持久化
volumes
?映射確保數據庫數據保存在主機上(即使容器刪除,數據也不會丟失) -
環境變量
-
POSTGRES_USER
: 超級用戶賬號 -
POSTGRES_PASSWORD
: 數據庫密碼(建議使用強密碼) -
POSTGRES_DB
: 容器啟動時創建的默認數據庫
-
-
端口映射
5432:5432
?將容器端口映射到主機端口
常用管理命令
命令 | 說明 |
---|---|
docker-compose stop | 停止容器 |
docker-compose start | 啟動容器 |
docker-compose down -v | 刪除容器和數據卷 |
docker exec -it postgres-16 psql -U your_username | 進入PSQL命令行 |
docker logs postgres-16 | 查看數據庫日志 |
安全增強建議(可選)
yaml
復制
下載
environment:# 禁用默認public模式寫權限POSTGRES_INITDB_ARGS: "--auth-host=scram-sha-256 --no-instance"# 啟用SSLPOSTGRES_SSL: "on"# 禁用超級用戶遠程登錄POSTGRES_HOST_AUTH_METHOD: "scram-sha-256"
💡?生產環境必做:
修改默認的?
postgres
?用戶名使用至少16位復雜密碼
通過?
.env
?文件管理敏感信息(避免密碼泄露)
連接示例(Python psycopg2)
python
復制
下載
import psycopg2conn = psycopg2.connect(host="localhost",port=5432,user="your_username",password="your_password",database="your_database" )
通過以上步驟,您已完成 PostgreSQL 16 的容器化部署!
[root@centos08 work]# cd postgresql/
[root@centos08 postgresql]# ls
[root@centos08 postgresql]# vi docker-compose.yml
[root@centos08 postgresql]# docker-compose up -d
WARN[0000] /work/postgresql/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Running 15/15
? postgres Pulled 72.0s
? 61320b01ae5e Pull complete 22.6s
? 2dced227b4f3 Pull complete 23.0s
? f91d7f75e2ed Pull complete 23.8s
? d52fe2b6a589 Pull complete 24.3s
? c66d2dcd9ac2 Pull complete 50.7s
? 5dedf13f853f Pull complete 51.2s
? a1b7b6ac9604 Pull complete 51.4s
? 54e868887d72 Pull complete 51.8s
? 54fbe2476d31 Pull complete 68.3s
? 0d367dbc515a Pull complete 68.6s
? fcdc97c8b0f7 Pull complete 68.9s
? 35ea952ba4d5 Pull complete 69.2s
? 6802029dd7cb Pull complete 69.6s
? 89ac21e5afbc Pull complete 69.9s
[+] Running 3/3
? Network postgresql_default Created 0.3s
? Volume "postgresql_postgres_data" Created 0.1s
? Container postgres-16 Started 1.3s
[root@centos08 postgresql]# docker ps -a | grep postgres-16
52f5cd24ba5c postgres:16 "docker-entrypoint.s…" 14 seconds ago Up 12 seconds 0.0.0.0:5432->5432/tcp postgres-16
[root@centos08 postgresql]#
[root@centos08 postgresql]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
postgres 16 616e340baeac 2 weeks ago 436MB
bitnami/kafka 3.5.2 21bd84c8365d 5 months ago 638MB
redis 7.2.4 9b38108e295d 14 months ago 116MB
zilliz/attu v2.3.3 5c2a0dd36e38 18 months ago 282MB
milvusdb/milvus v2.3.3 7e482a814849 19 months ago 870MB
minio/minio RELEASE.2023-03-20T20-16-18Z 400c20c8aac0 2 years ago 252MB
quay.io/coreos/etcd v3.5.5 673f29d03de9 2 years ago 182MB
[root@centos08 postgresql]# docker save postgres:16 -o postgres-16.tar
[root@centos08 postgresql]# ls -lh
total 424M
-rw-r--r-- 1 root root 429 Jun 5 09:59 docker-compose.yml
-rw------- 1 root root 424M Jun 5 10:03 postgres-16.tar
[root@centos08 postgresql]#