Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

psql: FATAL: password authentication failed for user "postgres" #112

Open
StephanMeijer opened this issue Dec 27, 2017 · 50 comments
Open

psql: FATAL: password authentication failed for user "postgres" #112

StephanMeijer opened this issue Dec 27, 2017 · 50 comments
Assignees

Comments

@StephanMeijer
Copy link

StephanMeijer commented Dec 27, 2017

$ sudo docker run --name postgresql -itd --restart always   --env 'PG_PASSWORD=postgres'   --env 'PG_TRUST_LOCALNET=true'   --publish 5432:5432   --volume /srv/docker/postgresql:/var/lib/postgresql   sameersbn/postgresql:9.6-2
59edeb353dec8c503a7f8d86396decfcdb31b98a3b463c3e9c990083fb4dc59e
$ psql -Upostgres -hlocalhost
Password for user postgres: 
psql: FATAL:  password authentication failed for user "postgres"
@zalper
Copy link

zalper commented Dec 3, 2018

v10, still reproduced. Solution is:

You need to define a password for user postgres

  1. Get container
    docker ps

  2. Enter inside of the container
    docker exec -it <hash> bash

  3. Start query console
    psql

  4. Define password
    ALTER ROLE postgres WITH PASSWORD 'your_password';

Then you may grant other users as superuser and etc. (Make your volume persist)

Container Mgmnt:

Up:
docker-compose up --remove-orphans --force-recreate --build PostgreSQL

Down:
docker-compose down -v

@AhmedBHameed
Copy link

AhmedBHameed commented Jan 16, 2019

@zalper how should i save your steps in the container!!
I did your steps and commit the container but still now saved when running my image again !!
BTW i'm NB to docker.

@eddex
Copy link

eddex commented Jan 29, 2020

I had a similar issue, probably not related to the one of @zalper but with the same error message so I'll leave the solution here for anyone having the same problems.

I'm using Windows and had ProstgreSQL 12 installed.

At the same time I tried to run a postgres:10 docker container.

When I tried to connect to the database running in the docker container using psql, I always got an error saying psql: FATAL: password authentication failed for user "postgres".

The issue was that the postgres docker container was using the default port 5432 on localhost and the PostgreSQL server on Windows was using the same port on localhost. However, there were no errors when starting either of them.

Solutions:

  • Option 1: Stop the PostgreSQL service on Windows
  • Option 2 (using WSL): Completely uninstall Protgres 12 from Windows and install postgresql-client on WSL (sudo apt install postgresql-client-common postgresql-client libpq-dev)
  • Option 3: Change the port of the docker container

@august-of-wind
Copy link

@eddex Just wanted to chime in to say your Option 1 above fixed this for me after I had been scratching my head for a couple of hours. That solution does not appear anywhere else online that I have been able to find and was extremely helpful. Thanks!

@TJBelcher
Copy link

@eddex Thanks! I also benefited from you recalling your problem and solution. I'd not seen it mentioned anywhere else in 2 days of troubleshooting. I had noticed double use of the port in some netstat results, but reasoned it away as being 2 references to the same process since I didn't know postgres 12 was running on my PC. Bad assumption on my part :)

@o-andres-u
Copy link

@eddex Thanks a lot. I was having the same issue with my local Postgres installation and the container I was running by using docker. I just stopped the service on Windows and started working.

@nikluwfy
Copy link

nikluwfy commented May 4, 2020

@eddex also saved the day for me here.

@Mallikanand
Copy link

@eddex - after a couple of hours of struggle, this answer helped me.
Didnt realise my Postgres locally starts automatically upon login.
As I didnt need the local version anymore, uninstalled it.

@sameersbn sameersbn self-assigned this May 25, 2020
@sameersbn
Copy link
Owner

/remind me about this on saturday

@reminders reminders bot added the reminder label May 25, 2020
@reminders
Copy link

reminders bot commented May 25, 2020

