无法加载pg_hba.conf

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法加载pg_hba.conf相关的知识,希望对你有一定的参考价值。

参考技术A 1、首先是网络卡顿原因。
2、其次是电脑系统出现问题。
3、最后是网址有问题,导致进不去。

主机没有 pg_hba.conf 条目

【中文标题】主机没有 pg_hba.conf 条目【英文标题】:no pg_hba.conf entry for host 【发布时间】:2010-11-27 05:18:55 【问题描述】:

当我尝试使用 DBI 连接时出现以下错误

DBI connect('database=chaosLRdb;host=192.168.0.1;port=5433','postgres',...) 失败:致命:主机“192.168.0.1”、用户“postgres”、数据库“chaosLRdb”、SSL 关闭没有 pg_hba.conf 条目

这是我的 pg_hba.conf 文件:

# "local" is for Unix domain socket connections only
local   all         all                               md5
# IPv4 local connections:
host    all         all         127.0.0.1/32          md5
# IPv6 local connections:
host    all         all         ::1/128               md5

host    all         postgres    127.0.0.1/32          trust

host    all        postgres     192.168.0.1/32        trust

host    all        all         192.168.0.1/32        trust

host    all        all         192.168.0.1/128        trust

host    all        all         192.168.0.1/32        md5

host    chaosLRdb    postgres         192.168.0.1/32      md5
local    all        all         192.168.0.1/32        trust

我的 perl 代码是

#!/usr/bin/perl-w
use DBI;
use FileHandle;

print "Start connecting to the DB...\n";

@ary = DBI->available_drivers(true);
%drivers = DBI->installed_drivers();
my $dbh = DBI->connect("DBI:PgPP:database=chaosLRdb;host=192.168.0.1;port=5433", "postgres", "chaos123");

我可以知道我在这里想念什么吗?

【问题讨论】:

【参考方案1】:

如果您可以更改此行:

host    all        all         192.168.0.1/32        md5

有了这个:

host    all        all         all                   md5

你可以看看这是否解决了问题。

但另一个考虑因素是您的 postgresql 端口(5432)非常容易受到黑客的密码攻击(也许他们可以暴力破解密码)。您可以将您的 postgresql 端口 5432 更改为 '33333' 或其他值,因此他们无法知道此配置。

【讨论】:

我试着像 "host all all * md5" 那样做。但它没有奏效。但我按照你的方式成功了。非常感谢。 这个解决方案让我意识到我根本看不懂第四个参数,谢谢!【参考方案2】:

在您的 pg_hba.conf 文件中,我看到一些不正确且令人困惑的行:

# fine, this allows all dbs, all users, to be trusted from 192.168.0.1/32
# not recommend because of the lax permissions
host    all        all         192.168.0.1/32        trust

# wrong, /128 is an invalid netmask for ipv4, this line should be removed
host    all        all         192.168.0.1/128       trust

# this conflicts with the first line
# it says that that the password should be md5 and not plaintext
# I think the first line should be removed
host    all        all         192.168.0.1/32        md5

# this is fine except is it unnecessary because of the previous line
# which allows any user and any database to connect with md5 password
host    chaosLRdb  postgres    192.168.0.1/32        md5

# wrong, on local lines, an IP cannot be specified
# remove the 4th column
local   all        all         192.168.0.1/32        trust

我怀疑如果你 md5'd 密码,如果你修剪线条,这可能会起作用。要获取 md5,您可以使用 perl 或以下 shell 脚本:

 echo -n 'chaos123' | md5sum
 > d6766c33ba6cf0bb249b37151b068f10  -

那么你的连接线会像这样:

my $dbh = DBI->connect("DBI:PgPP:database=chaosLRdb;host=192.168.0.1;port=5433",
    "chaosuser", "d6766c33ba6cf0bb249b37151b068f10");

如需了解更多信息,请联系documentation of postgres 8.X's pg_hba.conf file。

【讨论】:

是的,它可能因为错误而拒绝加载 pg_hba.conf 文件。【参考方案3】:

您的 postgres 服务器配置似乎正确

host    all         all         127.0.0.1/32          md5
host    all         all         192.168.0.1/32        trust
这应该授予客户端对 postgres 服务器的访问权限。所以这让我相信用户名/密码是失败的。

通过为该数据库创建特定用户来测试它

createuser -a -d -W -U postgres chaosuser

然后调整您的 perl 脚本以使用新创建的用户

my $dbh = DBI->connect("DBI:PgPP:database=chaosLRdb;host=192.168.0.1;port=5433", "chaosuser", "chaos123");

【讨论】:

