password authentication failed for user "postgres" with docker-compose up on EC2 - postgresql

On EC2 linux server create by docker-machine, when I launch docker postgres:10.6 by docker-compose up, I have these loop errors :
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 don't have these errors if I start container manually
=> docker run -e POSTGRES_PASSWORD=myPassword postgres:10.6
I don't have these errors in my local docker.
My docker-compose :
db:
container_name: postgres
image: postgres:10.6
restart: always
environment:
POSTGRES_PASSWORD: myPassword
ports:
- "5432:5432"
Does anyone know how to fix this?

It might be because the volume (or bind mount directory) being already initialized after your first start. The postgres user, and database creation only happens on the first start (ie, /var/lib/postgresql/data must not already contain database files).
Try to run:
docker-compose rm -fv postgres to delete any containers or volumes (in particular).
docker-compose up -d to start your container.

Sorry for that I have the answer to my question, it's not a bug I just have something that tries to connect permanently to postgres (through port 5432 which is open) ...
After search, I think it's an attempt at hacking because incoming connections never come from the same IP
connection received: host=45.120.120.149.81 port=47118
connection received: host=210.4.125.252 port=44774
connection received: host=82.223.55.254 port=36320
etc....

Related

Connecting to docker PostgreSQL from PhpStorm gives password authentication failed

The problem
I am trying to connect to PostgreSQL from PhpStorm, but it returns the following error:
[28P01] FATAL: password authentication failed for user "app"
The situation
I have the following .env file setup:
POSTGRES_DB=app
POSTGRES_USER=app
POSTGRES_PASSWORD=password
POSTGRES_VERSION=15
And the following in docker-compose.yml:
version: '3'
services:
database:
image: postgres:${POSTGRES_VERSION}-alpine
container_name: database
env_file:
- .env
volumes:
- db-data:/var/lib/postgresql/data:rw
ports:
- '5432'
volumes:
db-data:
When running docker-compose up -d that does create a container & volume successfully.
So then I enter the following into my PhpStorm:
But then the error pops up, entering the password again doesn't fix anything.
I am running this on Ubuntu 22.04.1 LTS
What I've tried
I've rebuilt the container several times with different user & password combinations (making sure to use docker-compose down -v to get rid of the volume), all with the same result.
I've tried changing the password by executing docker exec -it database psql -U app and then running ALTER ROLE app WITH PASSWORD 'password', but this did not change anything.
I also saw online that it might have to do something with authentication of the user being setup as ident, but I cannot find a way to change this in the docker-compose.yml file.
The question
How could I set this up so I can connect my PhpStorm to the PostgreSQL database properly?
In my case I was running Windows 10 with WSL2 (Ubuntu). I had installed Postgres in the Ubuntu instance as part of setting up an app. I'd removed the app but the Postgres server was still running. When I attempted to connect to the Docker Postgres instance using localhost:5432 I was instead connecting to the WSL2 Postgres instance.
In my case, since I was no longer using Postgres in WSL2 I removed it and this resolved the issue. You could also stop it or use the host name/IP as mentioned by #jjanes

cannot access postgres db running docker container from local machine