@sameersbn set a reminder for May 30th 2020

@reminders reminders bot removed the reminder label May 30, 2020
@reminders
Copy link

reminders bot commented May 30, 2020

👋 @sameersbn, about this

@khalilahmad0
Copy link

@eddex
Thanks man!

@STotev
Copy link

STotev commented Jul 28, 2020

I still got the issue and really don't know what changed.
I was doing some work on my home machine and everything was set up properly and was working. Then I moved working on my working machine, set it up there and it worked as well (and it still works there).

After moving to my working machine I removed the containers and images for postgres:12 from my home machine. Now I want to set the things up again but for some reason it refuses to log me. Haven't changed any docker-compose settings.
I am afraid that if I now remove everything from my working machine and try to set it up again, it'll stop working there too.

This is my docker-compose.yml file.

version: '3.1'

services:

    postgres:
        image: postgres:12
        container_name: "postgres-v12"
        ports:
            - 5432:5432
        environment:
            POSTGRES_PASSWORD: admin
        volumes:
            - db_vol:/var/lib/postgresql/data

    pgadmin:
        image: dpage/pgadmin4
        container_name: pgadmin4
        ports:
            - 3333:80
        environment:
            PGADMIN_DEFAULT_EMAIL: admin
            PGADMIN_DEFAULT_PASSWORD: admin


volumes:
    db_vol:
        external:
            name: "postgres-v12"

I don't have postgres installed directly on my machine, tried to change ports, passwords, adding networks, re-pulling images, restarting docker etc. and it just won't work.

Any ideas anyone?

@STotev
Copy link

STotev commented Jul 29, 2020

Today, I have decided to try and re-create the volume. So I deleted this postgres-v12 and created a new one and just in case I named it postgres12. I updated docker-compose.yml and voila everything started working again. I have never done any manual user updates, so somehow something messed up the postgres user in that volume.

@exaucae
Copy link

exaucae commented Dec 26, 2020

Thanks @eddex ! Solution 1 fixed my issue.

@Maxeeezy
Copy link

I had a similar issue, probably not related to the one of @zalper but with the same error message so I'll leave the solution here for anyone having the same problems.

I'm using Windows and had ProstgreSQL 12 installed.

At the same time I tried to run a postgres:10 docker container.

When I tried to connect to the database running in the docker container using psql, I always got an error saying psql: FATAL: password authentication failed for user "postgres".

The issue was that the postgres docker container was using the default port 5432 on localhost and the PostgreSQL server on Windows was using the same port on localhost. However, there were no errors when starting either of them.

Solutions:

  • Option 1: Stop the PostgreSQL service on Windows
  • Option 2 (using WSL): Completely uninstall Protgres 12 from Windows and install postgresql-client on WSL (sudo apt install postgresql-client-common postgresql-client libpq-dev)
  • Option 3: Change the port of the docker container

Do you maybe also know a solution for Mac OSX? I already spent hours on this bad connection error for this docker issue...

@Maxeeezy
Copy link

Maxeeezy commented Dec 30, 2020

In my case (dockerizing a rails app) I had to delete tmp/db folder and rerun docker-compose up for me to solve the issue.

@kamalhm
Copy link

kamalhm commented Mar 30, 2021

almost went crazy because I have tried even reinstalling docker to solve this issue, turns out it was because I forgot I installed a local postgresql. 🤪 thanks @eddex

@l-narasimhan
Copy link

Today, I have decided to try and re-create the volume. So I deleted this postgres-v12 and created a new one and just in case I named it postgres12. I updated docker-compose.yml and voila everything started working again. I have never done any manual user updates, so somehow something messed up the postgres user in that volume.

It worked for me as well

@nox-mthimkulu
Copy link

nox-mthimkulu commented Apr 30, 2021

I had the same issue with just running
docker run --name pg -p 5432:5432 -e POSTGRES_PASSWORD=postgres postgres and I eventually realised that the command was using the postgres docker image that I had locally, tagged as latest (rather than pulling the latest one from remote).
Run d
docker image ls