他得到一个关于缺少 pg_hba.conf 行的错误的事实意味着它还没有检查密码。【参考方案4】:

要解决这个问题,你可以试试这个。

首先,您通过以下方式找到了您的 pg_hba.conf:

从你的根目录 cd /etc/postgresql/9.5/main

并使用

打开文件
sudo nano pg_hba.conf

然后添加这一行:

local   all         all                               md5

到您的 pg_hba.conf 然后使用以下命令重新启动:

sudo service postgresql restart

【讨论】:

Hello 'local' 与 unix 套接字连接有关。为什么你认为它应该有助于解决网络问题? (我的 pg_hba.conf 中也有这一行,也有同样的问题,而且我的 postgresql 服务器不监听 unix 套接字。)【参考方案5】:

要解决这个问题,你可以试试这个。

首先你找到你的 pg_hba.conf 并写:

local all all md5

之后重启pg服务器:

postgresql restart

sudo /etc/init.d/postgresql restart

【讨论】:

【参考方案6】:

在 pg_hba.conf 中添加以下内容

hostnossl all all 0.0.0.0/0 trust

然后重启服务。

【讨论】:

【参考方案7】:

如果您遇到如下错误:

OperationalError: FATAL:  no pg_hba.conf entry for host "your ipv6",
                  user "username", database "postgres", SSL off

然后使用您的 mac 地址添加如下所示的条目。

host   all    all       [your ipv6]/128         md5

【讨论】:

我的问题与IPV6地址有关,因此,专门添加我的IPV6地址解决了问题。谢谢 Shiddu【参考方案8】:

对于那些在 DBeaver 中遇到此错误的人,可以在以下行找到解决方案 here:

在 SSL 页面上@lcustodio,设置 SSL 模式:要求并将 SSL 工厂留空或使用 org.postgresql.ssl.NonValidatingFactory

在网络 -> SSL 选项卡下,我选中了使用 SLL 复选框并设置了高级 -> SSL 模式 = 要求,它现在可以工作了。

【讨论】:

【参考方案9】:

对于那些尝试连接到本地数据库并尝试类似问题的人con = psycopg2.connect(database="my_db", user="my_name", password="admin"),请尝试传递附加参数,因此以下为我节省了一天:con = psycopg2.connect(database="my_db", user="my_name", password="admin", host="localhost")

【讨论】:

使用“本地”也拯救了我的一天!完整的主机名可以远程工作,但不能在本地工作。【参考方案10】:

如果您在使用 node 和 pg 模块时遇到此错误,您可以将 ssl 设置为不拒绝此类未经授权的访问

const pool = new Pool(
    connectionString: "your connection string",
    ssl: 
        rejectUnauthorized: false
    
)

【讨论】:

【参考方案11】:

还要检查 PGHOST 变量:

回声 $PGHOST

查看是否与本地机器名匹配

【讨论】:

【参考方案12】:

顺便说一句,在我的情况下,我需要在 url 中指定用户/密码,而不是作为独立属性,它们被忽略并且我的操作系统用户用于连接

我的配置在 WebSphere 8.5.5 server.xml 文件中

<dataSource 
    jndiName="jdbc/tableauPostgreSQL" 
    type="javax.sql.ConnectionPoolDataSource">
    <jdbcDriver 
        javax.sql.ConnectionPoolDataSource="org.postgresql.ds.PGConnectionPoolDataSource" 
        javax.sql.DataSource="org.postgresql.ds.PGPoolingDataSource" 
        libraryRef="PostgreSqlJdbcLib"/>
    <properties 
        url="jdbc:postgresql://server:port/mydb?user=fred&amp;password=secret"/>
</dataSource>

这不起作用并出现错误:

<properties 
    user="fred"
    password="secret"
    url="jdbc:postgresql://server:port/mydb"/>

【讨论】:

【参考方案13】:

在 pgadmin 中验证 postgres 连接主机名/地址,并在您的连接参数中使用相同的内容。

DBI connect('database=chaosLRdb;host="保持提及的内容" ;port=5433','postgres',...)

【讨论】:

欢迎来到 Stack Overflow!用户从未提及它可以使用 pgAdmin 访问数据库。

以上是关于无法加载pg_hba.conf的主要内容,如果未能解决你的问题,请参考以下文章

在Windows 7上更改/重置postgresql用户密码

查找pg_hba.conf

pg_hba.conf 和 pg_ident.conf

PostgreSQL重新读取pg_hba.conf文件

PostgreSQL的pg_hba.conf文件讲解

Postgresql错误,“没有pg_hba.conf条目”