There is a message in Docker PostgresQL docs:
Warning: the Docker specific variables will only have an effect if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup.
So when I recreate my postgres container with docker-compose down
and docker-compose up -d --remove-orphans
, it throws me errors like:
FATAL: password authentication failed for user "myuser"
It happens because postgres sees that there is the volume, so it skips all scripts and .env files (where I set POSTGRES_USER
and POSTGRES_PASSWORD
).
How this can be solved? (I mean provide vars from .env file everytime I re-create postgres container)
p.s. It is impossible for me to delete old volume as I have data there.
Here is my docker-compose.yml
file:
postgres:
image: postgres:alpine
restart: always
expose:
- 5432
volumes:
- "./init/postgres:/docker-entrypoint-initdb.d"
- "./data/postgres_data:/var/lib/postgresql/data"
environment:
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: ${POSTGRES_USR}
POSTGRES_PASSWORD: ${POSTGRES_PWD}
My .env file:
# Postgres
export POSTGRES_USR="someuser"
export POSTGRES_PWD="somepwd"
export POSTGRES_DB="somedb"
export POSTGRES_URL="postgres://${POSTGRES_USR}:${POSTGRES_PWD}@postgres:5432/${POSTGRES_DB}?sslmode=disable"