Postgres in docker returns 'password authentication failed (..) roles "username" does not exist' - postgresql

What I'm trying to do: connect to postgres db with a FastAPI app via common docker-compose file. It used to work until I changed postgres configuration from default.
I'm aware this is a common problem and I tried following every tip I found so far.
I did double check spelling of evn variables. I did remove volumes, images, networks and rebuild it from scratch multiple times by now.
Here are relevant parts of docker-compose
version: "3.4"
networks:
internal:
external: false
services:
db:
image: postgres:11
ports:
- "5432:5432"
environment:
- POSTGRES_USER=test_user
- POSTGRES_PASSWORD=test_pass
- POSTGRES_DB=test_db
networks:
- internal
my_app:
depends_on:
- db
networks:
- internal
Here's how I present db to app in code.
DATABASE_URL = "postgres://test_user:test_pass#db:5432/test_db"
I continue to get 'password authentication failed for user "test_user" / Role "test_user" does not exist. / Connection matched pg_hba.conf line 95: "host all all all md5"
What did I manage to miss?

For those who find it, the problem was actually in how I tried to provide env variables.
environment:
- POSTGRES_USER: test_user
- POSTGRES_PASSWORD: test_pass
- POSTGRES_DB: test_db
works

Related

postgres database issues with encoding UTF-8

I have created a docker image with this command docker compose up -d
where I was able to load pgAdmin instance in http://localhost:5050/browser/
create a database and table in the same , credentials are working properly.
However when I start to run my main spring boot application CustomerApplication it fails with below error >
org.postgresql.util.PSQLException: FATAL: la autentificación password falló para el usuario «amigoscode» (pgjdbc: autodetected server-encoding to be ISO-8859-1, if the message is not readable, please check database logs and/or host, port, dbname, user, password, pg_hba.conf)
I do not know what is wrong, my credentials are correct.
what seems to be the issue?
below are application.yml and docker-compose.yml
docker-compose.yml
services:
postgres:
container_name: postgres
image: postgres
environment:
POSTGRES_USER: amigoscode
POSTGRES_PASSWORD: password
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
pgadmin:
container_name: pgadmin
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
PGADMIN_CONFIG_SERVER_MODE: 'False'
volumes:
- pgadmin:/var/lib/pgadmin
ports:
- "5050:80"
networks:
- postgres
restart: unless-stopped
networks:
postgres:
driver: bridge
volumes:
postgres:
pgadmin:
application.yml
server:
port: 8080
spring:
application:
name: customer
datasource:
username: amigoscode
url: jdbc:postgresql://localhost:5432/customer
password: password
jpa:
hibernate:
ddl-auto: create-drop
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
show-sql: true
customer table Script
CREATE DATABASE customer
WITH
OWNER = amigoscode
ENCODING = 'UTF8'
LC_COLLATE = 'en_US.utf8'
LC_CTYPE = 'en_US.utf8'
TABLESPACE = pg_default
CONNECTION LIMIT = -1;
Since I have a postgres instance installed and running in my local (port 5432), the microservice customer was trying to connect to that instance, not the one from docker which was using the same port.
the solution was to change the url port from application.yml
url: jdbc:postgresql://localhost:5433/customer
and the port of postgres from docker-compose.yml
from
ports:
- "5432:5432"
to
ports:
- "5433:5432"
so microservice connects to the postgres instance in the docker image, not the local one
then re-run docker command docker compose up -d
run the CustomerApplication (SprinbgootApplication) and this time
application starts up nice and smoothly by creating the customer table.
When you created the role amigoscode did you actually set a password for it?
CREATE ROLE amigoscode WITH LOGIN PASSWORD 'password';
You may have logged into pgadmin as the postgres superuser, created the role amigoscode and never set a password. The encoding issue looks similar to the one specified here which appears when the supplied password is incorrect. It looks like that pgjdbc bug was fixed in 2014, but has possibly regressed in more modern versions >= 42.2.x as there is currently a similar unresolved issue.
Other likely alternatives:
SPRING_DATASOURCE_PASSWORD environment variable is set to something else.
You also have an application.properties file which has a different password that will take precedence over the one in application.yml.
You have a different password specified in any of the property sources that spring checks before application.yml (discussion of precedence here)
The same happened to me, the problem is that i was running another instance that was using the port 5432 (postgresql server and docker), I was using dbeaver and it was trying to connect to that instance instead the one that i wanted to use.
Check which applications are using the port and close the ones that you don't need and try again to connect to the database.

Postgres and Docker Compose; password authentication fails and role 'postgres' does not exist. Cannot connect from pgAdmin4

I have a docker-compose that brings up the psql database as below, currently I'm trying to connect to it with pgAdmin4 (not in a docker container) and be able to view it. I've been having trouble authenticating with the DB and I don't understand why.
docker-compose
version: "3"
services:
# nginx and server also have an override, but not important for this q.
nginx:
ports:
- 1234:80
- 1235:443
server:
build: ./server
ports:
- 3001:3001 # app server port
- 9230:9230 # debugging port
env_file: .env
command: yarn dev
volumes:
# Mirror local code but not node_modules
- /server/node_modules/
- ./server:/server
database:
container_name: column-db
image: 'postgres:latest'
restart: always
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres # The PostgreSQL user (useful to connect to the database)
POSTGRES_PASSWORD: root # The PostgreSQL password (useful to connect to the database)
POSTGRES_DB: postgres # The PostgreSQL default database (automatically created at first launch)
volumes:
- ./db-data/:/var/lib/postgresql/data/
networks:
app-network:
driver: bridge
I do docker-compose up then check the logs, and it says that it is ready for connections. I go to pgAdmin and enter the following:
where password is root. I then get this error:
FATAL: password authentication failed for user "postgres"
I check the docker logs and I see
DETAIL: Role "postgres" does not exist.
I'm not sure what I'm doing wrong, according to the docs the super user should be created with those specifications. Am I missing something? Been banging my head against this for an hour now. Any help is appreciated!
#jjanes solved it in a comment, I had used a mapped volume and never properly set up the db. Removed the volume and we're good to go.