and see what images you currently have. Removing the postgres:latest image I had locally and re-running the command ended up with the latest image being pulled and the login working!

@JustifydWarrior
Copy link

JustifydWarrior commented Jun 24, 2021

this worked for me... i just forward and expose 5435 on my machine. leave pg on 5432 inside the container. Then it's easy to config 5435 or any other port for dev use.

services:
  database:
    image: "postgres" # use latest official postgres version
    env_file:
      - database.env # configure postgres
    volumes:
      - database-data:/var/lib/postgresql/data/ # persist data even if container shuts down
    ports:
      - 5435:5432
    expose:
      - "5435"

volumes:
  database-data: # named volumes can be managed easier using docker-compose

my database.env for reference

  PGUSER=postgres
  PGPASSWORD=postgres
  PGDATABASE=postgres
  POSTGRES_USER=postgres
  POSTGRES_PASSWORD=postgres
  POSTGRES_DB=postgres

yes it's been redacted. i am not actually forcing the default values. just wanted to clarify the file structure and proper envs as noted here: https://www.postgresql.org/docs/9.3/libpq-envars.html

in addition to the others the docker container uses.

there seems to be no reason for 2 different sets of envs, but i haven't thought through every use case.

so perhaps that's a possible fix here is using the core PG envs and then update container docs to match.

@Limatucano
Copy link

I had a similar issue, probably not related to the one of @zalper but with the same error message so I'll leave the solution here for anyone having the same problems.

I'm using Windows and had ProstgreSQL 12 installed.

At the same time I tried to run a postgres:10 docker container.

When I tried to connect to the database running in the docker container using psql, I always got an error saying psql: FATAL: password authentication failed for user "postgres".

The issue was that the postgres docker container was using the default port 5432 on localhost and the PostgreSQL server on Windows was using the same port on localhost. However, there were no errors when starting either of them.

Solutions:

  • Option 1: Stop the PostgreSQL service on Windows
  • Option 2 (using WSL): Completely uninstall Protgres 12 from Windows and install postgresql-client on WSL (sudo apt install postgresql-client-common postgresql-client libpq-dev)
  • Option 3: Change the port of the docker container

You are so beautiful, THANKSS

@Chicitadel
Copy link

I had a similar issue, probably not related to the one of @zalper but with the same error message so I'll leave the solution here for anyone having the same problems.

I'm using Windows and had ProstgreSQL 12 installed.

At the same time I tried to run a postgres:10 docker container.

When I tried to connect to the database running in the docker container using psql, I always got an error saying psql: FATAL: password authentication failed for user "postgres".

The issue was that the postgres docker container was using the default port 5432 on localhost and the PostgreSQL server on Windows was using the same port on localhost. However, there were no errors when starting either of them.

Solutions:

* Option 1: Stop the PostgreSQL service on Windows

* Option 2 (using WSL): Completely uninstall Protgres 12 from Windows and install postgresql-client on WSL (`sudo apt install postgresql-client-common postgresql-client libpq-dev`)

* Option 3: Change the port of the docker container

You saved me tons of painful 72hours of battle with innocent Intellij IDE and plugins

@bibhuticoder
Copy link

bibhuticoder commented Jan 24, 2022

I had a similar issue, probably not related to the one of @zalper but with the same error message so I'll leave the solution here for anyone having the same problems.

I'm using Windows and had ProstgreSQL 12 installed.

At the same time I tried to run a postgres:10 docker container.

When I tried to connect to the database running in the docker container using psql, I always got an error saying psql: FATAL: password authentication failed for user "postgres".

The issue was that the postgres docker container was using the default port 5432 on localhost and the PostgreSQL server on Windows was using the same port on localhost. However, there were no errors when starting either of them.

Solutions:

  • Option 1: Stop the PostgreSQL service on Windows
  • Option 2 (using WSL): Completely uninstall Protgres 12 from Windows and install postgresql-client on WSL (sudo apt install postgresql-client-common postgresql-client libpq-dev)
  • Option 3: Change the port of the docker container

Faced the same issue. Solved it by changing port mapping on docker-compose

services:
  db:
    image: postgres
    container_name: postgres
    restart: always
    environment:
      POSTGRES_PASSWORD: ***
      POSTGRES_USER: ***
      POSTGRES_DB: ***
    ports:
      - 5435:5432    ==> changed 5432 to 5435

@gustavorps
Copy link

Same issue

@jeremybradbury
Copy link

jeremybradbury commented Mar 8, 2022

@gustavorps I think the solution is an easy one to work around. Using @JustifydWarrior's fix above:

    ports:
      - 5435:5432
    expose:
      - "5435"

5433 and 5434 are also reserved for more common uses, however 5435 is not a very common protocol by definition & is most often used as a second postgres port.

@bibhuticoder came up with the same solution.

I think someone should submit a PR to fix this, even if it is a second docker compose file, simply called by using the filename flag.

Disabling the default service, is the simplest solution, but that shouldn't be the preferred workaround, many of us need both running.

@fahricankacan
Copy link

I had the same problem . My solution for windows :

  1. Stop docker container
  2. win + r and type services.msc
  3. Find postgressql and stop
  4. Start your container again

I solved my problem with this way .

@escote
Copy link

escote commented Apr 28, 2022

Envs are used by postgres initdb to create your postgresql instance. If you changed yours envs after first execution, you need to delete the postgres container to force a recreation using new envs. If you have defined a volume, you will also need to remove it.

@thalles-victor
Copy link

My english is not very well, but i try help you.

In your project run

 ls -a

if exist a folder called database-data (name of the your volume of postgres of the docker-compose.yml), remove this folder and run again

  docker-compose up -d --force-recreate

The my work this way.

Or
if it didn't work, try
Remove all volumes outside project with

  docker volume rm $(docker volume ls -q)

and run again
```console`
docker-compose up -d --force-recreate
``

@coding-x1
Copy link

Below is my postgres:14 in the docker-compose.yml

I am writing Go Program in the microservice manner.
One day before POSTGRES_PASSWORD: password
was the password. Next day I changed POSTGRES_PASSWORD: secret
Now the said error came.

The folder /db-data/postgres/ which I mentioned below has content populated by docker.
Deleted that content and again run docker. Now ok.

postgres:
image: 'postgres:14.0'
ports:
- "5432:5432"
restart: always
deploy:
mode: replicated
replicas: 1
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: secret
POSTGRES_DB: users
volumes:
- ./db-data/postgres/:/var/lib/postgresql/data/

@anildalar
Copy link

@eddex Thanks a lot. I was having the same issue with my local Postgres installation and the container I was running by using docker. I just stopped the service on Windows and started working.

THanks it works for me

@tommy4421
Copy link

A uppercase character in the username will also trigger this error message...

@GSpletty
Copy link

I had a similar issue, probably not related to the one of @zalper but with the same error message so I'll leave the solution here for anyone having the same problems.

I'm using Windows and had ProstgreSQL 12 installed.

At the same time I tried to run a postgres:10 docker container.

When I tried to connect to the database running in the docker container using psql, I always got an error saying psql: FATAL: password authentication failed for user "postgres".

The issue was that the postgres docker container was using the default port 5432 on localhost and the PostgreSQL server on Windows was using the same port on localhost. However, there were no errors when starting either of them.

Solutions:

  • Option 1: Stop the PostgreSQL service on Windows
  • Option 2 (using WSL): Completely uninstall Protgres 12 from Windows and install postgresql-client on WSL (sudo apt install postgresql-client-common postgresql-client libpq-dev)
  • Option 3: Change the port of the docker container