I have been spending 3-4 hours on this and still have not found a solution.
I can successfully run the docker container and use psql from the container bash, however, when I try to call the db from my local machine I continue to get this error message:
error role "postgres" does not exist
I have already tried editing "listen_addresses" in the postgresql.conf file from the container bash
My setup:
I am using a macbook - Monterey 12.4
my docker compose file:
version: '3.4'
services:
postgres:
image: postgres:latest
ports:
- "5432:5432"
environment:
- POSTGRES_DB=postgres_db
- POSTGRES_USER=testUser
- POSTGRES_PASSWORD=testPW
volumes:
- postgres-data:/var/lib/postgresql/db
but this issue occurs if I do it through the standard CLI command as well, i.e:
docker run -d -p 5432:5432 --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword postgres
I tried to follow this tutorial but it didnt work:
[https://betterprogramming.pub/connect-from-local-machine-to-postgresql-docker-container-f785f00461a7][1]
when I try this command:
psql -h localhost -p 5432 -U postgres -W
it doesnt work:
psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL: role "postgres" does not exist
Also for reference, the user "postgres" does exist in postgres - as a superuser
Replace POSTGRES_USER=testUser with POSTGRES_USER=postgres in the compose configuration. Also use the password defined in POSTGRES_PASSWORD. Delete the old container and create a new one.
Thank you all for your help on this.
It turns out the issue was that I was running postgres on my local machine as well.
so once I turn that off I was able to connect.
I appreciate your time!

Docker runs PostgreSQL in "trust" mode

I am currently learning docker and trying to run a docker container with the PostgreSQL database. I managed that once, and everything seemed to work fine. After some time, I tried to run another docker container with almost identical settings, however, it didn't go as expected. My problem is that now, whenever I try to run PostgreSQL container, initdb initializes the database in "trust" mode and accepts any connections without the password.
So far, I've tried running the command from the console:
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 32000:5432 -d postgres:14.5-alpine
As well as running the docker-compose.yaml:
services:
db:
container_name: Test_container
image: postgres:14.5-alpine
restart: unless-stopped
ports:
- "32000:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: mysecretpassword
Additionally, I tried ordering tags differently, different images, and different values, cleaning docker: removing all containers, images, and volumes, and even reinstalling docker, however, whenever I inspect logs of a newly created container, I get:
sh: locale: not found
2022-08-16 09:35:50.709 UTC [30] WARNING: no usable system locales were found
performing post-bootstrap initialization ... ok
initdb: warning: enabling "trust" authentication for local connections
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.
syncing data to disk ... ok
One of my assumptions was that docker, for some reason, doesn't see the password I am specifying and thus starts the database in "trust" mode, however, if I add
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test_db
to the docker-compose.yaml, test_db database is being created.
I'd appreciate any suggestions on how to make docker run PostgreSQL containers not in a "trust" mode as it should by default if the password is specified.
Juan González pointed out:
Note 1: The PostgreSQL image sets up trust authentication locally so you may notice a password is not required when connecting from localhost (inside the same container). However, a password will be required if connecting from a different host/container.
So, according to the docs, I updated my docker-compose.yaml file:
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test_db
POSTGRES_HOST_AUTH_METHOD: scram-sha-256
POSTGRES_INITDB_ARGS: --auth-host=scram-sha-256
and once again tried swapping order and\or removing POSTGRES_INITDB_ARGS, but database still runs in "trust" mode.
As stated in Postgres' DockerHub documentation:
Note 1: The PostgreSQL image sets up trust authentication locally so you may notice a password is not required when connecting from localhost (inside the same container). However, a password will be required if connecting from a different host/container.
However, if you don't want trust mode even in local connections, you can set the POSTGRES_HOST_AUTH_METHOD environment variable to override this behavior. More info at the documentation mentioned above.
As #jjanes pointed out in the comment to my question, the solution is to add POSTGRES_INITDB_ARGS: --auth=scram-sha-256 which would set both local and host types of connections.

How to connect to a Postgres database running in local Docker container through locally-run psql command?

I'm running a docker container with the vanilla Postgres image on my local machine. I'd like to connect to the database from my local machine (i.e., not from "within the container". However, on trying to connect, I get an error.
Here's my docker-compose.yml file:
version: "3.8"
services:
db:
image: postgres
restart: always
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: mypassword
Here's how I start up:
docker-compose run db
Here's how I connect:
psql -h localhost -p 5432 -U postgres
This produces the error:
could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?
If I spin up the database without Docker Compose, the same connection command works as expected:
docker run --name mypg -p 5432:5432 -e POSTGRES_PASSWORD=password postgres
I could just go with the flow and use the command above. But this seems to be pointing to a flaw in how I think about Docker/Compose. For example, maybe Docker Compose's internal DNS resolver makes this approach fail.
Any ideas?
Version info:
psql --version
psql (PostgreSQL) 13.3
I have read through several SO posts, including these, but they don't address or fix the problem I'm seeing:
docker-compose: accessing postgres' shell (psql)
Can't connect to postgres when using docker-compose
Try docker-compose up db instead of run. Using run will run a one-off command against your container, whereas up will turn on the container and leave it running, so another application should be able to access it.
https://docs.docker.com/compose/faq/#whats-the-difference-between-up-run-and-start

Using Docker: sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: password authentication failed for user "username"

Error:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: password authentication failed for user "username"
Hello, I am trying to run a program locally using Docker and am getting the error in the title, even though it was working before.
I've tried reinstalling Docker, re-cloning the repo, reinstalling PostgresSQL (the problem started when I installed it for the first time). From reading similar questions, I ensured that the password matches. The password is 'password' for the Docker Postgres Database and I've tried changing it but it still hasn't worked.
I'm using 'docker-compose up -d' and then running tests but I get the error in the title. I've tried running 'docker-compose down' and then redoing it, but I still get the error.
.env file:
FLASK_ENV=development
REDIS_URL=redis://localhost:6379
DATABASE_URL=postgresql+psycopg2://username:password#localhost:5432/programname
PROGRAM_API_APP_NAME=test-prod.compute.random.com
docker-compose.yml:
version: '3'
services:
postgresql:
image: postgres:10-alpine
environment:
- POSTGRES_DB=programname
- POSTGRES_PASSWORD=password
- POSTGRES_USER=username
ports:
- "5432"
redis:
image: redis:5.0.3
ports:
- "6379:6379"
(I don't have the port as 5432:5432 because it didn't work with that and I found an answer to remove the second 5432.)
Boss helped me with this. Apparently, when I installed Postgres, it created a postgres user that had a process that was using port 5432 and even when we killed it, it automatically restarted. To solve this specific problem, we changed the docker-compose file to use port 5433 locally and 5432 in the container. Still have to find out how to get rid of the postgres user.