Psql ошибка важно пользователь postgres не прошел проверку подлинности peer

Solution of psql: FATAL: Peer authentication failed for user “postgres” (or any user) - psql-error-fix.md

Solution of psql: FATAL: Peer authentication failed for user “postgres” (or any user)

The connection failed because by default psql connects over UNIX sockets using peer authentication, that requires the current UNIX user to have the same user name as psql. So you will have to create the UNIX user postgres and then login as postgres or use sudo -u postgres psql database-name for accessing the database (and psql should not ask for a password).

If you cannot or do not want to create the UNIX user, like if you just want to connect to your database for ad hoc queries, forcing a socket connection using psql --host=localhost --dbname=database-name --username=postgres (as pointed out by @meyerson answer) will solve your immediate problem.

But if you intend to force password authentication over Unix sockets instead of the peer method, try changing the following pg_hba.conf* line:

from

# TYPE DATABASE USER ADDRESS METHOD
local  all      all          peer

to

# TYPE DATABASE USER ADDRESS METHOD
local  all      all          md5
  • peer means it will trust the identity (authenticity) of UNIX user. So not asking for a password.

  • md5 means it will always ask for a password, and validate it after hashing with MD5.

  • trust means it will never ask for a password, and always trust any connection.

You can, of course, also create more specific rules for a specific database or user, with some users having peer and others requiring passwords.

After changing pg_hba.conf you’ll need to restart PostgreSQL if it’s running. E.g. sudo service postgresql restart

Steps to change/create default postgres user’s password:
  1. trust connection by adding in pg_hba.conf file
  • local all postgres trust
  1. Restart postgresql service
  • sudo service postgresql restart
  1. psql -U postgres

  2. At the postgres=# prompt, change the user name postgres password:

  • ALTER USER postgres with password ‘new-password’;
  1. Revert the changes in pg_hba.conf file from trust to md5 and restart postgresql.
pg_hba.conf file location

The file pg_hba.conf will most likely be at /etc/postgresql/9.x/main/pg_hba.conf
To check location of pg_hba.conf connect to postgres db using psql then type SHOW hba_file; command.

After change pg_hba.conf file, you can execute SELECT pg_reload_conf(); or pg_ctl reload with superuser instead of restart postgresql service.

* Source

Помогите разобраться народ !!!
Установлен PostgreSQL 9.6
Это моя настройка в pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD
LOCAL   ALL             postgres                                md5
# "local" is for Unix domain socket connections only
LOCAL   ALL             ALL                                     md5
# IPv4 local connections:
host    ALL             ALL             127.0.0.1/32            md5

Задан пароль пользователю «postgres» в Ubuntu
sudo echo -e «PAROLnPAROLn» | passwd postgres;
Пароль пользователю «postgres» в базе был задан:
sudo -U postgres psql -c «ALTER USER postgres WITH ENCRYPTED PASSWORD ‘PAROL’;»
Перезапускаю service postgresql restart
В результате, в логах:

2017-03-09 14:55:43 MSK [11185-1] СООБЩЕНИЕ:  система БД была выключена: 2017-03-09 14:55:42 MSK
2017-03-09 14:55:43 MSK [11185-2] СООБЩЕНИЕ:  Защита от наложения мультитранзакций сейчас включена
2017-03-09 14:55:43 MSK [11184-1] СООБЩЕНИЕ:  система БД готова принимать подключения
2017-03-09 14:55:43 MSK [11189-1] СООБЩЕНИЕ:  процесс запуска автоочистки создан
2017-03-09 14:55:44 MSK [11191-1] [н/д]@[н/д] СООБЩЕНИЕ:  неполный стартовый пакет
2017-03-09 14:55:44 MSK [11194-1] postgres@postgres ВАЖНО:  пользователь "postgres" не прошёл проверку подлинности (по паролю)
2017-03-09 14:55:44 MSK [11194-2] postgres@postgres ПОДРОБНОСТИ:  Пароль не подходит для пользователя "postgres".
	Подключение соответствует строке 86 в pg_hba.conf: "local   all             postgres                                md5"
2017-03-09 14:55:45 MSK [11197-1] postgres@postgres ВАЖНО:  пользователь "postgres" не прошёл проверку подлинности (по паролю)
2017-03-09 14:55:45 MSK [11197-2] postgres@postgres ПОДРОБНОСТИ:  Пароль не подходит для пользователя "postgres".
	Подключение соответствует строке 86 в pg_hba.conf: "local   all             postgres                                md5"
2017-03-09 14:55:45 MSK [11200-1] postgres@postgres ВАЖНО:  пользователь "postgres" не прошёл проверку подлинности (по паролю)
2017-03-09 14:55:45 MSK [11200-2] postgres@postgres ПОДРОБНОСТИ:  Пароль не подходит для пользователя "postgres".
	Подключение соответствует строке 86 в pg_hba.conf: "local   all             postgres                                md5"