Stopping the postgres service and uninstalling from Windows was my exact issue

@0xmer1in
Copy link

Below is my postgres:14 in the docker-compose.yml

I am writing Go Program in the microservice manner. One day before POSTGRES_PASSWORD: password was the password. Next day I changed POSTGRES_PASSWORD: secret Now the said error came.

The folder /db-data/postgres/ which I mentioned below has content populated by docker. Deleted that content and again run docker. Now ok.

postgres: image: 'postgres:14.0' ports: - "5432:5432" restart: always deploy: mode: replicated replicas: 1 environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: secret POSTGRES_DB: users volumes: - ./db-data/postgres/:/var/lib/postgresql/data/

This work for me, thank you!

@Andrewsoares15
Copy link

Andrewsoares15 commented Aug 7, 2023

@eddex after two days of reading all of these forums, I finally found your solution, you saved my life hahaha Thanks my brotha!!

@hayyaun
Copy link

hayyaun commented Nov 2, 2023

Today, I have decided to try and re-create the volume. So I deleted this postgres-v12 and created a new one and just in case I named it postgres12. I updated docker-compose.yml and voila everything started working again. I have never done any manual user updates, so somehow something messed up the postgres user in that volume.

This answer save my time twice, once in my local, weeks later on my server. Thank you!

Kamilm55 added a commit to Kamilm55/Course-ERP that referenced this issue Nov 5, 2023
@aravazzibb
Copy link

For me, creating a new volume in docker-compose.yml by changing it in the volumes session and then running docker-compose up --remove-orphans --force-recreate --build postgres solved the problem

@Musilix
Copy link

Musilix commented Dec 13, 2023

I had the same problem . My solution for windows :

  1. Stop docker container
  2. win + r and type services.msc
  3. Find postgressql and stop
  4. Start your container again

I solved my problem with this way .

This worked for me, but why did it work? I'm scared and disoriented.

@omjogani
Copy link

omjogani commented Jan 3, 2024

I was having the same issue!

Here is how I solved it!

  • There was background service running for my local PostgreSql called postgres.exe. I stopped it. And again spin up docker image of postgres. and It just works!
  • Before that I was able to access the database with credential in adminer but not in my backend & Beekeeper Studio. once I stopped that service and try running docker compose It runs perfectly with adminer, backend code & Beekeeper Studio.

Hope it helps!

@quocthinhle
Copy link

I had the same problem . My solution for windows :

  1. Stop docker container
  2. win + r and type services.msc
  3. Find postgressql and stop
  4. Start your container again

I solved my problem with this way .

thanks, it worked!

@Shresth72
Copy link

I had a similar issue, probably not related to the one of @zalper but with the same error message so I'll leave the solution here for anyone having the same problems.

I'm using Windows and had ProstgreSQL 12 installed.

At the same time I tried to run a postgres:10 docker container.

When I tried to connect to the database running in the docker container using psql, I always got an error saying psql: FATAL: password authentication failed for user "postgres".

The issue was that the postgres docker container was using the default port 5432 on localhost and the PostgreSQL server on Windows was using the same port on localhost. However, there were no errors when starting either of them.

Solutions:

  • Option 1: Stop the PostgreSQL service on Windows
  • Option 2 (using WSL): Completely uninstall Protgres 12 from Windows and install postgresql-client on WSL (sudo apt install postgresql-client-common postgresql-client libpq-dev)
  • Option 3: Change the port of the docker container

Thnx so much, spent hours on a port error 😭

@xiayulu
Copy link

xiayulu commented Mar 31, 2024

In my case, the database password contains special symbols, e.g. $, @, after recreate a password without special symbol, it worked.

@AhmedBaset
Copy link

@eddex

It has been two days debugging this with success and your comment solved it to me.

Thanks

@prakhar-chandrakar
Copy link

Bro just run docker on another port like 5433, use -

