Metabase not connecting to postgres database

I am using helm to deploy metabase to a kubernetes cluster. I have passed a connection string to the helm chart, and specified postgres as the database. The challenge is that postgres does not connect. It seems the port number is hardcoded or fixed. It ignores the port I specified in my chart and tries to connect using the postgres default port.

I logged an issue with the chart creator and after reviewing it came to the conclusion that it must be from metabase itself.

Logs

2022-02-18 11:28:36,140 INFO metabase.core :: Setting up and migrating Metabase DB. Please sit tight, this may take a minute...
2022-02-18 11:28:36,144 INFO db.setup :: Verifying postgres Database Connection ...
2022-02-18 11:28:36,178 ERROR metabase.core :: Metabase Initialization FAILED
clojure.lang.ExceptionInfo: Unable to connect to Metabase postgres DB. {}
	at metabase.db.setup$fn__33730$verify_db_connection__33735$fn__33736$fn__33737.invoke(setup.clj:102)
	at metabase.db.setup$fn__33730$verify_db_connection__33735$fn__33736.invoke(setup.clj:100)
	at metabase.db.setup$fn__33730$verify_db_connection__33735.invoke(setup.clj:94)
	at metabase.db.setup$setup_db_BANG_$fn__33765$fn__33766.invoke(setup.clj:142)
	at metabase.util$do_with_us_locale.invokeStatic(util.clj:693)
	at metabase.util$do_with_us_locale.invoke(util.clj:679)
	at metabase.db.setup$setup_db_BANG_$fn__33765.invoke(setup.clj:141)
	at metabase.db.setup$setup_db_BANG_.invokeStatic(setup.clj:140)
	at metabase.db.setup$setup_db_BANG_.invoke(setup.clj:136)
	at metabase.db$setup_db_BANG_$fn__33873.invoke(db.clj:61)
	at metabase.db$setup_db_BANG_.invokeStatic(db.clj:56)
	at metabase.db$setup_db_BANG_.invoke(db.clj:51)
	at metabase.core$init_BANG_.invokeStatic(core.clj:91)
	at metabase.core$init_BANG_.invoke(core.clj:74)
	at metabase.core$start_normally.invokeStatic(core.clj:135)
	at metabase.core$start_normally.invoke(core.clj:129)
	at metabase.core$_main.invokeStatic(core.clj:168)
	at metabase.core$_main.doInvoke(core.clj:162)
	at clojure.lang.RestFn.invoke(RestFn.java:397)
	at clojure.lang.AFn.applyToHelper(AFn.java:152)
	at clojure.lang.RestFn.applyTo(RestFn.java:132)
	at metabase.core.main(Unknown Source)
Caused by: org.postgresql.util.PSQLException: Connection to :5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
	at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303)
	at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51)
	at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223)
	at org.postgresql.Driver.makeConnection(Driver.java:465)
	at org.postgresql.Driver.connect(Driver.java:264)
	at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
	at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
	at clojure.java.jdbc$get_driver_connection.invokeStatic(jdbc.clj:271)
	at clojure.java.jdbc$get_driver_connection.invoke(jdbc.clj:250)
	at clojure.java.jdbc$get_connection.invokeStatic(jdbc.clj:411)
	at clojure.java.jdbc$get_connection.invoke(jdbc.clj:274)
	at clojure.java.jdbc$db_query_with_resultset_STAR_.invokeStatic(jdbc.clj:1111)
	at clojure.java.jdbc$db_query_with_resultset_STAR_.invoke(jdbc.clj:1093)
	at clojure.java.jdbc$query.invokeStatic(jdbc.clj:1182)
	at clojure.java.jdbc$query.invoke(jdbc.clj:1144)
	at clojure.java.jdbc$query.invokeStatic(jdbc.clj:1160)
	at clojure.java.jdbc$query.invoke(jdbc.clj:1144)
	at metabase.driver.sql_jdbc.connection$can_connect_with_spec_QMARK_.invokeStatic(connection.clj:245)
	at metabase.driver.sql_jdbc.connection$can_connect_with_spec_QMARK_.invoke(connection.clj:242)
	at metabase.db.setup$fn__33730$verify_db_connection__33735$fn__33736$fn__33737.invoke(setup.clj:100)
	... 21 more
Caused by: java.net.ConnectException: Connection refused (Connection refused)
	at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.base/java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
	at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
	at java.base/java.net.AbstractPlainSocketImpl.connect(Unknown Source)
	at java.base/java.net.SocksSocketImpl.connect(Unknown Source)
	at java.base/java.net.Socket.connect(Unknown Source)
	at org.postgresql.core.PGStream.createSocket(PGStream.java:231)
	at org.postgresql.core.PGStream.<init>(PGStream.java:95)
	at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98)
	at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213)
	... 40 more
2022-02-18 11:28:36,190 INFO metabase.core :: Metabase Shutting Down ...
2022-02-18 11:28:36,191 INFO metabase.server :: Shutting Down Embedded Jetty Webserver
2022-02-18 11:28:36,201 INFO metabase.core :: Metabase Shutdown COMPLETE

To Reproduce
Steps to reproduce the behavior:

  1. deploy a helm chart to kubernetes using this chart: https://github.com/pmint93/helm-charts
  2. specify postgres as the database
  3. specify a connection string with a port order than the default port
ERROR metabase.core :: Metabase Initialization FAILED
clojure.lang.ExceptionInfo: Unable to connect to Metabase postgres DB. {}
	at metabase.db.setup$fn__33730$verify_db_connection__33735$fn__33736$fn__33737.invoke(setup.clj:102)
	at metabase.db.setup$fn__33730$verify_db_connection__33735$fn__33736.invoke(setup.clj:100)
	at metabase.db.setup$fn__33730$verify_db_connection__33735.invoke(setup.clj:94)
	at metabase.db.setup$setup_db_BANG_$fn__33765$fn__33766.invoke(setup.clj:142)
	at metabase.util$do_with_us_locale.invokeStatic(util.clj:693)
	at metabase.util$do_with_us_locale.invoke(util.clj:679)
	at metabase.db.setup$setup_db_BANG_$fn__33765.invoke(setup.clj:141)
	at metabase.db.setup$setup_db_BANG_.invokeStatic(setup.clj:140)
	at metabase.db.setup$setup_db_BANG_.invoke(setup.clj:136)
	at metabase.db$setup_db_BANG_$fn__33873.invoke(db.clj:61)
	at metabase.db$setup_db_BANG_.invokeStatic(db.clj:56)
	at metabase.db$setup_db_BANG_.invoke(db.clj:51)
	at metabase.core$init_BANG_.invokeStatic(core.clj:91)
	at metabase.core$init_BANG_.invoke(core.clj:74)
	at metabase.core$start_normally.invokeStatic(core.clj:135)
	at metabase.core$start_normally.invoke(core.clj:129)
	at metabase.core$_main.invokeStatic(core.clj:168)
	at metabase.core$_main.doInvoke(core.clj:162)
	at clojure.lang.RestFn.invoke(RestFn.java:397)
	at clojure.lang.AFn.applyToHelper(AFn.java:152)
	at clojure.lang.RestFn.applyTo(RestFn.java:132)
	at metabase.core.main(Unknown Source)
Caused by: org.postgresql.util.PSQLException: Connection to :5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
	at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303)
  • Your databases: Postgres
  • Metabase version: docker image metabase/metabase:v0.41.6
  • Metabase hosting environment: Digitalocean kubernetes
  • Metabase internal database: postgress

Hi @davidshare
You're not posting your connection string, so it's hard to help you - have a look here:
https://www.metabase.com/docs/latest/operations-guide/environment-variables.html#mb_db_connection_uri

This is what the connection string looks like:

postgres://<database user>:<password>@<part of the host>ondigitalocean.com:25060/defaultdb?ssl=true&sslmode=require&sslfactory=org.postgresql.ssl.NonValidatingFactory

@davidshare Then follow how it's written in our documentation - notice the user/pass is moved:
postgresql://db.example.com:port/mydb?user=dbuser&password=dbpassword&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

yes, I have seen it. Thank you.

I changed the url to this format, and I am still getting the same error messages:

postgresql://xxxx-metabase-xxxxxxxx.ondigitalocean.com:25060/defaultdb?user=xxxx&password=xxxxxxxxxx&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

error:

Connection to :5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

It appears metabase is still trying to use the default port for postgres.

Thank you @flamber for all your help. This works now. Turns out the secrets were not being deleted when the helm release was uninstalled, so it kept using the old secret file.

1 Like