Skip to content

Postgres not actually up when initialization script is run on a new container image, not accepting connections. #941

@JeffGradyAtMolarCo

Description

@JeffGradyAtMolarCo

Hi, all. My team has been using postgres:latest for a couple months and we're setting up our db user and credentials etc. via a bash script called database_setup.sh in /docker-entrypoint-initdb.d/.

In the past couple days, this seems to have stopped working. The exact failure mode seems to be that our initialization script is being run when postgres claims to be up, but isn't accepting connections as far as psql can tell:

% docker logs -f d99a893ceb7e
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok


Success. You can now start the database server using:

    pg_ctl -D /var/lib/postgresql/data -l logfile start

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
waiting for server to start....2022-03-08 14:34:38.124 UTC [49] LOG:  starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2022-03-08 14:34:38.126 UTC [49] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-03-08 14:34:38.129 UTC [50] LOG:  database system was shut down at 2022-03-08 14:34:37 UTC
2022-03-08 14:34:38.132 UTC [49] LOG:  database system is ready to accept connections
 done
server started

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/database_setup.sh
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
	Is the server running on that host and accepting TCP/IP connections?
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
	Is the server running on that host and accepting TCP/IP connections?
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
	Is the server running on that host and accepting TCP/IP connections?
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
	Is the server running on that host and accepting TCP/IP connections?

PostgreSQL Database directory appears to contain a database; Skipping initialization

2022-03-08 14:34:38.679 UTC [1] LOG:  starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2022-03-08 14:34:38.679 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2022-03-08 14:34:38.679 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2022-03-08 14:34:38.681 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-03-08 14:34:38.686 UTC [28] LOG:  database system was interrupted; last known up at 2022-03-08 14:34:38 UTC
2022-03-08 14:34:38.779 UTC [28] LOG:  database system was not properly shut down; automatic recovery in progress
2022-03-08 14:34:38.782 UTC [28] LOG:  invalid record length at 0/16FAE80: wanted 24, got 0
2022-03-08 14:34:38.782 UTC [28] LOG:  redo is not required
2022-03-08 14:34:38.789 UTC [1] LOG:  database system is ready to accept connections

The important part in the above log is:

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/database_setup.sh
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
	Is the server running on that host and accepting TCP/IP connections?
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
	Is the server running on that host and accepting TCP/IP connections?
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
	Is the server running on that host and accepting TCP/IP connections?
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
	Is the server running on that host and accepting TCP/IP connections?

Our db init script isn't doing anything exotic:

...
export PGUSER=postgres
export PGDATABASE=postgres

psql -c "CREATE USER $APP_DATABASE_USER WITH PASSWORD '$APP_DATABASE_PASSWORD'";
psql -c "CREATE DATABASE $APP_DATABASE_NAME";
psql -c "GRANT ALL PRIVILEGES ON DATABASE $APP_DATABASE_NAME TO $APP_DATABASE_USER";
psql -c "ALTER USER $APP_DATABASE_USER CREATEDB";

Here's our docker-compose.yml entry for postgres:

  postgres:
    restart: always
    image: postgres:latest
    ports:
      - "5432:5432"
    volumes:
      - ./postgres/bin/database_setup.sh:/docker-entrypoint-initdb.d/database_setup.sh
    environment:
      PGHOST: "localhost"
    env_file:
      - .env
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres" ]
      interval: 5s
      timeout: 5s
      retries: 5

Any thoughts as to what's going on?

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