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

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.

Related

Handling multiple Postgresql Instances on same port between Local & Docker container

I have an issue where my docker container is delivering an error message, database_1 | 2021-05-03 23:33:49.552 UTC [33] FATAL: role "myname" does not exist, for the postgres container I am running and I'm under the impression that it is possibly tied to the fact that its running on the same port as my postgres instance that runs locally on my computer as a background service. Not completely certain, but it seems strange as the role (or I assume username) is present when I connect to a database with my local instance running. Is there something that I can do to further debug? When I run a local node server for the application the credentials work without any issue.
Here is my docker-compose.yml setup:
version: "3.9"
services:
redis:
image: redis:alpine
database:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_DATABASE}
volumes:
- nextjs_auth_template:/var/lib/postgresql/data/ # persist data even if container shuts down
app:
image: nextjs-auth-boilerplate
build: .
depends_on:
- redis
- database
ports:
- "3000:3000"
environment:
- REDIS_HOST=redis
- DB_HOSTNAME=database
volumes:
nextjs_auth_template:
Here is my .env file:
DB_USERNAME=myname
DB_PASSWORD=''
DB_DATABASE=nextjs_auth_template
DB_HOSTNAME=127.0.0.1
DB_URL=postgresql://myname#127.0.0.1/nextjs_auth_template
If you are running both database instances on the same port and the same host, the problem must be that there is something that tries to connect as that user. If you are running the instance where the user exists, everything works. If you are running the other instance, you'll receive the error.
You'll have to figure out from where the client connects. For that, add %h to log_line_prefix to get the client IP address logged (change the parameter in the postgresql.conf file and reload the server). If you get no IP address in the log, that means that the connection is from your local computer.

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

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

Connecting to Postgres Docker server - authentication failed

I have a PostgreSQL container set up that I can successfully connect to with Adminer but I'm getting an authentication error when trying to connect via something like DBeaver using the same credentials.
I have tried exposing port 5432 in the Dockerfile and can see on Windows for docker the port being correctly binded. I'm guessing that because it is an authentication error that the issue isn't that the server can not be seen but with the username or password?
Docker Compose file and Dockerfile look like this.
version: "3.7"
services:
db:
build: ./postgresql
image: postgresql
container_name: postgresql
restart: always
environment:
- POSTGRES_DB=trac
- POSTGRES_USER=user
- POSTGRES_PASSWORD=1234
ports:
- 5432:5432
adminer:
image: adminer
restart: always
ports:
- 8080:8080
nginx:
build: ./nginx
image: nginx_db
container_name: nginx_db
restart: always
ports:
- "8004:8004"
- "8005:8005"
Dockerfile: (Dockerfile will later be used to copy ssl certs and keys)
FROM postgres:9.6
EXPOSE 5432
Wondering if there is something else I should be doing to enable this to work via some other utility?
Any help would be great.
Thanks in advance.
Update:
Tried accessing the database through the IP of the postgresql container 172.28.0.3 but the connection times out which suggests that PostgreSQL is correctly listening on 0.0.0.0:5432 and for some reason the user and password are not usable outside of Docker even from the host machine using localhost.
Check your pg_hba.conf file in the Postgres data folder.
The default configuration is that you can only login from localhost (which I assume Adminer is doing) but not from external IPs.
In order to allow access from all external addresses vi password authentication, add the following line to your pg_hba.conf:
host all all * md5
Then you can connect to your postgres DB running in the docker container from outside, given you expose the Port (5432)
Use the command docker container inspect ${container_number}, this will tell you which IPaddress:ports are exposed external to the container.
The command 'docker container ls' will help identify the 'container number'
After updating my default db_name, I also had to update the docker-compose myself by explicitly exposing the ports as the OP did
db:
image: postgres:13-alpine
volumes:
- dev-db-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=devdb
- POSTGRES_USER=user
- POSTGRES_PASSWORD=1234
ports:
- 5432:5432
But the key here was restarting the server! DBeaver has connected to localhost:5432 :)

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.