Postgres shuts down immediately when started with docker-compose
Asked Answered
S

4

64

Postgres shuts down immediately when started with docker-compose. The yaml file used is below

version: '2'

services:   
    postgres:
        image: postgres:9.5
        container_name: local-postgres9.5
        ports:
          - "5432:5432"

The log when docker-compose up command is executed

Creating local-postgres9.5
Attaching to local-postgres9.5
local-postgres9.5 | The files belonging to this database system will be owned by user "postgres".
local-postgres9.5 | This user must also own the server process.
local-postgres9.5 |
local-postgres9.5 | The database cluster will be initialized with locale "en_US.utf8".
local-postgres9.5 | The default database encoding has accordingly been set to "UTF8".
local-postgres9.5 | The default text search configuration will be set to "english".
local-postgres9.5 |
local-postgres9.5 | Data page checksums are disabled.
local-postgres9.5 |
local-postgres9.5 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
local-postgres9.5 | creating subdirectories ... ok
local-postgres9.5 | selecting default max_connections ... 100
local-postgres9.5 | selecting default shared_buffers ... 128MB
local-postgres9.5 | selecting dynamic shared memory implementation ... posix
local-postgres9.5 | creating configuration files ... ok
local-postgres9.5 | creating template1 database in /var/lib/postgresql/data/base/1 ... ok
local-postgres9.5 | initializing pg_authid ... ok
local-postgres9.5 | initializing dependencies ... ok
local-postgres9.5 | creating system views ... ok
local-postgres9.5 | loading system objects' descriptions ... ok
local-postgres9.5 | creating collations ... ok
local-postgres9.5 | creating conversions ... ok
local-postgres9.5 | creating dictionaries ... ok
local-postgres9.5 | setting privileges on built-in objects ... ok
local-postgres9.5 | creating information schema ... ok
local-postgres9.5 | loading PL/pgSQL server-side language ... ok
local-postgres9.5 | vacuuming database template1 ... ok
local-postgres9.5 | copying template1 to template0 ... ok
local-postgres9.5 | copying template1 to postgres ... ok
local-postgres9.5 | syncing data to disk ... ok
local-postgres9.5 |
local-postgres9.5 | WARNING: enabling "trust" authentication for local connections
local-postgres9.5 | You can change this by editing pg_hba.conf or using the option -A, or
local-postgres9.5 | --auth-local and --auth-host, the next time you run initdb.
local-postgres9.5 |
local-postgres9.5 | Success. You can now start the database server using:
local-postgres9.5 |
local-postgres9.5 |     pg_ctl -D /var/lib/postgresql/data -l logfile start
local-postgres9.5 |
local-postgres9.5 | ****************************************************
local-postgres9.5 | WARNING: No password has been set for the database.
local-postgres9.5 |          This will allow anyone with access to the
local-postgres9.5 |          Postgres port to access your database. In
local-postgres9.5 |          Docker's default configuration, this is
local-postgres9.5 |          effectively any other container on the same
local-postgres9.5 |          system.
local-postgres9.5 |
local-postgres9.5 |          Use "-e POSTGRES_PASSWORD=password" to set
local-postgres9.5 |          it in "docker run".
local-postgres9.5 | ****************************************************
local-postgres9.5 | waiting for server to start....LOG:  database system was shut down at 2016-05-16 16:51:54 UTC
local-postgres9.5 | LOG:  MultiXact member wraparound protections are now enabled
local-postgres9.5 | LOG:  database system is ready to accept connections
local-postgres9.5 | LOG:  autovacuum launcher started
local-postgres9.5 |  done
local-postgres9.5 | server started
local-postgres9.5 | ALTER ROLE
local-postgres9.5 |
local-postgres9.5 |
local-postgres9.5 | /docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
local-postgres9.5 |
local-postgres9.5 | LOG:  received fast shutdown request
local-postgres9.5 | LOG:  aborting any active transactions
local-postgres9.5 | LOG:  autovacuum launcher shutting down
local-postgres9.5 | LOG:  shutting down
local-postgres9.5 | waiting for server to shut down....LOG:  database system is shut down
local-postgres9.5 |  done
local-postgres9.5 | server stopped
local-postgres9.5 |
local-postgres9.5 | PostgreSQL init process complete; ready for start up.
local-postgres9.5 |
local-postgres9.5 | LOG:  database system was shut down at 2016-05-16 16:51:55 UTC
local-postgres9.5 | LOG:  MultiXact member wraparound protections are now enabled
local-postgres9.5 | LOG:  database system is ready to accept connections
local-postgres9.5 | LOG:  autovacuum launcher started

Postgres seems to work fine when a container is started using the same image with docker run

docker run --name local-postgres9.5 -p 5432:5432 postgres:9.5
Stemson answered 16/5, 2016 at 17:18 Comment(5)
if your problem solved then share with us!Cycloid
FWIW, looks like this is intentional behavior: github.com/docker-library/postgres/blob/…Diapause
The solution is mentioned in the logs you posted ----------------- local-postgres9.5 | Use "-e POSTGRES_PASSWORD=password" to setBrownfield
I've had the same issue which is a bit of a pain as I'm trying to run liquibase against the database. What is happening for me is my wait script for liquibase is seeing the database is up and running so it attempts to start running then the server is stopped and started like you mentioned, while it is being stopped liquibase errors out as it can not reach the database. Any suggestions would be greatly appreciated.Pfister
@Pfister Checkout this solution: https://mcmap.net/q/301217/-postgres-shuts-down-immediately-when-started-with-docker-compose, it was the same scenario as yours...Hjerpe
A
45

If you look at your log output, the following lines appear towards the end:

