前面是指南,后面是实际工作日志。
1. 创建 docker-compose.yml
文件
yaml
复制
下载
version: '3.9' services: postgres: image: postgres:16 container_name: postgres-16 environment: POSTGRES_USER: your_username # 替换为你的用户名 POSTGRES_PASSWORD: your_password # 替换为你的强密码 POSTGRES_DB: your_database # 替换为默认数据库名 volumes: - postgres_data:/var/lib/postgresql/data ports: - "5432:5432" restart: always volumes: 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" # 启用SSL POSTGRES_SSL: "on" # 禁用超级用户远程登录 POSTGRES_HOST_AUTH_METHOD: "scram-sha-256"
💡 生产环境必做:
修改默认的
postgres
用户名使用至少16位复杂密码
通过
.env
文件管理敏感信息(避免密码泄露)
连接示例(Python psycopg2)
python
复制
下载
import psycopg2 conn = 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]#