PostgreSQ 连接问题 FATAL: no pg_hba.conf entry for host
2017-05-22 16:43
483 查看
PostgreSQ数据库为了安全,它不会监听除本地以外的所有连接请求,当用户通过JDBC访问是,会报一些如下的异常:
org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host
要解决这个问题,只需要在PostgreSQL数据库的安装目录下找到/data/pg_hba.conf,找到“# IPv4 local connections:”
在其下加上请求连接的机器IP
host all all 127.0.0.1/32 md5
32是子网掩码的网段;md5是密码验证方法,可以改(见文件pg_hba.conf上的说明)...
Java连接数据库代码
[java]
view plain
copy
package org.zj.dbconn;
import java.sql.*;
public class DBConnPostgres {
public static final String DBDRIVER="org.postgresql.Driver";
public static final String DBURL="jdbc:postgresql://localhost:5432/user";
public static final String DBUSER="postgres";
public static final String DBPASS="zhangjin";
public static void main(String []args){
Connection con=null;
try {
Class.forName(DBDRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql="select * from t_user";
PreparedStatement ps= con.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1));
}
ps.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host
要解决这个问题,只需要在PostgreSQL数据库的安装目录下找到/data/pg_hba.conf,找到“# IPv4 local connections:”
在其下加上请求连接的机器IP
host all all 127.0.0.1/32 md5
32是子网掩码的网段;md5是密码验证方法,可以改(见文件pg_hba.conf上的说明)...
Java连接数据库代码
[java]
view plain
copy
package org.zj.dbconn;
import java.sql.*;
public class DBConnPostgres {
public static final String DBDRIVER="org.postgresql.Driver";
public static final String DBURL="jdbc:postgresql://localhost:5432/user";
public static final String DBUSER="postgres";
public static final String DBPASS="zhangjin";
public static void main(String []args){
Connection con=null;
try {
Class.forName(DBDRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql="select * from t_user";
PreparedStatement ps= con.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1));
}
ps.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
相关文章推荐
- PostgreSQ 连接问题 FATAL: no pg_hba.conf entry for host
- PostgreSQ 连接问题 FATAL: no pg_hba.conf entry for host
- PostgreSQ 连接问题 FATAL: no pg_hba.conf entry for host
- PostgreSQ 连接问题 FATAL: no pg_hba.conf entry for host
- PostgreSQ 连接问题 FATAL: no pg_hba.conf entry for host
- FATAL: no pg_hba.conf entry for host "::1" odoo10连接postgre navicate连接问题
- PostgreSQL 连接问题 FATAL: no pg_hba.conf entry for host
- PostgreSQL 连接问题 FATAL: no pg_hba.conf entry for host
- Navicat Premium远程连接GP(Greenplum)集群 解决FATAL:no pg_hba.conf entry for host "172.20.20.2"问题
- PostgreSQL 连接问题 FATAL: no pg_hba.conf entry for host
- Greenplum pg_hba.conf entry for host "192.168.2.111", user "gpadmin", database "template1", SSL off
- no sys_hba.conf entry for host "192.168.16.28"
- SQLSTATE[08006] [7] FATAL: no pg_hba.conf entry
- postgresql Connection to database failed: FATAL: no pg_hba.conf entry for host "10.129.195.161", us
- 程序链接PostgreSQL 时报错"no pg_hba.conf entry"
- PostgreSQL远程数据库连接 && PostgreSQL pg_hba.conf 文件简析
- (No manual entry for pthread_cond_init)等问题解决方案
- ArcMap 连接SDE 出错“Failed to connect to the specified server. Entry for SDE instance no found in services file.”
- RCP Hibernate Derby数据库连接问题 No suitable driver found for
- Linux下mysql用sqlyog连接报1042 can not get hostname for your address的问题