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

I am using jdbc to connect to the postgresql database in a Java application (actually the application is written in Groovy). I have postgresql installed to use the trust authentication method. Is it possible to open a jdbc connection without specifying a password? When I try to use a regular constructor with an empty password, it fails with

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

Even though it works fine from the command line

 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 
+7
java postgresql jdbc
source share
3 answers

Yes, you can.

Note that the Postres JDBC driver always uses IP sockets ( host in pg_hba.conf ), even if the database is on a local machine, then psql can use local sockets ( local in pg_hba.conf ). So, if psql works with trust authentication and JDBC does not, you should probably set up trust authentication for IP sockets, see the Documentation .

+4
source share

Already answered, but:

When you connect using the psql client program and do not specify the host ( -h ), the local socket is used by default (at least on Linux). In JDBC, you will use a TCP / IP socket instead. Then, to check your connection problem, you must call psql with the same settings that you use in JDBC, including the host.

for example

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

This is not the same as

  psql -U myuser mydatabase # uses local socket (non TCP/IP) 
+1
source share

GlassFish, at least including version 3.1, has a problem with specifying empty JDBC passwords .

0
source share

All Articles