SonarQube sometimes can't connect to Postgres (Docker Setup)


I’m running the latest official docker image for SonarQube (8.8 at time of writing) and everything appears to be working as expected. I can add projects, run analysis whatnot.

However, I’ve noticed my CI/CD pipeline sometimes randomly failing with " SonarQube server can not be reached". This seems to happen when Dependabot bumps a group of projects and they all send their analysis’ to Sonar in a short period of time.

While this is going on, the Sonar UI is still working and responsive. Nothing indicates anything is wrong.

Taking a look at the logs from Sonar, it looks like it’s dropping the database connection and unable to reconnect.

Caused by: org.postgresql.util.PSQLException: Connection to sonarqube-postgres: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(
	at org.postgresql.core.ConnectionFactory.openConnection(
	at org.postgresql.jdbc.PgConnection.<init>(
	at org.postgresql.Driver.makeConnection(
	at org.postgresql.Driver.connect(
	at org.apache.commons.dbcp2.DriverConnectionFactory.createConnection(
	at org.apache.commons.dbcp2.PoolableConnectionFactory.makeObject(
	at org.apache.commons.pool2.impl.GenericObjectPool.create(
	at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(
	at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(
	at org.apache.commons.dbcp2.PoolingDataSource.getConnection(
	at org.apache.commons.dbcp2.BasicDataSource$
	at org.apache.commons.dbcp2.BasicDataSource$
	at java.base/ Method)
	at org.apache.commons.dbcp2.BasicDataSource.getConnection(
	at org.sonar.db.profiling.NullConnectionInterceptor.getConnection(
	at org.sonar.db.profiling.ProfiledDataSource.getConnection(
	at org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(
	at org.apache.ibatis.transaction.jdbc.JdbcTransaction.getConnection(
	at org.apache.ibatis.executor.BaseExecutor.getConnection(
	at org.apache.ibatis.executor.ReuseExecutor.prepareStatement(
	at org.apache.ibatis.executor.ReuseExecutor.doQuery(
	at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(
	at org.apache.ibatis.executor.BaseExecutor.query(
	at org.apache.ibatis.executor.CachingExecutor.query(
	at org.apache.ibatis.executor.CachingExecutor.query(
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(
	... 23 common frames omitted
Caused by: Connection refused (Connection refused)
	at java.base/ Method)
	at java.base/ Source)
	at java.base/ Source)
	at java.base/ Source)
	at java.base/ Source)
	at java.base/ Source)
	at org.postgresql.core.PGStream.createSocket(
	at org.postgresql.core.PGStream.<init>(
	at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(
	at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(
	... 49 common frames omitted

If I take a look a the database logs:

PostgreSQL Database directory appears to contain a database; Skipping initialization

2021-04-12 15:11:23.762 UTC [1] LOG:  starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2021-04-12 15:11:23.763 UTC [1] LOG:  listening on IPv4 address "", port 5432
2021-04-12 15:11:23.763 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2021-04-12 15:11:23.767 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2021-04-12 15:11:23.775 UTC [26] LOG:  database system was shut down at 2021-04-12 15:11:21 UTC
2021-04-12 15:11:23.784 UTC [1] LOG:  database system is ready to accept connections

Everything seems to be in order.

Here is how I’m running both Sonar and Postgres behind a reverse proxy with context path mapping, have I just messed up somewhere?

Is there a way I can debug this further and work out where the problem sits?

Hello @peavers and welcome to the community :wave:

your config looks okay for now even thou i would recommend you switching to our environment variables for configuration and maybe remove the port forwarding of sonarqube in favor of routing everything throu traefik.

I assume that the connection drop is a result of a burst of resources. you can try to set limits to the sonarqube deployment to make sure that on high load there is enough cpu/ram available for underlying the system processes:

    container_name: sonarqube
    image: sonarqube:latest
    restart: unless-stopped
    cpus: 0.5
    mem_limit: 4096M
    mem_reservation: 1024M

hope that helps

Thanks a lot for the response, I did wonder if CPU/Memory was an issue so I’ll implement your suggestions and report back.

Quick question though: I was changing over to environment variables as suggested but I can’t find the var for to replace sonar.core.serverBaseURL, do you happen to know what this should be?

This setting should be configured in the UI as it will persist in the database, so there is no need to have it as an env var permanently. if you want to set it automatically you could use the API for this (api/settings/set).