1

I'm trying to integrate my Keycloak server with Postgres, and ran into an odd problem. When starting my keycloak server with the provided command on Ubuntu:

./kc.sh start-dev --http-port ${PORT} --db-url=jdbc:postgres://myurl.com/database --db=postgres --db-schema auth --db-username postgres --db-password password

I get the following error:

[org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator] (JPA Startup Thread: keycloak-default) HHH000342: Could not obtain connection to query metadata: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

But of course, shown above, my database isn't on localhost, and I set the --db-url option accordingly. This makes me think keycloak is ignoring --db-url, as localhost:5432 is the default value for a Postgres connection.

Does anyone know why this might be happening?

EDIT: Running Keycloak version 19.0.1

Twisted Tea
  • 49
  • 11
  • Which version of Keycloak you are running? You might want to add that to the post so it would be easier to answer this question. Thank you! – Laurenzo Sep 20 '22 at 05:30
  • 1
    @Laurenzo Good call, post updated. – Twisted Tea Sep 20 '22 at 17:12
  • 1
    Shouldn't this be `--db-url jdbc:postgres://myurl.com/database` (without the equal sign)? – sventorben Sep 20 '22 at 18:00
  • @sventorben Yeah I just gave that a try, it gives me the same error and still thinks I'm trying to connect to localhost:5432. – Twisted Tea Sep 20 '22 at 23:58
  • 1
    Good comment by @sventorben! Also `--db=postgres` should be `--db postgres` instead! – Laurenzo Sep 21 '22 at 10:54
  • @Laurenzo Tried that too, still the same issue. – Twisted Tea Sep 22 '22 at 13:23
  • @TwistedTea this is weird. I've tried this out with the config you gave, with the exception I didn't use `start-dev` (I use more prod-like setup). Are you sure you didn't give some options described in Keycloak manual (https://www.keycloak.org/server/all-config#_database) that might overlap with the`--db-url` parameter? I've faced similar situation where I gave `--db-url-host` or `--db-url-port` in addition to the `--db-url` parameter and it only used the `--db-url-host` and `--db-url-port` parameter values and ignored the `--db-url` one completely. – Laurenzo Sep 27 '22 at 09:25
  • @Laurenzo Yeah it's a pretty weird issue, I really appreciate the help. My keycloak.conf is completely commented out, so the only parameters that apply should be the ones listed in the command. The only thing I can think of is that I'm running keycloak on a virtual machine, and hosting the database on localhost with ngrok (`ngrok tcp 5432`). But even when I changed the URL it would give the same error message, saying it was trying to connect to localhost:5432. – Twisted Tea Sep 30 '22 at 02:49

1 Answers1

1

The JDBC is should be:

--db-url=jdbc:postgresql://myurl.com/database

not:

--db-url=jdbc:postgres://myurl.com/database
Eamonn Kenny
  • 1,926
  • 18
  • 20