FATAL: password authentication failed for user "postgres" using simple docker - postgresql

I was having issues connecting to my PG database in my compose file. I finally eliminated all other factors and made this bare-bones compose file:
version: '2.1'
services:
database:
image: postgres:9.5.6
environment:
POSTGRES_PASSWORD: secretpass
ports:
- 5432:5432
I then started it up with this command:
docker-compose --file docker-compose.pg.yml up --build
and it seemed to start ok:
Starting app_database_1 ...
Starting app_database_1 ... done
Attaching to app_database_1
database_1 | LOG: database system was shut down at 2017-09-22 18:48:55 UTC
database_1 | LOG: MultiXact member wraparound protections are now enabled
database_1 | LOG: database system is ready to accept connections
database_1 | LOG: autovacuum launcher started
Then from another shell, tried to connect with this command:
psql postgresql://postgres:secretpass#0.0.0.0
And got this output on my first shell where the compose was run:
database_1 | FATAL: password authentication failed for user "postgres"
database_1 | DETAIL: Connection matched pg_hba.conf line 95: "host all all 0.0.0.0/0 md5"
I have also tried to connect from another container running
Rails defined in the compose file and get the same error. I always end up with an authentication error. What am I missing?

Related

Unable to connect to postgres using docker compose

I have a very simple docker-compose.yml file which currently is only to set up a posgresql database, but I can't connect to it once it is running. Instead I get this error:
2022-10-10 11:19:41.820 UTC [43] FATAL: lock file "postmaster.pid" already exists
2022-10-10 11:19:41.820 UTC [43] HINT: Is another postmaster (PID 1) running in data directory "/var/lib/postgresql/data"?
The docker-compose.yml is:
version: "3.9"
services:
db:
image: "postgres"
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- '5432:5432'
volumes:
- /pgdata:/var/lib/postgresql/data
When I try to connect to the running container I use:
docker exec -it -u postgres 31f122d6a413 postgres
The logs from the container are:
Recreating test-docker-proj ...
WARNING: Service "db" is using volume "/var/lib/postgresql/data" from the previous container. Host mapping "/pgdata" has no effect.
Recreating test-docker-proj ... done
Attaching to test-docker-proj
db_1 |
db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1 |
db_1 | 2022-10-10 11:05:02.349 UTC [1] LOG: starting PostgreSQL 14.5 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 11.2.1_git20220219) 11.2.1 20220219, 64-bit
db_1 | 2022-10-10 11:05:02.349 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2022-10-10 11:05:02.349 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2022-10-10 11:05:02.352 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2022-10-10 11:05:02.356 UTC [22] LOG: database system was shut down at 2022-10-10 10:47:01 UTC
db_1 | 2022-10-10 11:05:02.361 UTC [1] LOG: database system is ready to accept connections
It seems that postgres/docker is persisting user login details from previous containers or something? I am at a bit of a loss anyway, so any guidance would be appreciated.
I am using:
Mac Monterey M1
docker-compose version 1.29.2
Probably worth adding that I can connect to the container using /bin/sh rather than postgres

Docker desktop (Windows 10) issue with Postgres docker-compose

On Docker Desktop (Windows 10), i'm unable to start postgres container with docker-compose
version: '3'
services:
postgres:
image: postgres:latest
environment:
- POSTGRES_PASSWORD='somepassword'
- POSTGRES_HOST_AUTH_METHOD=trust
Getting error that password is not set:
$ docker-compose up --build
Starting complex_postgres_1 ... done
Attaching to complex_postgres_1
postgres_1 | Error: Database is uninitialized and superuser password is not specified.
postgres_1 | You must specify POSTGRES_PASSWORD to a non-empty value for the
postgres_1 | superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
postgres_1 |
postgres_1 | You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
postgres_1 | connections without a password. This is *not* recommended.
postgres_1 |
postgres_1 | See PostgreSQL documentation about "trust":
postgres_1 | https://www.postgresql.org/docs/current/auth-trust.html
complex_postgres_1 exited with code 1
However, on Linux (CentOS 8) all works fine, even without last line - POSTGRES_HOST_AUTH_METHOD=trust
Has anyone experienced same issue on Windows 10 ?
Docker desktop is on latest version as well as docker-compose command
The single quote in your password is also going as a part of password
version: '3'
services:
postgres:
image: postgres:latest
environment:
- POSTGRES_PASSWORD=somepassword
- POSTGRES_HOST_AUTH_METHOD=trust

Password authentication failed for user when using new docker-compose