$ sudo docker run --name postgresql -itd --restart always --env 'PG_PASSWORD=postgres' --env 'PG_TRUST_LOCALNET=true' --publish 5433:5432 ...

It worked for me :)

@Saad-Abbasi
Copy link

Changing the Port fixed the issue

docker run --name pg-con -e POSTGRES_PASSWORD=anypassTest -p 8080:5432 postgres

you will use 8080 in your connection instead of 5432

@reihane-rangamiz
Copy link

IN MY CASE, I had to check the container's ip address and sync it with the .env file.

  1. make sure the container us running --> docker ps it shows the running containers, if empty, run the project containers with docker compose up or similar commands based on the structure

  2. restart the compose file

IF DIDN'T WORK:
3) set password for the same user:
docker exec -it <postgres container name> sh. --> enter the shell and run the commands below:

# psql -U postgres
psql (16.3 (Debian 16.3-1.pgdg120+1))
Type "help" for help.
postgres=# \password
Enter new password for user "postgres":    --> enter a password here
Enter it again: 
postgres=# exit --> all set, run exit command to exit the container's shell
# exit

IF NOT SOLVED YET:

  1. Compare the details of your environment variables with the container configs.(port, host,...)
    run the command docker inspect <postgres container name>. --> now you can see the container config
    check the detail and fix any mismatches(in my case the container's ip address was on 172.18.0.2 but my POSTGRES_HOST was on 127.0.0.1 since I waited to run it locally.)

@AndyMender
Copy link

Since above solutions are of the sort "X worked for me", I did some testing and the issue is not with the way the Dockerfile for PostgreSQL is written, but with the container first being created with incorrect/empty credentials and then reused without changes.

If you're running into issues despite having a correct Docker or docker-compose setup

  1. Make sure the host's port you're exposing on is not used by something else
  2. Delete all Docker images related to your setup
  3. Run your setup

@OmHS8
Copy link

OmHS8 commented Jun 17, 2024

I had the same problem . My solution for windows :

  1. Stop docker container
  2. win + r and type services.msc
  3. Find postgressql and stop
  4. Start your container again

I solved my problem with this way .

This is the root cause for the problem if anyone wondering ..

@maik-emanoel
Copy link

I had a similar issue, probably not related to the one of @zalper but with the same error message so I'll leave the solution here for anyone having the same problems.

I'm using Windows and had ProstgreSQL 12 installed.

At the same time I tried to run a postgres:10 docker container.

When I tried to connect to the database running in the docker container using psql, I always got an error saying psql: FATAL: password authentication failed for user "postgres".

The issue was that the postgres docker container was using the default port 5432 on localhost and the PostgreSQL server on Windows was using the same port on localhost. However, there were no errors when starting either of them.

Solutions:

  • Option 1: Stop the PostgreSQL service on Windows
  • Option 2 (using WSL): Completely uninstall Protgres 12 from Windows and install postgresql-client on WSL (sudo apt install postgresql-client-common postgresql-client libpq-dev)
  • Option 3: Change the port of the docker container

Thanks man, helped a lot

@Divyashreeparvati
Copy link

This is my docker-compose.yml file
version: '3.8'

services:
catalog_db_server:
image: postgres:14
environment:
POSTGRES_USER: catalog_db
POSTGRES_PASSWORD: 123456
POSTGRES_DB: catalog_service
volumes:
- ./db-data/catalog_db_server/:/var/lib/postgresql/data
ports:
- "5433:5432"
deploy:
mode: replicated
replicas: 1

order_db_server:
image: postgres:14
environment:
POSTGRES_USER: order_db
POSTGRES_PASSWORD: 78910
POSTGRES_DB: order_service
volumes:
- ./db-data/order_db_server/:/var/lib/postgresql/data
ports:
- "5432:5432"
deploy:
mode: replicated
replicas: 1

catalog_db_server I am able to connect but order_db_server is throwing " FATAL: password authentication failed for user order_db". I tried stopping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests