1

I tried to restart Postgres in Docker using 'docker restart ' command. It got stopped but I'm not able to start it. When I run the command 'ps -a' it says the status as 'Exited'. Is there any way to start it again? I don't want to loose any data in that database.
The container had one active connection during restarting. Is that creating a problem?

1
  • You need to add a lot more information to your question. What command was used to start the original container? What version of Docker? What host OS is Docker installed? If installed on macOS, are you using Docker for Mac or Docker Machine? What is the output of docker logs <container_id>? What is the exit code? Commented Jun 14, 2017 at 21:34

1 Answer 1

1

If the container crashed due to a bug or something, you may not be able to restart it. However, you should still be able to recover at least part of your data by making a new image out of the container that you want to recover. Here's how you do it:

  1. First, list all the containers that have run in your machine:

    docker ps -a
    
  2. Find out which one is the container that run with all the data you want to recover. You should be able to figure out from the CREATED field (you know when you started it).

  3. Grab the hash (CONTAINER_ID) of the container, and execute the following command:

    docker commit <hash> <a_new_name:tag>
    

    This will save the container as an image that you can execute.

  4. Execute the container with a bash or sh session, depending on what our base image offers:

    docker run --entrypoint sh/bash -it <a_new_name:tag>
    

This will give you access to the state of the container at the time of exiting, which will allow you to inspect its conditions, find bugs, and possibly recover some data. Good luck!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I just checked the logs. It says 'error: seems like another PostgreSQL server uses DB data because the file '/var/lib/pgsql/data/data/postmaster.pid' already exists. If you are sure that the pidfile is leftover, use 'clear_pgdata_pidfile' option (see 'container-usage' for more info)'. I'm trying to recover the same container.
I committed the image and created a new container using that image. It created a new container but I was not able to connect to the database. Not sue about the reason. Finally logged in as a root user and deleted the '/var/lib/pgsql/data/data/postmaster.pid'. Now I was able to start the same container successfully. I will explore more on how I can create the container using the created image.
I would never recommend committing an image from an existing container like this. Your data should have been in a volume, and 100% accessible. Use docker inspect <container> to see whether a volume was there. If you are using the official postgres image, you will definitely have a volume for your /var/lib/pgsql folder.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.