Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). I have postgresql set up to use the 'trust' authentication method. Is it possible to open a jdbc connection without specifying a password? 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
like image 450
mojones Avatar asked May 14 '10 09:05

mojones


People also ask

How do I log into Postgres without password?

Edit the pg_dba. conf file and change all local connections from md5 to trust. By doing this, you can log in to the PostgreSQL database server without using a password.

Is the strongest password authentication method in PostgreSQL?

SCRAM-SHA-256: The strongest authentication method, introduced in PostgreSQL 10. This method prevents password sniffing on untrusted connections. The default password authentication method is MD5 to use this feature, the configuration parameter password_encryption should be changed to scram-sha-256.


2 Answers

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). So, if psql works with trust authentication and JDBC doesn't, you probably should configure trust authentication for IP sockets, see documentation.

like image 67
axtavt Avatar answered Nov 15 '22 17:11

axtavt


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). In JDBC, instead, you will use a TCP/IP socket. Then, to check your connection problem you should invoke psql with the same settings you are using in JDBC, host included.

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)
like image 44
leonbloy Avatar answered Nov 15 '22 18:11

leonbloy