PostgreSQL database connection error: psql: error: FATAL: password authentication failed for user “postgres“

Insert image description here

1. Introduction to the environment

1.1 Introduction to the practical environment

The environment is as follows, use yum to install PostgreSQL

hostname IP address Operating system version PostgreSQL version
jeven 192.168.3.166 centos 7.6 13.10

1.2 Introduction to PostgreSQL

PostgreSQL (often referred to as Postgres) is an open source relational database management system that implements all mainstream functions based on the SQL language and supports advanced functions such as transaction processing, concurrency control, complex queries, foreign keys, triggers, and stored procedures. It is highly scalable, stable and secure, and is one of the preferred database systems for many large enterprise-level applications.

1.3 PostgreSQL features

  • Open source and free: PostgreSQL is an open source software, the source code can be used and modified for free, and it can also be used in commercial projects.
  • Highly scalable: PostgreSQL supports horizontal and vertical expansion and can easily handle massive data and high concurrent requests.
  • Multiple data type support: PostgreSQL supports many data types, including JSON, arrays, ranges, XML, UUIDs, and more.
  • Backup and recovery: PostgreSQL provides a variety of backup and recovery methods, including physical backup and logical backup.
  • Security: PostgreSQL provides many security mechanisms, such as SSL/TLS encryption, access control, authentication and authorization, etc.
  • Extensibility: PostgreSQL supports many extensions, such as spatial data support, full-text search, etc.

2. Error reporting scenario

When connecting to the PostgreSQL database remotely, the password verification error occurred.

[root@jeven ~]#  psql -h192.168.3.166  -Upostgres -W
Password:
psql: error: FATAL:  password authentication failed for user "postgres"

Insert image description here

3. Analyze the reasons

  • Analyze the following possible causes and investigate them one by one.

1. The password is forgotten and entered incorrectly;
2. The /var/lib/pgsql/13/data/postgresql.conf file is incorrectly configured; 3.
The /var/lib/pgsql/13/data/pg_hba.conf file is incorrectly configured;
4. Settings The database user password is incorrect.

4. Check related configurations

  • Check the /var/lib/pgsql/13/data/postgresql.conf file to see if the following contents are modified correctly.
listen_addresses = '*'		# what IP address(es) to listen on;
  • Check the /var/lib/pgsql/13/data/pg_hba.conf file to see if the following contents are modified correctly.
host    all            all      127.0.0.1/32      ident
host    all            all      0.0.0.0/0  md5
  • After checking that the above is correct, restart the service and test again.

5. Solutions to error reports in related scenarios

5.1 Forgot login password

When you forget your login password, log in to local PostgreSQL and reset the password.

su - postgres
psql -c " ALTER USER postgres WITH PASSWORD 'postgres';"

5.2 The password is incorrectly set or not set

The database password is set incorrectly or is not set. Just reset it after connecting to the local service. This error was caused by carelessly typing a letter in the password.

sudo -u postgres
psql -c " ALTER USER postgres WITH PASSWORD 'postgres';"

5.3 Configuration file error

When there is an error in the configuration file, try the following modifications.

  • Check the /var/lib/pgsql/13/data/postgresql.conf file to see if the following contents are modified correctly.
listen_addresses = '*'		# what IP address(es) to listen on;
  • Check the /var/lib/pgsql/13/data/pg_hba.conf file to see if the following contents are modified correctly.
host    all            all      127.0.0.1/32      ident
host    all            all      0.0.0.0/0  md5
  • After checking that the above is correct, restart the service and test again.

5.4 Set up password-free login

When there is a problem with local login, you can temporarily set up password-free login. After changing the password, modify the configuration file /var/lib/pgsql/13/data/pg_hba.conf to the original one.

  • Set up password-free login
vim /var/lib/pgsql/13/data/pg_hba.conf
host    all            all      127.0.0.1/32     trust
host    all            all      0.0.0.0/0  trust

5.5 Environmental issues

Clear the environment or change to a new environment and redeploy the PostgreSQL database.

6. Summary of PostgreSQL database connection error reports

  • Database connection configuration error: Check whether the connection parameters are correct, including database address, port number, user name, password, etc., and ensure that the connection parameters are consistent with the actual situation.

  • The database service has not been started: Check whether the database service has been started. If not, you need to start the database service manually.

  • Database permission issues: Make sure the connected user has permission to access the database.

  • Firewall or network issues: Make sure the firewall is not blocking database access and that the network connection is working.

  • Database configuration file error: Check whether the database configuration file is set correctly, such as the database listening address, etc.

  • Database version is incompatible: Check whether the connected client version is compatible with the database version.

Guess you like

Origin blog.csdn.net/jks212454/article/details/133952198