local-postgres9.5 | server stopped
local-postgres9.5 |
local-postgres9.5 | PostgreSQL init process complete; ready for start up.

Apparently, stopping and restarting the Postgres server is part of the initialisation process. In fact, the second-to-last line says

local-postgres9.5 | LOG:  database system is ready to accept connections.
Accumulative answered 25/5, 2016 at 12:31 Comment(2)
The problem is it says "database system is ready to accept connections" twice as it starts,stops,starts.Pfister
I couldn't get it to run the first time... It simply becomes unresponsive, it only works when I stop the container and run it again. Before starting the container I'm deleting the previously created one using docker rm container_name and then running again using docker-compose up --build --force-recreate. My docker-compose.yml is pretty much just like the one above. I'd like to have the container up and running even though is the first time I'm creating the container. I also tried to connect to using any other client (DBeaver, psql) but none of them could connect successfully...Hjerpe
H
8

I'm using the same Postgres docker image version of yours (9.5) and I was running into the same problem.

The first time the container is created, Postgres run a series of commands and in the end, it just sends a shutdown signal and the server becomes unresponsive (but it works the second time onwards).

After several trials, I figured that once I tried to connect to the server, before it was ready to accept connections, PostgresDB would shutdown unexpectedly, and the same would happen for any client trying to connect remotely (outside the container).

I came across this error in the following scenario - I have two containers: one for the PostgresDB itself and other containing a Postgres client (psql) that tries to connect to the first container to create some users, databases and run a liquibase script that creates all the schemas.

At first, I was using a loop in my bash script checking if the server was available every 5 seconds, then after the first attempt, Postgres issued a signal to shutdown and became unresponsive.

I could get rid of this error by adding a healthcheck in my docker-compose.yml:

Checkout the CHANGE 1 and CHANGE 2 comments below

version: '3.9'
services:
  postgres-db:
    container_name: ${POSTGRES_HOST}
    image: postgres:9.5
    restart: always
    ports:
      - "5432:5432"
    command: postgres
    expose:
      - 5432
    environment:
      POSTGRES_USER: "${POSTGRES_USER}"
      POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
      POSTGRES_DB: "${POSTGRES_DB}"

      #this ENV variable is only required for the healthcheck section - if you don't specify it, the check command will fail stating the root user doesn't exist in posgres
      PGUSER: "postgres"
    healthcheck:
      #CHANGE 1: this command checks if the database is ready, right on the source db server
      test: [ "CMD-SHELL", "pg_isready" ]
      interval: 5s
      timeout: 5s
      retries: 5

  liquibase:
    container_name: liquibase-schema-config
    image: company/liquibase
    build:
      context: ./liquibase
    environment:
      - PGPASSWORD=${POSTGRES_PASSWORD}
      - PGPORT=${POSTGRES_PORT}
      - PGHOST=${POSTGRES_HOST}
      - PGUSER=${POSTGRES_USER}
      - PGDATABASE=${POSTGRES_DB}

      - JDBC_URL=jdbc:postgresql://${POSTGRES_HOST}:${POSTGRES_PORT}/
      - LIQUIBASE_HOME=${LIQUIBASE_HOME}
    depends_on:
      #CHANGE 2: it prevents issuing a request while the server is starting to depend on the healthy status of postgres-db 
      postgres-db:
        condition: service_healthy

EDIT: There was another issue preventing Docker from accessing VPN protected sites (and even internet sites) while using Cisco AnyConnect on Linux (it doesn't happen on MacOS). To get around this unwanted behavior I installed openconnect sudo apt install openconnect on my Ubuntu and stopped using AnyConnect.

Hope it helps!

Hjerpe answered 1/3, 2022 at 21:59 Comment(0)
A
3

I tried your docker-compose and the service seems running in the container:

root@0afe99de0f0b:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
postgres     1  0.5  0.8 227148 16128 ?        Ss   14:42   0:00 postgres
postgres    74  0.0  0.0 227148  1768 ?        Ss   14:42   0:00 postgres: checkpointer process  
postgres    75  0.0  0.0 227148  1772 ?        Ss   14:42   0:00 postgres: writer process  
postgres    76  0.0  0.0 227148  1768 ?        Ss   14:42   0:00 postgres: wal writer process  
postgres    77  0.0  0.1 227576  2720 ?        Ss   14:42   0:00 postgres: autovacuum launcher process  
postgres    78  0.0  0.0  82132  1888 ?        Ss   14:42   0:00 postgres: stats collector process  
root        79  2.0  0.0  21820  1984 ?        Ss   14:42   0:00 /bin/bash
root        84  0.0  0.0  19092  1296 ?        R+   14:42   0:00 ps aux

Anyway, for my project I use another image for postgresql: https://github.com/sameersbn/docker-postgresql. This one works fine.

Aquiver answered 19/5, 2016 at 14:45 Comment(2)
Thanks. You're right postgres is actually running. I assumed that since docker-compose didn't move past spinning up postgres container that this was an issue. When I used the option -d everything seemed to work fine.Stemson
It's correct that the container runs and restarts if the data directory doesn't exist. This is because of how the official image bootstrap is scripted. More detail here: #65417840Diapause
W
-6

Add password under postgres service in docker-compose.yml as given in the screenshot. Thank you. click to see the screenshot

version: '3'
services:
     postgres:
          image: postgres
          environment:
              - POSTGRES_PASSWORD=postgres_password
Wraf answered 25/4, 2020 at 8:1 Comment(1)
Please include any code in your answers as a text, this way people can easily copy and try it out. Also, this makes it visible to the search engines, so it can be found in the first place.Hammerskjold

© 2022 - 2024 — McMap. All rights reserved.