当前位置: 首页 > ai >正文

使用 Docker Compose 安装 PostgreSQL 16

前面是指南,后面是实际工作日志。

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


关键配置说明

  1. 数据持久化
    volumes 映射确保数据库数据保存在主机上(即使容器删除,数据也不会丢失)

  2. 环境变量

    • POSTGRES_USER: 超级用户账号

    • POSTGRES_PASSWORD: 数据库密码(建议使用强密码)

    • POSTGRES_DB: 容器启动时创建的默认数据库

  3. 端口映射
    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"

💡 生产环境必做

  1. 修改默认的 postgres 用户名

  2. 使用至少16位复杂密码

  3. 通过 .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]#

http://www.xdnf.cn/news/12108.html

相关文章:

  • css实现文字颜色渐变
  • 直线导轨微型化技术难点在哪里?
  • python项目中,。 __all__ = [‘StorageConfig‘] 这个__all__ 代表什么含义
  • uboot移植之GPIO上电初始状态的调整
  • HarmonyOS-ArkUI 自定义弹窗
  • 企业im,为企业设计的私有化即时通讯工具
  • [蓝桥杯]修改数组
  • 智慧充电:新能源汽车智慧充电桩的发展前景受哪些因素影响?
  • 塑料回收新突破!Nature 重磅:2 小时解聚碳纤维废料
  • 筑牢企业网管域安全防线,守护数字核心——联软网管域安全建设解决方案
  • SEO长尾关键词优化实战解析
  • AMFCNN-RKD:齿轮故障诊断的轻量级多传感器融合模型详解(python代码复现)
  • pbootcms文章加了浏览权限后出现404错误
  • 【芯片仿真中的X值:隐藏的陷阱与应对之道】
  • NoSQL——Redis配置与优化
  • python技巧:pyvisa打开hislip设备;IEEE 488.2
  • centos查看开启关闭防火墙状态
  • 电子行业AI赋能软件开发经典案例——某金融软件公司
  • 平安养老险蚌埠中心支公司开展金融宣教活动
  • C++定长内存块的实现
  • 【Redis】zset 类型
  • 在 CentOS 上将 Ansible 项目推送到 GitHub 的完整指南
  • 佰力博科技与您探讨压电d33测试有哪些注意事项
  • PySpark性能调优手册:大数据处理中的避坑与实践
  • HTML5+CSS3+JS小实例:具有粘性重力的磨砂玻璃导航栏
  • 航道无人机巡检系统
  • 【HarmonyOS 5】 社交行业详解以及 开发案例
  • vue3+ts+vite:详细、完整的 tsconfig.json 示例 / 常见配置项及其用途
  • React Navive初识
  • 强化学习原理入门-2马尔科夫决策过程