docker-library / postgres

Docker Official Image packaging for Postgres
http://www.postgresql.org
MIT License
2.14k stars 1.11k forks source link

Report errors "FATAL: role "XXX" does not exist" when POSTGRES_USER is customized. #1138

Closed Chr15t0pher closed 7 months ago

Chr15t0pher commented 9 months ago

I'm a totally newbie, trying to using a shell script to run postgres image. Here is my code:

DB_USER="${POSTGRES_USER:=admin}"
DB_PASSWORD="${POSTGRES_PASSWORD:=admin}"
DB_NAME="${POSTGRES_DB:=scaffold}"
DB_PORT="${POSTGRES_PORT:=5432}"
DB_HOST="${POSTGRES_HOST:=localhost}"

docker run \
  --name "postgres_$(date '+%s')" \
  -e POSTGRES_USER=${DB_USER} \
  -e POSTGRES_PASSWORD=${DB_PASSWORD} \
  -e POSTGRES_DB=${DB_NAME} \
  -p "${DB_PORT}":5432 \
  -d \
  postgres -N 1000

until PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do
  >&2 echo "Postgres is still unavailable - sleeping"
  sleep 1
done

The errors I've been able to see are:

psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL:  role "admin" does not exist

What am I missing and how can I fix this so I can actually proceed with Postgres? When i set DB_USER to the default value postgres, it is Ok. But i just don't know the reason.

LaurentGoderre commented 9 months ago

I just tried this and I was able to connect. Maybe there is an issue with your docker networking. What do you get if you run docker ps?

Chr15t0pher commented 9 months ago

I just tried this and I was able to connect. Maybe there is an issue with your docker networking. What do you get if you run docker ps?

I tried to run docker ps, it worked as i expected, i got the activated containers.

docker ps --filter 'name=postgres' --format '{{.ID}}'
CONTAINER ID   IMAGE      COMMAND                   CREATED         STATUS         PORTS                    NAMES
bdb2469f4a82   postgres   "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes   0.0.0.0:5432->5432/tcp   postgres_1698292090

I thought after running docker run \ --name "postgres_$(date '+%s')" \ -e POSTGRES_USER=${DB_USER} \ -e POSTGRES_PASSWORD=${DB_PASSWORD} \ -e POSTGRES_DB=${DB_NAME} \ -p "${DB_PORT}":5432 \ -d \ postgres -N 1000 , i should manually set $DB_USER. Then, i tried to modify the script like this:

docker run \
    --name "${RUNNING_POSTGRES_CONTAINER}" \
    -e POSTGRES_USER=${DB_USER} \
    -e POSTGRES_PASSWORD=${DB_PASSWORD} \
    -e POSTGRES_DB=${DB_NAME} \
    -p "${DB_PORT}":5432 \
    -d \
    postgres -N 1000

sleep 5
docker exec -it $RUNNING_POSTGRES_CONTAINER psql -U $DB_USER -d $DB_NAME -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';"

However, it reports : ERROR: role "admin" already exists.

Chr15t0pher commented 9 months ago

I just tried this and I was able to connect. Maybe there is an issue with your docker networking. What do you get if you run docker ps?

According to https://stackoverflow.com/questions/11919391/postgresql-error-fatal-role-username-does-not-exist, maybe i must use the system user postgres to init database?

LaurentGoderre commented 9 months ago

The user is successfully created. Do you have postgres installed locally as well? I am wondering if your script isn't connecting to your container postgres but another one.

Chr15t0pher commented 9 months ago

The user is successfully created. Do you have postgres installed locally as well? I am wondering if your script isn't connecting to your container postgres but another one.

Yes, i have installed locally. But i did not start local postgres, i just use the psql as a tool to connect to the postgres container.