Skip to content

Shutting down postgres gracefully with docker-compose down to avoid recovery process #544

Closed

Description

@csebranek

Sometimes when I shutdown postgres (v9.4.20) with docker-compose down (v1.23.2) , the next time that I run docker-compose up, postgres claims database system was not properly shut down; automatic recovery in progress and takes forever to start back up. Even though when I shut it down the postgres logs say received smart shutdown request. Even trying with docker-compose down -v does not seem to resolve the issue. Here's my docker-compose.yml file.

version: "3.5"
networks:
  network1:
    driver: bridge
    name: sharedservices

services:

  postgres:
    image: postgres:9.4
    volumes:
      - "${PG_CONF_FILE}:/conf/postgresql.conf"
      - "${PG_DATA_DIR}:/var/lib/postgresql/data"
      - "${PG_HBA_FILE}:/conf/pg_hba.conf"
      - "${PG_LOG_DIR}:/logs"
    ports:
      - "${PG_PORT}:5432"
    expose:
      - "5432"
    networks:
      - network1
    environment:
      TZ: America/Chicago
    command: postgres -c log_directory=/logs -c config_file=/conf/postgresql.conf -c hba_file=/conf/pg_hba.conf

Any ideas on how to avoid an "automatic recovery" ?

Activity

  1. added
    questionUsability question, not directly related to an error with the image
    on Jan 17, 2019
  2. wglambert commented on Jan 17, 2019

    @wglambert

    Relevant thread #184

    Noting #184 (comment)

    Smart Shutdown: ... lets existing sessions end their work normally. It shuts down only after all of the sessions terminate.

    So if you want to ensure a graceful shutdown of Postgres then do a pg_ctl stop

  3. tianon commented on Jan 18, 2019

    @tianon
    Member
  4. csebranek commented on Jan 19, 2019

    @csebranek
    Author

    My feeling is that docker-compose sends the SIGTERM but closes the container after some time, which is what you are talking about with the timeout, but doesn't really ensure that postgres is actually shutdown, and it stops the container anyway before postgres is actually shutdown.

  5. tianon commented on Jan 19, 2019

    @tianon
    Member

    Right, so if you increase --stop-timeout (on docker run) / stop_grace_period: (on docker-compose.yml; https://docs.docker.com/compose/compose-file/compose-file-v2/#stop_grace_period), it will give PostgreSQL more time before it gets killed and thus increase your chances of PostgreSQL actually finishing up properly and shutting down cleanly.

    For my own database instances, I typically use a pretty generous --stop-timeout value of 120 since if I'm using docker stop / SIGTERM, I want to give the database as much time as it needs (if I want a faster shutdown, I use docker rm -f / docker kill directly).

  6. csebranek commented on Jan 19, 2019

    @csebranek
    Author

    Oh nice, it seems like stop_grace_period looks promising. I'll give that a shot and report my findings here.

  7. csebranek commented on Jan 23, 2019

    @csebranek
    Author

    So it seems like stop_grace_period does indeed do what it says. However, it still fails to cut some connections and then the container eventually stops leading to another "automatic recovery" by postgres. In my case, I've narrowed it down to the connection between postgres and pgAdmin not terminating, so I'm thinking of doing some sort of a wrapper script to send a SIGINT after some time... e.g.:

    docker-compose exec postgres /bin/bash -c "su - postgres -c '/usr/lib/postgresql/9.4/bin/pg_ctl stop -m fast -D /var/lib/postgresql/data'"

  8. tianon commented on Jan 25, 2019

    @tianon
    Member

    Yeah, that seems reasonable, but at this point I think we've determined this isn't really an issue with the image (certainly not something we can fix), so I'm going to close. 👍

  9. ducttapecoder-vt commented on Jun 21, 2019

    @ducttapecoder-vt
  10. locked as resolved and limited conversation to collaborators on Jun 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionUsability question, not directly related to an error with the image

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions