Postgres service keeps restarting

May 2024 ยท 3 minute read

I'm running the followning docker compose with a postgres service :

version: "3" services: db: image: postgres:latest restart: always container_name: dev_db environment: - POSTGRES_HOST_AUTH_METHOD = trust ports: - 5435:5432 volumes: - dev_data:/var/lib/postgresql/data - ./dbscripts/postgres:/docker-entrypoint-initdb.d volumes: dev_data: 

However, my container keeps restarting and when checking the logs I get the following message :

Error: Database is uninitialized and superuser password is not specified. You must specify POSTGRES_PASSWORD to a non-empty value for the superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run". You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all connections without a password. This is *not* recommended. See PostgreSQL documentation about "trust": https://www.postgresql.org/docs/current/auth-trust.html 

Anyone knows the source of this issue ?

2 Answers

There is a syntax problem in the environment variable which you defined. Please try with the below docker-compose.yml

version: "3" services: db: image: postgres:latest restart: always container_name: dev_db environment: - POSTGRES_HOST_AUTH_METHOD=trust ports: - 5435:5432 volumes: - dev_data:/var/lib/postgresql/data - ./dbscripts/postgres:/docker-entrypoint-initdb.d volumes: dev_data: 

Logs of the docker-compose up command

docker-compose up Creating network "test_default" with the default driver Pulling db (postgres:latest)... latest: Pulling from library/postgres afb6ec6fdc1c: Already exists 51be5f829bfb: Pull complete e707c08f571a: Pull complete 98ddd8bce9b5: Pull complete 5f16647362a3: Pull complete 5d56cdf9ab3b: Pull complete 2207a50ca41d: Pull complete a51d14a628f3: Pull complete 24dcb11335d0: Pull complete 54cc759cb0bb: Pull complete debc11d66570: Pull complete 3ffd0589b5fc: Pull complete 490b7ee49751: Pull complete 3511c6be34a0: Pull complete Digest: sha256:ec7cfff29672a2f676c11cc53ae7dafe63a57ccefc2b06ea423493227da29b9c Status: Downloaded newer image for postgres:latest Creating dev_db ... done Attaching to dev_db dev_db | ******************************************************************************** dev_db | WARNING: POSTGRES_HOST_AUTH_METHOD has been set to "trust". This will allow dev_db | anyone with access to the Postgres port to access your database without dev_db | a password, even if POSTGRES_PASSWORD is set. See PostgreSQL dev_db | documentation about "trust": dev_db | https://www.postgresql.org/docs/current/auth-trust.html dev_db | In Docker's default configuration, this is effectively any other dev_db | container on the same system. dev_db | dev_db | It is not recommended to use POSTGRES_HOST_AUTH_METHOD=trust. Replace dev_db | it with "-e POSTGRES_PASSWORD=password" instead to set a password in dev_db | "docker run". dev_db | ******************************************************************************** dev_db | The files belonging to this database system will be owned by user "postgres". dev_db | This user must also own the server process. dev_db | dev_db | The database cluster will be initialized with locale "en_US.utf8". dev_db | The default database encoding has accordingly been set to "UTF8". dev_db | The default text search configuration will be set to "english". dev_db | dev_db | Data page checksums are disabled. dev_db | dev_db | fixing permissions on existing directory /var/lib/postgresql/data ... ok dev_db | creating subdirectories ... ok dev_db | selecting dynamic shared memory implementation ... posix dev_db | selecting default max_connections ... 100 dev_db | selecting default shared_buffers ... 128MB dev_db | selecting default time zone ... Etc/UTC dev_db | creating configuration files ... ok dev_db | running bootstrap script ... ok dev_db | performing post-bootstrap initialization ... ok dev_db | syncing data to disk ... ok dev_db | dev_db | dev_db | Success. You can now start the database server using: dev_db | dev_db | pg_ctl -D /var/lib/postgresql/data -l logfile start 

I think docker is not able to read your environment variable because it's not set properly. The correct syntax is

environment: - "POSTGRES_HOST_AUTH_METHOD=trust" 
1

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobWtoZWV9cYCOqaasrJensrR50p6pr6GTmnqsscSpqmaqlajBor7ToqWg