2017-03-09 14:55:46 MSK [11203-1] postgres@postgres ВАЖНО:  пользователь "postgres" не прошёл проверку подлинности (по паролю)
2017-03-09 14:55:46 MSK [11203-2] postgres@postgres ПОДРОБНОСТИ:  Пароль не подходит для пользователя "postgres".
	Подключение соответствует строке 86 в pg_hba.conf: "local   all             postgres                                md5"
2017-03-09 14:55:46 MSK [11206-1] postgres@postgres ВАЖНО:  пользователь "postgres" не прошёл проверку подлинности (по паролю)
2017-03-09 14:55:46 MSK [11206-2] postgres@postgres ПОДРОБНОСТИ:  Пароль не подходит для пользователя "postgres".
	Подключение соответствует строке 86 в pg_hba.conf: "local   all             postgres                                md5"
2017-03-09 14:55:47 MSK [11209-1] postgres@postgres ВАЖНО:  пользователь "postgres" не прошёл проверку подлинности (по паролю)
2017-03-09 14:55:47 MSK [11209-2] postgres@postgres ПОДРОБНОСТИ:  Пароль не подходит для пользователя "postgres".
	Подключение соответствует строке 86 в pg_hba.conf: "local   all             postgres                                md5"
2017-03-09 14:55:47 MSK [11212-1] postgres@postgres ВАЖНО:  пользователь "postgres" не прошёл проверку подлинности (по паролю)
2017-03-09 14:55:47 MSK [11212-2] postgres@postgres ПОДРОБНОСТИ:  Пароль не подходит для пользователя "postgres".
	Подключение соответствует строке 86 в pg_hba.conf: "local   all             postgres                                md5"
2017-03-09 14:55:48 MSK [11215-1] postgres@postgres ВАЖНО:  пользователь "postgres" не прошёл проверку подлинности (по паролю)
2017-03-09 14:55:48 MSK [11215-2] postgres@postgres ПОДРОБНОСТИ:  Пароль не подходит для пользователя "postgres".
	Подключение соответствует строке 86 в pg_hba.conf: "local   all             postgres                                md5"
2017-03-09 14:55:48 MSK [11218-1] postgres@postgres ВАЖНО:  пользователь "postgres" не прошёл проверку подлинности (по паролю)
2017-03-09 14:55:48 MSK [11218-2] postgres@postgres ПОДРОБНОСТИ:  Пароль не подходит для пользователя "postgres".
	Подключение соответствует строке 86 в pg_hba.conf: "local   all             postgres                                md5"
2017-03-09 14:55:49 MSK [11221-1] postgres@postgres ВАЖНО:  пользователь "postgres" не прошёл проверку подлинности (по паролю)
2017-03-09 14:55:49 MSK [11221-2] postgres@postgres ПОДРОБНОСТИ:  Пароль не подходит для пользователя "postgres".
	Подключение соответствует строке 86 в pg_hba.conf: "local   all             postgres                                md5"
2017-03-09 14:55:49 MSK [11224-1] postgres@postgres ВАЖНО:  пользователь "postgres" не прошёл проверку подлинности (по паролю)
2017-03-09 14:55:49 MSK [11224-2] postgres@postgres ПОДРОБНОСТИ:  Пароль не подходит для пользователя "postgres".
	Подключение соответствует строке 86 в pg_hba.conf: "local   all             postgres                                md5"
2017-03-09 14:55:49 MSK [11184-2] СООБЩЕНИЕ:  получен запрос на "вежливое" выключение
2017-03-09 14:55:49 MSK [11189-2] СООБЩЕНИЕ:  процесс запуска автоочистки завершается
2017-03-09 14:55:49 MSK [11186-1] СООБЩЕНИЕ:  выключение
2017-03-09 14:55:49 MSK [11184-3] СООБЩЕНИЕ:  система БД выключена

When I connect to my local PostgreSQL database server with command $ psql postgres 'postgres' I encounter the error message psql: FATAL: Peer authentication failed for user “postgres”.

:~$ psql postgres 'postgres'
psql: FATAL:  Peer authentication failed for user "postgres"

After investigating I finally find the solution to fix this error. This error is because PostgreSQL has two authenticate methods as below.

1. PostgreSQL Authentication Methods.

  1. Peer Authentication (peer) Mode: This authentication method will use the base operating system’s user name and password as the PostgreSQL database server user account to login, this method is only effective for local PostgreSQL connections.
  2. Password Authentication (md5) Mode: This authentication method will need the login user to provide a username and password, and this method is effective for both local and remote PostgreSQL database server connections. The password can be saved both in clear-text or md5-encrypted. But we recommend using md5-encrypted. This can make the user account more safety.
  3. Trust (trust) Mode: This means the PostgreSQL DB server trust any connection, no password for any connection.