Authentication failed when logging in to Postgres database created via docker-compose

I have set up the following docker-compose.yml file to set up and run PostgreSQL and PgAdmin.
version: '3.1'
services:
db:
image: postgres:latest
container_name: postgres-dopp
restart: unless-stopped
environment:
POSTGRES_USER: dopp_dev
POSTGRES_PASSWORD: dopp_dev_pass
PGDATA: /data/postgres
ports:
- "5432:5432"
volumes:
- dbdata-dopp:/data/postgres
networks:
- network-dopp
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: pgadmin4#pgadmin.org
PGADMIN_DEFAULT_PASSWORD: admin
PGADMIN_CONFIG_SERVER_MODE: 'False'
depends_on:
- db
volumes:
- dbdata-dopp:/data/pgadmin
ports:
- "5050:80"
networks:
- network-dopp
networks:
network-dopp:
driver: bridge
volumes:
dbdata-dopp:
name: dopp-db-data
driver: local
This works fine, insofar as I can navigate to PgAdmin in my host machine's browser and through that I can connect to the database using the credentials I've defined in the environment variables. However, when attempting to make a direct connection to the postgres database from my host machine (by connecting to localhost:5432, since I have configured to expose that port), I then get the following error response:
[28P01] FATAL: password authentication failed for user "dopp_dev"
I'm fairly new to the peculiarities of Postgres and docker configuration, so I'm not sure what is causing Postgres to say that password authentication fails when connecting from my host machine, while it works perfectly fine if I do it through PgAdmin, which is on the same internal docker network.
Actually, I discovered that the docker postgres service's port 5432 was being shadowed by a local postgres instance running my host machine.

Docker password authentication failed for user "postgres"

I'm writing a docker-compose file to launch some services. But the db service is a trouble maker, I always get this error:
FATAL: password authentication failed for user "postgres"
DETAIL: Password does not match for user "postgres".
Connection matched pg_hba.conf line 95: "host all all all md5"
I've read a lot of threads, and I've correctly set the POSTGRES_USER and POSTGRES_PASSWORD. I have also remove the previous volumes and container to force postgresql to re-init the password. But I can't figure out why it's still not working.
So what is the correct way to force the re-initialization of the postgresql image. So I would be able to connect to my database.
I've seen that this error: Connection matched pg_hba.conf line 95: "host all all all md5", and I've heard about the postgres conf file. But it's an official container it's supposed to work, isn't it ?
version: '3'
services:
poll:
build: poll
container_name: "poll"
ports:
- "5000:80"
networks:
- poll-tier
environment:
- REDIS_HOST=redis
depends_on:
- redis
worker:
build: worker
container_name: "worker"
networks:
- back-tier
environment:
- REDIS_HOST=redis
- POSTGRES_HOST=db
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=root
depends_on:
- redis
- db
redis:
image: "redis:alpine"
container_name: "redis"
networks:
- poll-tier
- back-tier
result:
build: result
container_name: "result"
ports:
- "5001:80"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=root
- POSTGRES_HOST=db
- RESULT_PORT=80
networks:
- result-tier
depends_on:
- db
db:
image: "postgres:alpine"
container_name: "db"
restart: always
networks:
- back-tier
- result-tier
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=root
- POSTGRES_DB=postgres
volumes:
db-data:
driver: local
networks:
poll-tier: {}
back-tier: {}
result-tier: {}
I'm expected to get the db connected, and not password authentication failed for user "postgres".
Make sure your APPs (not the database container) are actually using the POSTGRES_USER and POSTGRES_PASSWORD variables. I suspect they are looking for something like DB_USER or similar and so aren't getting the right values in.
By default, every PostgreSQL database driver and admin tool defaults to the postgres user. This may explain why the error message complains about postgres even if the environment variable isn't being used.
A good way to verify is to change all references to the database user in the docker-compose file to something like postgres2. I suspect you'll still see apps complaining that password auth failed for postgres.
In my case, it was caused by postgres.exe service on Windows 10 running in the background. When I stopped the service, uninstalled PostgreSQL 12 from Windows, and restarted, I could finally connect to Postgres Docker container.

Authentication error when connecting to Postgres in a Docker container

I'm getting the following error when I try to connect to a Postgres database inside a Docker container. I'm using pgAdmin 4 on windows as my client.
Password does not match for user "postgres".
My docker-compose file is as follows:
version: "3"
services:
db:
image: postgres
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=postgres
ports:
- "5433:5432"
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
Not sure what I'm doing wrong. The file states that the username and password are both postgres. I was able to connect when I just docker run the image. Now I'm using the compose file I seem to be getting this problem.
Fixed by doing the following:
Stopped container.
Removed container.
Removed volume.
Volume must have contained previous configuration.