postgresqljdbc

Specifying multiple addresses to postgres DB via JDBC driver


When connecting to postgres via psql I connect via:

psql 'postgresql://clienthttpfile@pg-clienthttpfile-dev3.dev-dc1-c1.gb.app:5432,pg-clienthttpfile-dev3.dev-dc2-c1.gb.app:5432/clienthttpfile?connect_timeout=5&target_session_attrs=read-write'

I'm trying to connect my app via the JDBC driver and the connection string above does not work:

I tried with double and single quotes

database.url="jdbc:postgresql://clienthttpfile@pg-clienthttpfile-dev3.dev-dc1-c1.gb.app:5432,pg-clienthttpfile-dev3.dev-dc2-c1.gb.app:5432/clienthttpfile?connect_timeout=5&target_session_attrs=read-write"
database.url='jdbc:postgresql://clienthttpfile@pg-clienthttpfile-dev3.dev-dc1-c1.gb.app:5432,pg-clienthttpfile-dev3.dev-dc2-c1.gb.app:5432/clienthttpfile?connect_timeout=5&target_session_attrs=read-write'

Both throw:

Driver org.postgresql.Driver claims to not accept jdbcUrl, <some db url>

If I use no quotes the connection just times out. Any idea what's wrong?


Solution

  • It fails because the URL is wrong. As documented in the PostgreSQL JDBC driver documentation under Connection Fail-over:

    The syntax for the connection url is: jdbc:postgresql://host1:port1,host2:port2/database

    In other words, you need to use

    jdbc:postgresql://pg-clienthttpfile-dev3.dev-dc1-c1.gb.app:5432,pg-clienthttpfile-dev3.dev-dc2-c1.gb.app:5432/clienthttpfile?connectTimeout=5&targetServerType=primary

    That is:

    Don't just assume that a connection URL for one tool maps one-to-one for another tool or library, check their documentation.

    I would also guess that you should not add quotes (whether single or double quotes) around the URL, but that depends on the actual configuration file format. Assuming it is a properties file, it should not be enclosed in quotes.