This page shows how to move SonarQube off its embedded H2 database and onto PostgreSQL: run PostgreSQL in Docker, point sonar.properties at it, set the kernel limits that Elasticsearch needs, then start SonarQube and check that it works. SonarQube has no supported tool for copying H2 data into PostgreSQL, so in practice you switch to a fresh PostgreSQL database and re-analyse your projects. The sections below the original steps cover what you can keep, safer commands and how to fix the usual errors.
Step 1 – Run Postgresql using docker command
$ docker run --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres
Step 2 – Modify sonar.properties
sonar.jdbc.username=postgres
sonar.jdbc.password=mysecretpassword
sonar.jdbc.url=jdbc:postgresql://172.31.13.0:5432/postgres?currentSchema=publicCode language: JavaScript (javascript)
Step 3 – Do this
Step 4 – Do this
Step 5 – Start SonarQube
Step 6 – Access http://52.66.187.172:9000/
admin
admin
What this migration really does
The embedded H2 database is for evaluation only. It cannot be upgraded to newer SonarQube versions and is not supported in production. SonarQube does not provide a tool that exports H2 data and imports it into PostgreSQL. When you change sonar.jdbc.url as in the steps above, SonarQube finds an empty database, creates a new schema and starts with no data.
Plan for these results: projects, analysis history, issues and their comments, users, tokens and permissions all start over. Your code and CI pipelines stay the same. Once the pipelines run again against the new server, current measurements and issues come back. Old history does not.
Note: Keep the old $SONARQUBE_HOME/data directory, which holds the H2 files, until the new setup is confirmed. To roll back, stop SonarQube, comment out the sonar.jdbc.* lines and start it again.
Save what you can before switching
Some configuration can be exported from the H2 instance before you shut it down. Do this first.
| Item | How to carry it over |
|---|---|
| Custom quality profiles | Quality Profiles > profile menu > Back Up saves an XML file. On the new instance use Restore. The api/qualityprofiles/backup and api/qualityprofiles/restore Web API endpoints do the same thing. |
| Quality gates | There is no built-in export. Note the conditions and recreate them in the UI or through the api/qualitygates Web API. |
| Global settings and webhooks | Copy them from Administration and set them again by hand. |
| Users, groups, permissions | Recreate them, or connect LDAP, SAML or another identity provider so users are provisioned again. |
| Analysis tokens | Generate new tokens and update the secrets in your CI tools. |
| Project history | Cannot be migrated. Re-run analysis for each project. |
Run PostgreSQL with persistent storage (improved Step 1)
The command in Step 1 uses the postgres superuser and the default postgres database. It also has no volume, so all SonarQube data is lost if the container is removed. The version below pins the image tag, stores data in a named volume and only publishes the port on localhost. Before you pick a tag, check that the PostgreSQL major version is listed as supported in the SonarQube requirements for your version.
docker run -d --name sonar-postgres \
--restart unless-stopped \
-p 127.0.0.1:5432:5432 \
-e POSTGRES_PASSWORD='Admin-Pa55-change-me' \
-v sonar_pgdata:/var/lib/postgresql/data \
postgres:16Code language: JavaScript (javascript)
Next, create a dedicated user and a UTF-8 database owned by that user. SonarQube requires UTF-8. Making sonar the owner of the database also gives it the CREATE rights on the public schema that PostgreSQL 15 and later no longer grant to everyone by default.
docker exec -it sonar-postgres psql -U postgres \
-c "CREATE USER sonar WITH ENCRYPTED PASSWORD 'Sonar-Pa55-change-me';" \
-c "CREATE DATABASE sonarqube OWNER sonar ENCODING 'UTF8';"Code language: JavaScript (javascript)
Note: If SonarQube runs on a different host, publish the port on a private interface (for example -p 5432:5432) and use a firewall or security group so that only the SonarQube server can reach port 5432. Do not expose PostgreSQL to the internet. Newer postgres images change the default data path, so read the image documentation before changing the tag or the volume mount.
Point SonarQube at PostgreSQL (improved Step 2)
Edit $SONARQUBE_HOME/conf/sonar.properties. Use the address where PostgreSQL can actually be reached. If both run on the same host and the port is published as shown above, that address is 127.0.0.1. Hard-coding a private cloud IP such as the one in Step 2 breaks when the instance is replaced. currentSchema=public is the default, so you can leave it out.
sonar.jdbc.username=sonar
sonar.jdbc.password=Sonar-Pa55-change-me
sonar.jdbc.url=jdbc:postgresql://127.0.0.1:5432/sonarqubeCode language: JavaScript (javascript)
If you prefer to keep the password out of the file, set the equivalent environment variables for the service. When both are set, the environment variables take precedence over sonar.properties. They are also how the official SonarQube Docker image is configured.
export SONAR_JDBC_URL=jdbc:postgresql://127.0.0.1:5432/sonarqube
export SONAR_JDBC_USERNAME=sonar
export SONAR_JDBC_PASSWORD='Sonar-Pa55-change-me'Code language: JavaScript (javascript)
Stop SonarQube before you edit anything. Also delete the Elasticsearch data directory ($SONARQUBE_HOME/data/es7 or data/es8, depending on the version) so that the search indexes are rebuilt from the new database and not from leftover H2 data.
Kernel and process limits (what Steps 3 and 4 mean)
SonarQube includes Elasticsearch, which will not start unless the host meets minimum limits. The pages linked in Steps 3 and 4 explain the Elasticsearch errors. Current SonarQube documentation sets higher minimums than those error messages show: vm.max_map_count of at least 524288, fs.file-max of at least 131072, at least 131072 open files and at least 8192 threads for the user that runs SonarQube.
# Apply now
sudo sysctl -w vm.max_map_count=524288
sudo sysctl -w fs.file-max=131072
# Persist across reboots
cat <<'EOF' | sudo tee /etc/sysctl.d/99-sonarqube.conf
vm.max_map_count=524288
fs.file-max=131072
EOF
# Per-user limits for the account that runs SonarQube
cat <<'EOF' | sudo tee /etc/security/limits.d/99-sonarqube.conf
sonarqube - nofile 131072
sonarqube - nproc 8192
EOFCode language: PHP (php)
Note: If SonarQube runs as a systemd service, limits.d does not apply. Put LimitNOFILE=131072 and LimitNPROC=8192 in the [Service] section of the unit file, then run systemctl daemon-reload.
Start SonarQube and verify (Steps 5 and 6 in detail)
- Start SonarQube as a non-root user. Elasticsearch refuses to run as root:
sudo -u sonarqube /opt/sonarqube/bin/linux-x86-64/sonar.sh start - Watch the logs while the schema is created:
tail -f /opt/sonarqube/logs/sonar.log /opt/sonarqube/logs/web.log - Check the status. It shows
STARTINGwhile the schema is being created, thenUP:curl -s http://localhost:9000/api/system/status - Confirm that the tables were created in PostgreSQL:
docker exec -it sonar-postgres psql -U sonar -d sonarqube -c '\dt' - Open
http://<server-ip>:9000, log in withadmin/adminand set a new password when prompted. - Check that the embedded-database warning no longer appears in the UI and that Administration > System shows PostgreSQL as the database.
- Restore the quality profiles, recreate quality gates and tokens, then re-run your CI analyses.
$ curl -s http://localhost:9000/api/system/status
{"id":"...","version":"...","status":"UP"}Code language: JavaScript (javascript)
Common errors and fixes
| Symptom (log file) | Cause | Fix |
|---|---|---|
Connection refused (web.log) | Wrong host or port, the container is not running, or a firewall is blocking the connection | Run docker ps, check the JDBC host, and test with pg_isready -h <host> -p 5432 |
password authentication failed for user (web.log) | Wrong credentials in sonar.properties or environment variables | Reset the password with ALTER USER sonar WITH PASSWORD '...' and update the config |
permission denied for schema public (web.log) | PostgreSQL 15 or later, and the user does not own the database | ALTER DATABASE sonarqube OWNER TO sonar; or GRANT ALL ON SCHEMA public TO sonar; |
max virtual memory areas vm.max_map_count [65530] is too low (es.log) | Kernel limit is not set | Set vm.max_map_count=524288 as shown above |
max file descriptors [4096] ... is too low (es.log) | nofile limit is not applied to the service user | Fix limits.d or the systemd LimitNOFILE setting |
can not run elasticsearch as root (es.log) | SonarQube was started as root | Create a sonarqube user, run chown -R on the install directory, start as that user |
| UI still warns about the embedded database | The JDBC URL is commented out, the wrong file was edited, or an environment variable overrides it | Check conf/sonar.properties and the service environment |
Best practices for production
- Back up the database on a schedule, for example with
pg_dump -Fc sonarqube. SonarQube state lives in PostgreSQL, so these backups are your disaster recovery. - Give SonarQube its own database and an account that is not a superuser. Do not reuse the
postgressuperuser. - Keep PostgreSQL on a version that your SonarQube release lists as supported, and check this before every SonarQube upgrade.
- Store database credentials in a secrets manager or in the service environment, not in files committed to version control.
- Run SonarQube behind a reverse proxy with HTTPS. Do not expose port 9000 directly with a public IP.
For more on code analysis tooling, see the Code Quality & Analysis tutorials. For PostgreSQL and other database topics, see the Databases tutorials.
Frequently asked questions
Can I migrate existing SonarQube data from H2 to PostgreSQL?
No. SonarQube does not provide a supported H2-to-PostgreSQL migration tool. You start with an empty PostgreSQL database, restore exported quality profiles and re-analyse your projects.
What is the default SonarQube login after switching to PostgreSQL?
A new database starts with the default admin / admin account. SonarQube makes you change the password at the first login.
Which PostgreSQL version does SonarQube support?
The supported range depends on the SonarQube release. Check the prerequisites page in the SonarQube documentation for your exact version before you choose the PostgreSQL image tag.
Can SonarQube and PostgreSQL both run in Docker?
Yes. Put both containers on the same user-defined Docker network and set SONAR_JDBC_URL=jdbc:postgresql://sonar-postgres:5432/sonarqube, using the database container name as the host. Mount volumes for SonarQube's data, extensions and logs directories as well as for PostgreSQL.
Why does SonarQube fail to start after I change the database?
Usually it is one of three things: SonarQube cannot connect to PostgreSQL, the sonar user is missing schema privileges, or Elasticsearch refuses to start because of kernel limits. Check web.log and es.log in $SONARQUBE_HOME/logs for the exact error.
Related tutorials
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals