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

使用 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.lqws.cn/news/148861.html

相关文章:

  • 网络安全面试题目(无答案)
  • Tailwind CSS 实战:基于 Kooboo 构建 AI 对话框页面(七):消息框交互功能添加
  • 招工招聘系统开发,重塑人才招聘新格局
  • Life:Internship finding
  • 【6.2-6.9学习周报】
  • SpringBoot EhCache 缓存
  • 国产linux系统(银河麒麟,统信uos)使用 PageOffice在线编辑word文件保存数据同时保存文件
  • NVM!(可以快速替换你的node版本)
  • 无人机军用与民用技术对比分析
  • 购物商城网站 Java+Vue.js+SpringBoot,包括商家管理、商品分类管理、商品管理、在线客服管理、购物订单模块
  • 神经网络与深度学习 网络优化与正则化
  • vue3+ts+vite:详细、完整的 tsconfig.json 示例 / 常见配置项及其用途
  • 在word中点击zotero Add/Edit Citation没有反应的解决办法
  • 【HTML】HTML 与 CSS 基础教程
  • 低功耗高安全:蓝牙模块在安防系统中的应用方案
  • python版若依框架开发:项目结构解析
  • 使用Node.js分片上传大文件到阿里云OSS
  • 【深度学习】为什么2个3×3的卷积可以相当于一个5×5的卷积核?为什么3个3×3的卷积相当于一个7×7的卷积核,到底区别在哪里?我们该如何使用?
  • 从零搭建到 App Store 上架:跨平台开发者使用 Appuploader与其他工具的实战经验
  • 金融系统功能测试:科技赋能,安全护航
  • 总结HTML中的文本标签
  • 深入浅出玩转物联网时间同步:基于BC260Y的NTP实验与嵌入式仿真教学革命
  • HTML中各种标签的作用
  • Apache Doris + MCP:Agent 时代的实时数据分析底座
  • OrCAD X Capture CIS设计小诀窍系列第二季--03.如何在Capture中输出带有目录和元器件信息的PDF
  • javaweb -html -CSS
  • 专业级PDF转CAD解决方案
  • php apache构建 Web 服务器
  • 使用Python提取PDF元数据的完整指南
  • 【bug】Error: /undefinedfilename in (/tmp/ocrmypdf.io.9xfn1e3b/origin.pdf)