Postgres could not locate a valid checkpoint record 恢复方法

这篇博客记录了在Docker环境下PostgreSQL因两个容器同时写入数据导致崩溃的恢复过程。通过停止容器,使用pg_resetwal或pg_resetxlog进行修复,然后重启容器,成功解决了问题。提醒在执行修复命令前务必做好数据备份。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录一篇Docker 中的 PostgreSQL 崩溃恢复记录
在 Docker 中运行的 PostgreSQL 数据库突然报 database shutdown, 错误日志类似这样:

invalid resource manager ID in primary checkpoint record
could not locate a valid checkpoint record 
startup process PID 25 was terminated by Signal 6 : Aborted
aborting startup due to startup process failure

我的PG之所以报这样的错是因为,两个PG容器同时映射同一块磁盘目录,业务服务切换pg库时操作不当导致两个容器都在写入数据造成 pg 崩溃,并自动下线

我是用docker部署的Postgres

  • 如果使用的 PostgreSQL 是 10.x 或更高的的版本, 使用 pg_resetwal DATADIR 来解决;
  • 否则使用 pg_resetxlog DATADIR 来解决;

恢复操作:

  • 1.停止报错的PG容器,docker stop postgis-zy
  • 2.覆盖 entrypoint 来的形式启动一个临时的容器,这一步很重要, 需要使用原来的镜像, 覆盖默认的 entrypoint 启动, 进入可交互的命令行窗口
docker run -it --rm --entrypoint /bin/bash \
  -v ${pwd}/data:/var/lib/postgresql/data \
  postgres:12.5
  • 3.在命令行窗口中,根据数据库的版本选择 pg_resetwal 或者 pg_resetxlog 进行修复
    请在恢复前做好备份
#请在执行此命令前做好备份
pg_resetwal -f /var/lib/postgresql/data

如果不加 -f ,会返回这样的提示

The database server was not shut down cleanly.
Resetting the transaction log might cause data to be lost. If you want to proceed anyway, use -f to force reset.

  • 4.退出容器,再启动容器,docker restart postgis-zy

  • 5.如果出现 pg_resetwal : error : Cannot be executed by root
    pg_resetwal: You must run pg_resetwal as the PostgreSQL superuser
    请用 gosu postgres pg_resetwal -f /var/lib/postgresql/data

参考了网友的博客:https://beginor.github.io/2020/12/16/recovery-of-postgres-in-docker-fail-to-start.html

postgresql官网文档地址:https://www.postgresql.org/docs/current/app-pgresetwal.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值