password authentication failed for user “postgres” with docker
Steps:
-
- 1. run the instruction to create a docker container.
docker run --rm --name test-postgres -p 5432:5432 -e POSTGRES_PASSWORD=pw -d postgres
-
- 2. run the following command in node code to connect to the database:
import pg from 'pg'
const { Pool } = pg
pool = new Pool({
database: 'postgres',
user: 'postgres',
password: 'pw',
port: 5432
})
The following error is thrown:
error: password authentication failed for user “postgres”
Cause analysis
Because Postgres has been installed locally, Postgres is automatically started when the system is started by default. When connecting to the database, the locally installed Postgres service is preferentially connected, so the connection fails.
Solution:
Open Window Task Manager, under Services you will see a postgres service running. Right-click to turn off the postgres service, open the Service window and double-click on the postgres service to set the Startup Type to Manual.
then
Other troubleshooting methods
Open the Terminal of the container
- Enter the following two commands to see if there is a problem with local host permissions.
cd var/lib/postgresql/data
cat pg_hba.conf
- To see if the default user is postgres.
psql -U postgres -x -c "select * from current_user;"
- Check the password expiration date. rolvaliduntil no value means no expiration date.
psql -h 127.0.0.1 -U postgres -d postgres
SELECT * FROM pg_roles WHERE rolname='postgres';
- Try clearing docker’s columns and containers, and restarting docker