简体   繁体   English

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

[英]Is it possible to create a jdbc connection without a password (using postgresql 'trust')?

I am using jdbc to connect to a postgresql database in a java application (actually the app is written in Groovy). 我正在使用jdbc连接到java应用程序中的postgresql数据库(实际上该应用程序是用Groovy编写的)。 I have postgresql set up to use the 'trust' authentication method. 我有postgresql设置使用'信任'身份验证方法。 Is it possible to open a jdbc connection without specifying a password? 是否可以在不指定密码的情况下打开jdbc连接? When I try to use the normal constructor with a blank password, it fails with 当我尝试使用带有空密码的普通构造函数时,它会失败

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

Even though, from the command line, this works fine 即使从命令行,这工作正常

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

Yes, you can. 是的你可以。

Note that Postres JDBC driver always uses IP sockets ( host in pg_hba.conf ), even if database is on the local machine, then psql can use local sockets ( local in pg_hba.conf ). 需要注意的是Postres JDBC驱动程序始终使用IP套接字( hostpg_hba.conf ),即使数据库是在本地机器上,然后psql可以使用本地套接字( localpg_hba.conf )。 So, if psql works with trust authentication and JDBC doesn't, you probably should configure trust authentication for IP sockets, see documentation . 因此,如果psql使用trust身份验证而JDBC不使用,则可能应该为IP套接字配置trust身份验证, 请参阅文档

Already answered but: 已经回答但是:

When you connect using the psql client program, and don't specify a host ( -h ), the default is to use a local socket (at least in Linux). 使用psql客户端程序连接时,如果没有指定主机( -h ),则默认使用本地套接字 (至少在Linux中)。 In JDBC, instead, you will use a TCP/IP socket. 相反,在JDBC中,您将使用TCP / IP套接字。 Then, to check your connection problem you should invoke psql with the same settings you are using in JDBC, host included. 然后,要检查连接问题,您应该使用在JDBC中使用的相同设置来调用psql,包括主机。

For example 例如

 psql -U myuser -h 127.0.0.1 mydatabase    # uses TCP/IP

Which is not the same as 哪个不一样

 psql -U myuser mydatabase      # uses local socket (non TCP/IP)

至少包括3.1版的GlassFish在指定空JDBC密码时存在问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM