diff options
author | Christian Grothoff <christian@grothoff.org> | 2024-03-24 13:01:32 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2024-03-24 13:01:32 +0100 |
commit | 2460b772ac4e870aa307512cce1a8aa454d2d625 (patch) | |
tree | 9dd028a039ed4cc09f28bbb3ff76340e41438f70 | |
parent | 5da67e78df95fc49fc38858aedd123af19fe0bb9 (diff) | |
download | libeufin-2460b772ac4e870aa307512cce1a8aa454d2d625.tar.gz libeufin-2460b772ac4e870aa307512cce1a8aa454d2d625.tar.bz2 libeufin-2460b772ac4e870aa307512cce1a8aa454d2d625.zip |
make libeufin respect PGHOST and PGPORT environment variables
-rw-r--r-- | common/src/main/kotlin/DB.kt | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/common/src/main/kotlin/DB.kt b/common/src/main/kotlin/DB.kt index e36182b7..d2cb0a01 100644 --- a/common/src/main/kotlin/DB.kt +++ b/common/src/main/kotlin/DB.kt @@ -62,22 +62,38 @@ fun getJdbcConnectionFromPg(pgConn: String): String { } var maybeUnixSocket = false val parsed = URI(pgConn) - val hostAsParam: String? = if (parsed.query != null) { + var hostAsParam: String? = if (parsed.query != null) { getQueryParam(parsed.query, "host") } else { null } + var pgHost = System.getenv("PGHOST") + if (null == pgHost) + pgHost = parsed.host + var pgPort = System.getenv("PGPORT") + if (null == pgPort) { + if (-1 == parsed.port) + pgPort = "5432" + else + pgPort = parsed.port.toString() + } + /** * In some cases, it is possible to leave the hostname empty * and specify it via a query param, therefore a "postgresql:///"-starting * connection string does NOT always mean Unix domain socket. * https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING */ - if (parsed.host == null && + if (pgHost == null && (hostAsParam == null || hostAsParam.startsWith('/')) ) { maybeUnixSocket = true } + if (pgHost != null && + (pgHost.startsWith('/')) + ) { + maybeUnixSocket = true + } if (maybeUnixSocket) { // Check whether the database user should differ from the process user. var pgUser = getCurrentUser() @@ -86,7 +102,9 @@ fun getJdbcConnectionFromPg(pgConn: String): String { if (maybeUserParam != null) pgUser = maybeUserParam } // Check whether the Unix domain socket location was given non-standard. - val socketLocation = hostAsParam ?: "/var/run/postgresql/.s.PGSQL.5432" + if ( (null == hostAsParam) && (null != pgHost) ) + hostAsParam = pgHost + "/.s.PGSQL." + pgPort + val socketLocation = hostAsParam ?: "/var/run/postgresql/.s.PGSQL." + pgPort if (!socketLocation.startsWith('/')) { throw Exception("PG connection wants Unix domain socket, but non-null host doesn't start with slash") } @@ -99,6 +117,7 @@ fun getJdbcConnectionFromPg(pgConn: String): String { // into one that the JDBC driver likes. return "jdbc:postgresql://" + pgConn.removePrefix("postgres://") } + logger.info("connecting to database via JDBC string '$pgConn'") return "jdbc:$pgConn" } |