java - 是否可以在没有密码的情况下创建 jdbc 连接(使用 postgresql 'trust' )?

标签 java postgresql jdbc

我正在使用 jdbc 连接到 java 应用程序中的 postgresql 数据库(实际上该应用程序是用 Groovy 编写的)。我已将 postgresql 设置为使用“信任”身份验证方法。是否可以在不指定密码的情况下打开 jdbc 连接?当我尝试使用带有空白密码的普通构造函数时,它失败并显示

Exception in thread "Thread-2" org.postgresql.util.PSQLException: FATAL: password authentication failed for user "myuser"

即使在命令行中,这也能正常工作

psql -U myuser mydatabase
Welcome to psql 8.3.5, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
      \h for help with SQL commands
      \? for help with psql commands
      \g or terminate with semicolon to execute query
      \q to quit

最佳答案

是的,你可以。

请注意,Postres JDBC 驱动程序始终使用 IP 套接字(pg_hba.conf 中的host),即使数据库在本地机器上,psql 可以使用本地套接字(pg_hba.conf 中的local)。因此,如果 psql 使用 trust 身份验证而 JDBC 不使用,您可能应该为 IP 套接字配置 trust 身份验证,see documentation .

关于java - 是否可以在没有密码的情况下创建 jdbc 连接(使用 postgresql 'trust' )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2832966/

相关文章:

Java 用户输入必须在范围内/为 20 行生成 10 的整数

java - 使用 Jersey 添加新资源

java - 我的 1 :30 minute long WAV file is only playing for three seconds

sql - 使用 bash 命令在 postgresql 中创建数据库

mysql - 对 MySQL 数据库的 JavaBean SQL 查询,仅在 JSF 中显示两倍的结果

java - 从 Java 中的 yaml 读取 map 得到 null

sql - Postgres 将数据按天分组

sql - 如何从两个只有 ID 重叠的表中获取最大值?

postgresql - 有什么方法可以直接在JDBC中禁用参数或执行SQL语句吗?

java - 从 jdbc/postgresql 获取新创建表的列元数据