Pg_resetwal can be used to start the PostgreSQL Docker container when WAL is broken and cannot be started.

The battery of the local machine that was running the PostgreSQL Docker container ran out and fell. When I secured the power supply and restarted it, I got an error and couldn't start up.

Note that the container data is mounted in the directory of the host machine and used persistently. Definition part from docker-compose.yml.

services:
  db:
    image: postgres:12-alpine
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=foobar
    volumes:
      - ./dataDir:/var/lib/postgresql/data

It is a log at startup.

db_1                |
db_1                | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1                |
db_1                | 2020-02-07 01:36:06.106 UTC [1] LOG:  starting PostgreSQL 12.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 8.3.0) 8.3.0, 64-bit
db_1                | 2020-02-07 01:36:06.107 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1                | 2020-02-07 01:36:06.107 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1                | 2020-02-07 01:36:06.270 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1                | 2020-02-07 01:36:06.434 UTC [21] LOG:  database system was interrupted; last known up at 2020-02-07 01:19:57 UTC
db_1                | 2020-02-07 01:36:10.655 UTC [21] LOG:  invalid record length at 1/94338ED0: wanted 24, got 0
db_1                | 2020-02-07 01:36:10.655 UTC [21] LOG:  invalid primary checkpoint record
db_1                | 2020-02-07 01:36:10.655 UTC [21] PANIC:  could not locate a valid checkpoint record
db_1                | 2020-02-07 01:36:10.656 UTC [1] LOG:  startup process (PID 21) was terminated by signal 6: Aborted
db_1                | 2020-02-07 01:36:10.656 UTC [1] LOG:  aborting startup due to startup process failure
db_1                | 2020-02-07 01:36:10.661 UTC [1] LOG:  database system is shut down
db_1                | 2020-02-07 01:36:10.655 UTC [21] PANIC:  could not locate a valid checkpoint record

I spit out this log and the startup stopped. When I looked it up, I heard that the WAL (Write ahead log) was damaged.

To recover from this state, use the command pg_resetxlog.

pg_resetxlog clears the write-ahead log (WAL) and optionally initializes some of the control information stored in the pg_control file. This feature may be needed if these files become corrupted. Use this feature only as a last resort if you cannot start the server due to such corruption.

From Postgres10, pg_resetxlog was renamed to pg_resetwal. Change the command name depending on the version.

docker run --rm -it -v $(pwd)/dataDir:/var/lib/postgres/data/ postgres:12 /bin/bash

Since the data was mounted on the host machine side, mount it.

gosu postgres pg_resetwal -f /var/lib/postgres/data

When the container starts, execute the log initialization command. Execution started successfully.

This is just a forced initialization of the log, so the data integrity may be corrupted. It seems better to make a backup and check the consistency such as reinserting the data.

This time it was the development environment at hand, and at worst it was okay if everything disappeared, As long as it can be recovered, we have decided to take measures up to this point.

Recommended Posts

Pg_resetwal can be used to start the PostgreSQL Docker container when WAL is broken and cannot be started.
"ClassNotFoundException" is displayed when tomcat is started in eclipse and cannot be started
Wait for PostgreSQL to start with Docker and then start the WEB service
[Docker] How to build when the source code is bind-mounted on the container
How to mount the batch file location via WSL2 and start the Docker container
How to solve when you cannot connect to DB with a new container because the port is assigned to the existing docker container
[Android] Solution when the camera cannot be started on Android 9
Systemctl cannot be used on Ubuntu inside Docker container
When I started ansible + docker now, I stumbled from the beginning, but I managed to start it
[Docker] When you enter the db container and look at the data, Japanese is displayed as ???
Wait for the container service to start with docker healthcheck
Settings to be made when HTML or JSP is formatted in Eclipse and the layout is disappointing
Lenovo ThinkPad T14 Gen1 How to solve the problem that the speaker becomes dummy and cannot be used when installing Ubuntu 20.04 on AMD
When requested access to the resource is denied when pushing with Docker
Check the contents by docker pull from ECR and start the container
Until you build the docker environment and start / stop the Ubuntu container
About the range and scope where Day16 variables can be used
[Ruby On Rails] In the create action and destroy action, emergency measures when redirect_to action:: show cannot be (cannot be used)
Push the Docker Image distributed by Docker Hub to Google Container Registry and start the VM based on that Image
What to do when CentOS cannot be started with VirtualBox on Catalina
[Rails] "pry-rails" that can be used when saving with the create method
I used Docker to solidify the template to be developed with spring boot.
767 JSON :: ParserError occurs and the problem that the local server cannot be started
I installed WSL2 without using Microsoft Store and tried to build an environment where Docker can be used
The problem that the localhost page launched by Docker cannot be confirmed in the browser when the ESET firewall is operating
Check when the container cannot be accessed from Host in the Laravel on docker environment using VS Code's Remote container.
When reassigning to an argument in a Ruby method and then calling `super` → The reassigned one is used