2. How To Change Postgresql Authentication Method.

PostgreSQL authentication method is saved in a configuration file, the file name is pg_hba.conf. So you can change the authentication method follow the below steps.

  1. Locate the pg_hba.conf configuration file uses Linux locate command.
    :~$ locate pg_hba.conf
    /etc/postgresql/10/main/pg_hba.conf
    /usr/share/postgresql/10/pg_hba.conf.sample
    
  2. From the above result, we can see that the pg_hba.conf file is generally saved in /etc/postgresql/10/main/ directory.
  3. Now edit the file using your favorite text editor, I use gedit in ubuntu.
    $ sudo gedit /etc/postgresql/10/main/pg_hba.conf 
    [sudo] password for zhaosong: 
    
    (gedit:317): IBUS-WARNING **: 11:34:40.540: The owner of /home/zhaosong/.config/ibus/bus is not root!
    
  4. Comment database administrative login text line which ends with peer and adds a new line use md5 to replace peerDo not forget to save the changes.
    #local   all             postgres                                peer
    local    all             postgres                                md5
    
  5. Reload the PostgreSQL service with the below command.
    ~$ /etc/init.d/postgresql reload
    [ ok ] Reloading postgresql configuration (via systemctl): postgresql.service.
    
  6. Or restart the PostgreSQL server.
    $ sudo service postgresql restart
  7. Now login PostgreSQL server again, you may find another error message psql: error: FATAL: password authentication failed for user “postgres” like below.
    $ psql postgres postgres
    Password for user postgres: 
    psql: error: FATAL:  password authentication failed for user "postgres"
    
  8. Change the line local all postgres md5 to local all postgres trust<.span> in the file /etc/postgresql/10/main/pg_hba.conf.
    #local   all             postgres                                peer
    #local    all             postgres                                md5
    local    all             postgres                                trust
  9. Now login PostgreSQL server again with the command psql user=postgres or psql postgres postgres, the error will be fixed.
    ~$ psql user=postgres
    psql (10.7 (Ubuntu 10.7-0ubuntu0.18.04.1))
    Type "help" for help.
    
    postgres=# 
    
  10. Below is the explanation of the settings text line like host   all   all   127.0.0.1/32   trust in the pg_hba.conf file. When you meet a login authentication error, you can change the METHOD column value to trust, this method will not require any password and trust any connection. But for the production environment, you had better use the method peer or md5 for safety issues.
    # TYPE  DATABASE        USER            ADDRESS                 METHOD
    
    # "local" is for Unix domain socket connections only
    local   all             all                                     trust
    # IPv4 local connections:
    host    all             all             127.0.0.1/32            trust

3. How To Fix Psql: Error: Fatal: Password Authentication Failed For User “postgres”.

3.1 Question.

  1. When I connect to the PostgreSQL in my python source code, I get the below error.
    FATAL: Peer authentication failed for user "postgres"
  2. I have edited the pg_hba.conf file and change the authentication method to md5, but it does not take effect.
  3. Then I try to create a new user with a new database, but when I run the command sudo -u postgres psql -l to list the databases, it returns the below error. How to fix it.
    psql: error: FATAL: password authentication failed for user "postgres"

3.2 Answer1.

  1. If this is your test environment, you can change the authentication method to trust like below, then it will not require you to provide the password to connect to the PostgreSQL DB server.
    local   all             postgres                                trust
  2. On Ubuntu, you can run the command sudo gedit /etc/postgresql/12/main/pg_hba.conf to open the PostgreSQL configuration file to edit it and save it.
  3. Run the command sudo systemctl restart postgresql.service to reload the above settings to make it take effect.
  4. Now when you run the command sudo -u postgres psql -l, you only need to provide the OS logged in user’s password, it will not ask you to provides the user postgres‘s password, and then it will run the command psql -l to show the PostgreSQL database tables list.
    $ sudo -u postgres psql -l
    [sudo] password for jerry: 
                                      List of databases
       Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
    -----------+----------+----------+-------------+-------------+-----------------------
     postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
     template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
               |          |          |             |             | postgres=CTc/postgres
     template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
               |          |          |             |             | postgres=CTc/postgres
    (3 rows)

Понравилась статья? Поделить с друзьями:
  • Psql как изменить пароль пользователя
  • Provider named pipes provider error 40 ошибка
  • Ps4 ошибка ce 34788 0 как исправить
  • Ps4 ошибка ce 33937 5
  • Ps4 ошибка ce 32753 0