Is the password of postgresql installed with docker automatically changed? - postgresql

I created the postgresql docker with the command below
docker run -d -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=1234 -v pgdata:/var/lib/postgresql/data postgres
But after some time, the following error occurs
[575] FATAL: password authentication failed for user "postgres"
[575] DETAIL: Password does not match for user "postgres".
Connection matched pg_hba.conf line 95: "host all all all md5"
Run the command below and it will work again
docker exec postgres su - postgres -c "psql -U postgres -d postgres -c \"alter user postgres with password '1234';\""
Can you tell me why this is happening?

Related

cannot authenticate with postgres

I'm trying to connect to running postgres container with psql:
docker pull postgres
docker run -e POSTGRES_PASSWORD=password -d postgres
C:\Program Files\PostgreSQL\13\bin>psql <myUserName>
Password for user <myUserName>:
at this point I type the given password, in this case just password and get the error
psql: error: FATAL: password authentication failed for user "<myUserName>"
What am I doing incorrectly ?
When you use docker run -e POSTGRES_PASSWORD=password -d postgres, the POSTGRES_PASSWORD would be set for user postgres as default. you can specify your user with POSTGRES_USER environment.
Second thing is that when you run a postgresql container and don't bind any ports for that, you can't connect to that container from outside. So you won't being able to connect to your container with pure pqsl command. Here you have 3 way to connect to your container:
1- Use docker run -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres to run container, then connect to it with psql -h <YOUR_IP> -p 5432 -U <USERNAME>
2- Get your container ip with docker inspect <CONTAINER_NAME> command, then connect to it with psql -h <CONTAINER_IP> -U <USERNAME>
3- Use psql inside your container with docker exec -it <CONTAINER_NAME> psql -U <USERNAME>

Cannot connect to postgres running in Docker

I am on a Mac.
I pulled the postgres docker image by running:
docker pull postgres:12.4
Then I start the container by running:
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=pass -d -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data postgres
I can confirm the container is running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ef29362b6f3 postgres "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:5432->5432/tcp pg-docker
I installed only psql on the host and when I try to connect to the postgres, I get the following error:
psql -h localhost -U postgres -d postgres
psql: error: could not connect to server: FATAL: password authentication failed for user "postgres"
password retrieved from file "/Users/me/.pgpass"
I am actually following the steps as outline in this post
I am not sure what is going wrong or how to fix this. Any ideas?

why am I getting Postgres password authentication fails error?

I'm trying to learn and exercise with postgres.
I'm running on Ubuntu 16.04.
I installed Postgres docker and run it with the following command:
sudo docker run --rm --name pg-docker -e POSTGRES_PASSWORD=docker -d -p 5433:5433 -v $HOME/amitliron/UserA/docker/volumes/postgres:/var/lib/postgresql/data postgres
(I'm using 5433 port, becuse when trying to use 5432 port I'm getting the following error:
Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use, I think it's not need to be problem, but writing here everying I did)
I'm trying to check the postgres docker with the following command:
psql -h localhost -U postgres -d postgres
and entered the password: "docker" but I'm getting the following error:
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
FATAL: password authentication failed for user "postgres"
What am I missing ?
I needed to specify the port:
psql -h localhost -p 5433 -U postgres -d postgres

Docker Postgres - error while creating a database and user

I use the official Postgres image from the Docker Hub
docker pull postgres
I start my container in my machine on local:
docker run --name some-postgres -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -e POSTGRES_DB=test postgres
The container would have to create my test base with my user on port 542
I have this error when I want to connect on my db
$ sudo docker run -it postgres /bin/bash
root#ef4407c26a96:/# su postgres
postgres#ef4407c26a96:/$ psql
psql: error: could not connect to server: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
postgres#ef4407c26a96:/$
Please provide the database that you want to connect to. As you've provided POSTGRES_DB=test, use it when you wish to connect to psql:
psql -d test -U user
Also, I was able to connect with psql both as root and postgres user.

How to move postgres into docker container?

I usually attach to postgres by typing in the terminal:
psql -U user db
I want to move postgres into docker container:
docker run -v /var/lib/postgresql:/var/lib/postgresql -p 5432:5432 postgres
Then I run command and have an error:
psql -h localhost -p 5432 -U user
psql: FATAL: role "user" does not exist
I expected that -v /var/lib/postgresql:/var/lib/postgresql will make possible to reach db by user. But it doesn't happen. How to make it correctly?