PostgreSQL-用户“ postgres”的对等身份验证失败

时间:2020-08-10 16:31:10

标签: python postgresql server ubuntu-20.04 asyncpg

我有一个在Windows机器上运行的机器人,最近我购买了Ubuntu虚拟服务器。在发生一系列类似的无限错误之后,我只是完全重置了服务器并从头开始重试。您可以在这里查看我以前的一些错误的另一篇文章:no pg_hba.conf entry for host / Connect call failed / Invalid data directory for cluster 12 main - Postgresql Ubuntu

所以现在当我尝试启动我的机器人时,它给了我这个错误:

Traceback (most recent call last):
  File "bot.py", line 950, in <module>
    bot.loop.run_until_complete(create_db_pool())
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "bot.py", line 160, in create_db_pool
    bot.pg_con = await asyncpg.create_pool(database='xxx', user='postgres', password='???')
  File "/usr/local/lib/python3.8/dist-packages/asyncpg/pool.py", line 398, in _async__init__
    await self._initialize()
  File "/usr/local/lib/python3.8/dist-packages/asyncpg/pool.py", line 426, in _initialize
    await first_ch.connect()
  File "/usr/local/lib/python3.8/dist-packages/asyncpg/pool.py", line 125, in connect
    self._con = await self._pool._get_new_connection()
  File "/usr/local/lib/python3.8/dist-packages/asyncpg/pool.py", line 468, in _get_new_connection
    con = await connection.connect(
  File "/usr/local/lib/python3.8/dist-packages/asyncpg/connection.py", line 1718, in connect
    return await connect_utils._connect(
  File "/usr/local/lib/python3.8/dist-packages/asyncpg/connect_utils.py", line 663, in _connect
    con = await _connect_addr(
  File "/usr/local/lib/python3.8/dist-packages/asyncpg/connect_utils.py", line 642, in _connect_addr
    await asyncio.wait_for(connected, timeout=timeout)
  File "/usr/lib/python3.8/asyncio/tasks.py", line 483, in wait_for
    return fut.result()
asyncpg.exceptions.InvalidAuthorizationSpecificationError: Peer authentication failed for user "postgres"

数据库,用户和密码都正确。即使该bot在我的Windows计算机上运行,​​它现在也可以正常工作,这就是为什么我认为这是Ubuntu特有的问题。我的收听地址设置为“

listen_addresses = '*'

本地Windows服务器上的pg_hba.conf:

# TYPE  DATABASE        USER            ADDRESS            METHOD

# IPv4 local connections:
host    all             all             0.0.0.0/0            md5
# IPv6 local connections:
host    all             all             ::0/                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
host    replication     all             0.0.0.0/0            md5
host    replication     all             ::0/                 md5

,我在Ubuntu服务器上的pg_hba.conf设置为:

# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5

如果将第一行从peer更改为md5,则会得到:

asyncpg.exceptions.InvalidPasswordError: password authentication failed for user "postgres"

更改为trust表示没有数据库xxx,这是我的本地Windows计算机上的数据库。 键入ps ax | grep postgres显示:

   9929 ?        Ss     0:02 /usr/lib/postgresql/12/bin/postgres -D /var/lib/pos           tgresql/12/main -c config_file=/etc/postgresql/12/main/postgresql.conf
   9931 ?        Ss     0:00 postgres: 12/main: checkpointer
   9932 ?        Ss     0:01 postgres: 12/main: background writer
   9933 ?        Ss     0:01 postgres: 12/main: walwriter
   9934 ?        Ss     0:01 postgres: 12/main: autovacuum launcher
   9935 ?        Ss     0:01 postgres: 12/main: stats collector
   9936 ?        Ss     0:00 postgres: 12/main: logical replication launcher
  21821 pts/0    S+     0:00 grep --color=auto postgres

我已经坚持了很长时间,我也不知道该怎么办。我真的希望有人知道这个问题,希望我能提供足够的信息。谢谢。

我认为它试图连接到Ubuntu服务器上的数据库,而不是Windows计算机上的数据库。如何使其针对Windows计算机?

1 个答案:

答案 0 :(得分:3)

client.pg_con = await asyncpg.create_pool(database=database, user=user, password=password, host="127.0.0.1")

同时添加 host="127.0.0.1",这解决了我的问题。