I am using django-cookiecutter template and for the second time for new project. And it fails to connect to postgres with following error:
postgres_1 | 2018-04-30 14:54:09.747 UTC [1] LOG: database system is ready to accept connections
postgres_1 | 2018-04-30 14:54:10.029 UTC [28] FATAL: password authentication failed for user "IViLGLIEWLBDGBnsAuoOEhtFaKrqKxfX"
postgres_1 | 2018-04-30 14:54:10.029 UTC [28] DETAIL: Role "IViLGLIEWLBDGBnsAuoOEhtFaKrqKxfX" does not exist.
postgres_1 | Connection matched pg_hba.conf line 95: "host all all all md5"
django_1 | PostgreSQL is unavailable (sleeping)...
One of the maintainers explained this:
the thing is, every time you bootstrap the project POSTGRES_USER and POSTGRES_PASSWORD get reset to a newly-generated random values
I tried to remove all docker containers but no success any idea how am i able to solve this? I dont have the old credentials to replace them.
With the help of https://github.com/webyneter (contributor to django-cookiecutter)
The solution is following:
To see existing volumes: docker volume ls
To remove the respective volumes docker volume rm <your project_slug>_postgres_backup_local <your project_slug>_postgres_data_local

How to install and run postgress using docker-compose.yml version "1"

1: docker-compose.yml
postgres96:
image: postgres:9.6
ports:
- "5432:5432"
volumes:
- ./Postgres/data:/var/lib/postgresql/data
env:
POSTGRES_PASSWORD: admin#123
POSTGRES_USER: postgres
2) $ docker-compose up &
postgres96_1 | LOG: database system was not properly shut down; automatic recovery in progress
postgres96_1 | LOG: invalid record length at 0/1570D50: wanted 24, got 0
postgres96_1 | LOG: redo is not required
postgres96_1 | LOG: MultiXact member wraparound protections are now enabled
postgres96_1 | LOG: database system is ready to accept connections
postgres96_1 | LOG: autovacuum launcher started
But while testing from pg-admin-iv on windows it show that 'user postgress has no password',
So is that flow from YML file is right, i want simply up the postgress and put data out side docker-container, so How to achieve this?
I would use named volumes for that instead of hosted mapped volumes:
postgres96:
image: postgres:9.6
ports:
- "5432:5432"
volumes:
- data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: admin#123
POSTGRES_USER: postgres
Probably you have a permission issue in your folder.
You data is saved in a volume outside the container. Then check with:
docker volume ls
Also use docker-compose as:
docker-compose up -d
Instead of &
And
docker-compose logs -f
For further information this is the last part of my log:
postgres96_1 | PostgreSQL init process complete; ready for start up.
postgres96_1 |
postgres96_1 | LOG: database system was shut down at 2016-11-14 21:05:51 UTC
postgres96_1 | LOG: MultiXact member wraparound protections are now enabled
postgres96_1 | LOG: database system is ready to accept connections
postgres96_1 | LOG: autovacuum launcher started
postgres96_1 | LOG: incomplete startup packet
Regards

docker alpine postgres in compose not executing docker-entrypoint-initdb.d scripts

When using the https://github.com/kiasaki/docker-alpine-postgres image in docker compose, the scripts inside /docker-entrypoint.initdb.d are not being executed as I view from the logs:
db_1 | LOG: database system was shut down at 2016-09-05 18:16:02 UTC
db_1 | LOG: MultiXact member wraparound protections are now enabled
db_1 | LOG: database system is ready to accept connections
db_1 | LOG: autovacuum launcher started
The thing is, if I build the Dockerfile standalone and run the container, it will execute the sql files inside the folder, like you can see from the logs:
waiting for server to start....LOG: database system was shut down at 2016-09-05 18:28:18 UTC
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
done
server started
/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/syna-setup.sql
CREATE TABLE
INSERT 0 1
CREATE TABLE
waiting for server to shut down...LOG: received fast shutdown request
.LOG: aborting any active transactions
LOG: autovacuum launcher shutting down
LOG: shutting down
LOG: database system is shut down
done
server stopped
LOG: database system was shut down at 2016-09-05 18:28:21 UTC
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
LOG: received fast shutdown request
LOG: aborting any active transactions
LOG: autovacuum launcher shutting down
LOG: shutting down
LOG: database system is shut down
The docker-compose.yml:
version: '2'
services:
db:
build: ./db
environment:
- POSTGRES_PASSWORD=testpass
restart: always
app:
build: ./app
ports:
- "5000:5000"
volumes:
- .:/app/src
depends_on:
- db
environment:
- DB_SERVER=postgres://postgres:testpass#db:5432/postgres
What am I doing wrong here?
UPDATED as he use his own Dockerfile, not from the github link.
Could you proide the docker run command so that if I build the Dockerfile standalone and run the container, it will execute the sql files inside the folder. You should mount some directory to the container so it can find the SQL files.
I read the Dockerfile at https://github.com/kiasaki/docker-alpine-postgres/blob/master/Dockerfile and asked myself where are the SQL files? It just make the directory without copy SQL files
mkdir /docker-entrypoint-initdb.d
Then, in the docker-entrypoint.sh, it check if there's any SQL file to execute
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; psql --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" < "$f" && echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
done
I think we should mount a SQL directory, like
version: '2'
services:
db:
build: ./db
environment:
- POSTGRES_PASSWORD=testpass
restart: always
entrypoint:
- docker-entrypoint.sh
volumes:
- ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
- ./docker-entrypoint.sh:/docker-entrypoint.sh