I have docker-compose.dev.yaml
file where I specified configuration for starting PostgreSQL
and other containers. But the problem here is when I enter docker compose -f docker-compose.dev.yaml up --build
command, I do not get my user created.
To begin with, here is my yaml
file configurations for postgres:
services:
# PostgreSQL
db:
image: postgres:16.1-alpine
restart: always
env_file:
- .env
ports:
- '5432:5432'
volumes:
- db:/var/lib/postgresql/data
healthcheck:
test: [ "CMD-SHELL", "sh -c 'pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}'" ]
interval: 10s
timeout: 3s
retries: 3
volumes:
db:
Here is .env
file, located in the same directory as the docker-compose.dev.yaml
:
POSTGRES_DB=dbStartup
POSTGRES_USER=dbUser
POSTGRES_PASSWORD=dbPass
And finally logs when I run docker compose -f docker-compose.dev.yaml up --build
after deleting volume for the container (I also tried to clean all docker data including all images, volumes, containers, cache):
startup-db-1 | The files belonging to this database system will be owned by user "postgres".
startup-db-1 | This user must also own the server process.
startup-db-1 |
startup-db-1 | The database cluster will be initialized with locale "en_US.utf8".
startup-db-1 | The default database encoding has accordingly been set to "UTF8".
startup-db-1 | The default text search configuration will be set to "english".
startup-db-1 |
startup-db-1 | Data page checksums are disabled.
startup-db-1 |
startup-db-1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
startup-db-1 | creating subdirectories ... ok
startup-db-1 | selecting dynamic shared memory implementation ... posix
startup-db-1 | selecting default max_connections ... 100
startup-db-1 | selecting default shared_buffers ... 128MB
startup-db-1 | selecting default time zone ... UTC
startup-db-1 | creating configuration files ... ok
startup-db-1 | running bootstrap script ... ok
startup-db-1 | sh: locale: not found
startup-db-1 | 2024-01-27 01:20:34.995 UTC [30] WARNING: no usable system locales were found
startup-db-1 | performing post-bootstrap initialization ... ok
startup-db-1 | initdb: warning: enabling "trust" authentication for local connections
startup-db-1 | initdb: hint: 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.
startup-db-1 | syncing data to disk ... ok
startup-db-1 |
startup-db-1 |
startup-db-1 | Success. You can now start the database server using:
startup-db-1 |
startup-db-1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
startup-db-1 |
startup-db-1 | waiting for server to start....2024-01-27 01:20:35.690 UTC [36] LOG: starting PostgreSQL 16.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 13.2.1_git20231014) 13.2.1 20231014, 64-bit
startup-db-1 | 2024-01-27 01:20:35.691 UTC [36] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
startup-db-1 | 2024-01-27 01:20:35.697 UTC [39] LOG: database system was shut down at 2024-01-27 01:20:35 UTC
startup-db-1 | 2024-01-27 01:20:35.702 UTC [36] LOG: database system is ready to accept connections
startup-db-1 | done
startup-db-1 | server started
startup-db-1 | CREATE DATABASE
startup-db-1 |
startup-db-1 |
startup-db-1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
startup-db-1 |
startup-db-1 | 2024-01-27 01:20:35.850 UTC [36] LOG: received fast shutdown request
startup-db-1 | waiting for server to shut down....2024-01-27 01:20:35.851 UTC [36] LOG: aborting any active transactions
startup-db-1 | 2024-01-27 01:20:35.855 UTC [36] LOG: background worker "logical replication launcher" (PID 42) exited with exit code 1
startup-db-1 | 2024-01-27 01:20:35.859 UTC [37] LOG: shutting down
startup-db-1 | 2024-01-27 01:20:35.861 UTC [37] LOG: checkpoint starting: shutdown immediate
startup-db-1 | 2024-01-27 01:20:35.910 UTC [37] LOG: checkpoint complete: wrote 925 buffers (5.6%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.020 s, sync=0.025 s, total=0.051 s; sync files=301, longest=0.002 s, average=0.001 s; distance=4269 kB, estimate=4269 kB; lsn=0/191BA30, redo lsn=0/191BA30
startup-db-1 | 2024-01-27 01:20:35.919 UTC [36] LOG: database system is shut down
startup-db-1 | done
startup-db-1 | server stopped
startup-db-1 |
startup-db-1 | PostgreSQL init process complete; ready for start up.
startup-db-1 |
startup-db-1 | 2024-01-27 01:20:35.979 UTC [1] LOG: starting PostgreSQL 16.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 13.2.1_git20231014) 13.2.1 20231014, 64-bit
startup-db-1 | 2024-01-27 01:20:35.980 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
startup-db-1 | 2024-01-27 01:20:35.980 UTC [1] LOG: listening on IPv6 address "::", port 5432
startup-db-1 | 2024-01-27 01:20:35.982 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
startup-db-1 | 2024-01-27 01:20:35.987 UTC [52] LOG: database system was shut down at 2024-01-27 01:20:35 UTC
startup-db-1 | 2024-01-27 01:20:35.992 UTC [1] LOG: database system is ready to accept connections
Also, when I try to do something with the db
container for postgres (create migration from Python
to PostgreSQL
, for example) the operation failes with these logs:
startup-db-1 | 2024-01-27 02:08:30.527 UTC [44] FATAL: password authentication failed for user "startUser"
startup-db-1 | 2024-01-27 02:08:30.527 UTC [44] DETAIL: Role "startUser" does not exist.
startup-db-1 | Connection matched file "/var/lib/postgresql/data/pg_hba.conf" line 128: "host all all all scram-sha-256"
I have been struggling with the problem for two days and even can not find something useful that can help me solve it. I have already tried deleting all volumes, images, cache, containers, but it did not help me.
I would be very grateful for any help with the issue.
P.S. Sorry if I made some mistakes in the text, as English is not my native language.
2
Answers
The problem turned out to be so silly, that I even didn't notice it. I used
DSN
for the database with variables (e.g.POSTGRES_USER
) different from variables specified in.env
..Based on the provided logs, it seems that the PostgreSQL container is not creating the user specified in the .env file. This issue could be caused by the POSTGRES_USER environment variable not being passed correctly to the container.
Here are a few things you can try to troubleshoot and resolve the problem:
Check the .env file: Verify that the .env file is located in the same directory as the docker-compose.dev.yaml file and that it contains the correct values for POSTGRES_DB, POSTGRES_USER, and POSTGRES_PASSWORD. Ensure that there are no leading or trailing spaces in the variable values.
Check the environment variable usage: Ensure that the .env file is being correctly loaded by the PostgreSQL container. You can add a command section to the service definition in your docker-compose.dev.yaml file to print the environment variables inside the container. For example:
After adding the command section, run docker-compose -f docker-compose.dev.yaml up –build and check the output to see if the environment variables are correctly loaded.
Verify the container configuration: Confirm that the PostgreSQL container is using the POSTGRES_USER environment variable to create the user. You can inspect the container’s startup logs to check if the POSTGRES_USER value is used correctly. Look for log lines similar to:
If you don’t see any log lines indicating that the user is being created, there may be an issue with the environment variable configuration or the container image itself.
Try a different PostgreSQL image: It’s possible that the issue is specific to the postgres:16.1-alpine image you are using. You can try using a different PostgreSQL image to see if it resolves the problem. For example, you can use the postgres:latest image or try a different version in the postgres repository.
Check for conflicting environment variables: Make sure there are no conflicting environment variables defined elsewhere in your environment or in the Docker Compose files that could override the values specified in the .env file.
By following these troubleshooting steps, you should be able to identify the cause of the issue and resolve it.