基于Debian构建的生产就绪型PostgreSQL 17.2 Docker镜像,专注于高可用性部署。集成wal-g和pg_auto_failover工具,提供WAL归档、备份及自动故障转移能力,简化流复制配置,适用于对数据库可靠性有高要求的生产环境。
postgres-contrib扩展包,默认启用pg_stat_statements性能统计扩展pg_upgrade --link命令使用冲突bash# 使用密码保护,默认情况下镜像已配置安全设置 docker run -d --name postgres -p 5432:5432 -v postgres:/var/lib/postgresql -e POSTGRES_PASSWORD=somepassword livingdocs/postgres:17.2
bash# 假设已创建旧版本容器(示例为14.5) docker run -d --name postgres -p 5432:5432 -v postgres:/var/lib/postgresql livingdocs/postgres:14.5 # 首先停止旧容器,然后运行升级镜像 docker stop postgres docker run --rm -v postgres:/var/lib/postgresql livingdocs/postgres:17.2-upgrade # 升级成功后,使用新镜像启动并挂载现有卷 docker run -d --name postgres -p 5432:5432 -v postgres:/var/lib/postgresql livingdocs/postgres:17.2
常规构建
bashdocker build -t livingdocs/postgres:17.2 .
使用buildx构建多架构镜像
bash# 构建并推送多架构镜像(amd64/arm64)至Docker Hub docker buildx build --platform linux/amd64,linux/arm64 -t livingdocs/postgres:17.2 --push . # 构建升级版本镜像 docker buildx build --platform linux/amd64,linux/arm64 -t livingdocs/postgres:17.2-upgrade --push -f Dockerfile.upgrade .
使用nerdctl在lima/containerd环境构建
bash# 构建多架构镜像 nerdctl build --platform=amd64,arm64 -t livingdocs/postgres:17.2 . nerdctl build --platform=amd64,arm64 -t livingdocs/postgres:17.2-upgrade -f Dockerfile.upgrade . # 推送所有架构镜像 lima nerdctl push --all-platforms livingdocs/postgres:17.2 lima nerdctl push --all-platforms livingdocs/postgres:17.2-upgrade
简单配置
bash# 创建主从容器 docker run -d -p 5433:5432 --name postgres-1 livingdocs/postgres:17.2 docker run -d -p 5434:5432 --name postgres-2 livingdocs/postgres:17.2 standby -d "host=host.docker.internal port=5433 user=postgres target_session_attrs=read-write" # 测试复制功能 docker exec postgres-1 psql -c "CREATE TABLE hello (value text); INSERT INTO hello(value) VALUES('world');" docker exec postgres-2 psql -c "SELECT * FROM hello;" # 输出: # value # ------- # world # (1 row)
高级配置(使用密码认证)
bash# 创建Docker网络模拟生产环境DNS解析 docker network create local # 启动主库容器 docker run -d -p 5433:5432 --name postgres-1 --network=local --network-alias=postgres -e POSTGRES_HOST_AUTH_METHOD=md5 livingdocs/postgres:17.2 # 初始化数据库用户(也可在集群启动时挂载SQL脚本至/var/lib/postgresql/initdb.d自动执行) docker exec postgres-1 psql -c "ALTER ROLE postgres ENCRYPTED PASSWORD 'some-postgres-password';" docker exec postgres-1 psql -c "CREATE USER replication REPLICATION LOGIN ENCRYPTED PASSWORD 'some-replication-password';" # 启动副本容器 export DB_URL="host=postgres port=5432 user=replication password=some-replication-password target_session_attrs=read-write" docker run -d -p 5434:5432 --name postgres-2 --network=local --network-alias=postgres livingdocs/postgres:17.2 standby -d $DB_URL docker run -d -p 5435:5432 --name postgres-3 --network=local --network-alias=postgres livingdocs/postgres:17.2 standby -d $DB_URL # 测试复制功能 docker exec postgres-1 psql -c "CREATE TABLE hello (value text); INSERT INTO hello(value) VALUES('hello');" docker exec postgres-2 psql -c "SELECT * FROM hello;" docker exec postgres-3 psql -c "SELECT * FROM hello;" # 两个副本输出均为: # value # ------- # hello # (1 row) # # 测试手动提升副本为主库 # docker rm -f postgres-1 # 此时向副本插入数据会失败:ERROR: cannot execute INSERT in a read-only transaction docker exec postgres-2 psql -c "INSERT INTO hello(value) VALUES('world');" # 提升副本 docker exec postgres-2 touch /var/lib/postgresql/data/promote.signal # 验证提升结果 docker exec postgres-2 psql -c "INSERT INTO hello(value) VALUES('world');" docker exec postgres-3 psql -c "SELECT * FROM hello;" # 输出均为: # value # ------- # hello # world # (2 rows)
注意:提升前需确保旧主库已停止接受写入,可通过停止容器或执行以下SQL命令实现:
sqlALTER SYSTEM SET default_transaction_read_only TO 'on'; SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid();
提升副本有两种方式:
在副本容器的数据目录创建promote.signal文件:
bashtouch /var/lib/postgresql/data/promote.signal
若修改过配置,需确保
promote_trigger_file参数指向该路径
在容器内执行pg_ctl promote命令:
bashgosu postgres pg_ctl promote
| 环境变量 | 说明 | 默认值 |
|---|---|---|
POSTGRES_PASSWORD | postgres超级用户密码 | 无(必填) |
POSTGRES_HOST_AUTH_METHOD | 主机认证方式(如md5、trust等) | 未指定 |


探索更多轩辕镜像的使用方法,找到最适合您系统的配置方式
通过 Docker 登录认证访问私有仓库
无需登录使用专属域名
Kubernetes 集群配置 Containerd
K3s 轻量级 Kubernetes 镜像加速
VS Code Dev Containers 配置
Podman 容器引擎配置
HPC 科学计算容器配置
ghcr、Quay、nvcr 等镜像仓库
Harbor Proxy Repository 对接专属域名
Portainer Registries 加速拉取
Nexus3 Docker Proxy 内网缓存
需要其他帮助?请查看我们的 常见问题Docker 镜像访问常见问题解答 或 提交工单
manifest unknown
no matching manifest(架构)
invalid tar header(解压)
TLS 证书失败
DNS 超时
410 Gone 排查
402 与流量用尽
401 认证失败
429 限流
D-Bus 凭证提示
413 与超大单层
来自真实用户的反馈,见证轩辕镜像的优质服务