-->

主机没有进入pg_hba.conf里(no pg_hba.conf entry for host)

2019-07-17 15:29发布

当我尝试使用DBI连接我得到以下错误

DBI connect('database=chaosLRdb;host=192.168.0.1;port=5433','postgres',...) 
failed: FATAL:  no pg_hba.conf entry for host "192.168.0.1", user "postgres", database "chaosLRdb", SSL off

这里是我的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");

我可以知道我在这里错过?

Answer 1:

在你的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密码,这可能,如果你修剪线工作。 为了让您可以使用Perl MD5或下面的shell脚本:

 echo -n 'chaos123' | md5sum
 > d6766c33ba6cf0bb249b37151b068f10  -

所以,那么你的连接线想是这样的:

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

欲了解更多信息,这里的Postgres的8.X的pg_hba.conf文件的文档 。



Answer 2:

如果你可以改变这一行:

host    all        all         192.168.0.1/32        md5

有了这个:

host    all        all         all                   md5

你可以看到,如果这个问题解决了。

但另一个考虑因素是你的PostgreSQL端口(5432)是黑客密码攻击非常开放的(也许他们可以暴力破解密码)。 你可以改变你的PostgreSQL端口5432为“33333”或其他价值,这样他们就可以不知道这个配置。



Answer 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"); 



Answer 4:

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

首先你得找出你的pg_hba.conf和写:

local all all md5

即重启PG服务器后:

postgresql restart

要么

sudo /etc/init.d/postgresql restart


Answer 5:

对于那些谁拥有类似的问题试图连接到本地数据库,并试图像
con = psycopg2.connect(database="my_db", user="my_name", password="admin")试图通过额外的参数,所以下面救了我的一天:
con = psycopg2.connect(database="my_db", user="my_name", password="admin", host="localhost")



Answer 6:

对于那些谁收到此错误DBeaver发现该溶液这里在行:

@lcustodio的SSL页面上,设置SSL模式:需要,要么离开SSL厂空白或使用org.postgresql.ssl.NonValidatingFactory

在网络 - > SSL选项卡我选中使用SLL复选框,设置高级 - > SSL模式=需要和它现在的作品。



Answer 7:

还要检查PGHOST变量:

ECHO $ PGHOST

看它是否本地机器名相匹配



Answer 8:

如果您收到类似下面的错误:

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


Answer 9:

顺便说一句,在我的情况是,我需要在URL中指定的用户名/密码,而不是作为独立的属性,它们被忽略了,我的OS用户用来连接

我的配置是在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"/>


文章来源: no pg_hba.conf entry for host