โ˜• XOPSSCHOOL โ€” LEARNING MADE AFFORDABLE
Build the Skills Your Next Career Move Demands.
For the Price of One Coffee a Month.
Why spend hundreds on individual courses? Unlock an entire year of technical learning with one Coffee Pass.
US$4.90 /month
US$58.80/year
โ‚น499 + GST/month
โ‚น5,988 + GST/year
โ‚ฌ4.90 /month
โ‚ฌ58.80/year
12 Months of Access • Billed Annually • No Automatic Renewal
DevOps • Cloud • SRE • Kubernetes • Terraform • DevSecOps • AI • AIOps • MLOps • LLMOps • FinOps • GitOps • Platform Engineering & Much More
192
Premium Courses
1,085
Video Sessions
99
Ops Disciplines
86
Tool Courses
340
Assignments
227
Quizzes
โœ“ QR-Verifiable Certificates   โœ“ Learn at Your Own Pace   โœ“ Hands-on Learning   โœ“ New Courses Included
Learn from real-world, instructor-led video sessions by
Rajesh Kumar
Principal Architect • Trainer • Mentor • 15+ Years of Industry Experience
โ˜• Get My Coffee Pass โ€” Start Learning Today →
One Payment. Every Premium Course. One Full Year.
The best investment you can make is in the person you are becoming.

How to Migrate Sonarqube from H2 to Postgresql?

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.

ItemHow to carry it over
Custom quality profilesQuality 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 gatesThere is no built-in export. Note the conditions and recreate them in the UI or through the api/qualitygates Web API.
Global settings and webhooksCopy them from Administration and set them again by hand.
Users, groups, permissionsRecreate them, or connect LDAP, SAML or another identity provider so users are provisioned again.
Analysis tokensGenerate new tokens and update the secrets in your CI tools.
Project historyCannot 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)

  1. 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
  2. Watch the logs while the schema is created: tail -f /opt/sonarqube/logs/sonar.log /opt/sonarqube/logs/web.log
  3. Check the status. It shows STARTING while the schema is being created, then UP: curl -s http://localhost:9000/api/system/status
  4. Confirm that the tables were created in PostgreSQL: docker exec -it sonar-postgres psql -U sonar -d sonarqube -c '\dt'
  5. Open http://<server-ip>:9000, log in with admin / admin and set a new password when prompted.
  6. Check that the embedded-database warning no longer appears in the UI and that Administration > System shows PostgreSQL as the database.
  7. 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)CauseFix
Connection refused (web.log)Wrong host or port, the container is not running, or a firewall is blocking the connectionRun 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 variablesReset 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 databaseALTER 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 setSet 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 userFix limits.d or the systemd LimitNOFILE setting
can not run elasticsearch as root (es.log)SonarQube was started as rootCreate a sonarqube user, run chown -R on the install directory, start as that user
UI still warns about the embedded databaseThe JDBC URL is commented out, the wrong file was edited, or an environment variable overrides itCheck 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 postgres superuser.
  • 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

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals

Related Posts

Gitlab Tutorials: CI for automation and .Net Framework Sonarqube Codecoverage

This page shows how to build a .NET Framework project in GitLab CI, run its unit tests with code coverage, and send the results to SonarQube or…

Read More

Step by Step Guide to Monitor PostGreSQL using Zabbix Agent

To monitor PostgreSQL with the classic Zabbix agent, you install the agent on the database host, create a read-only monitoring role, and deploy the user parameters and…

Read More

Example of sonar-project.properties file

sonar-project.properties is the configuration file SonarScanner CLI reads from a project's root directory. It tells the scanner which project to report to, where the source and test…

Read More

Rundeck Database Migration From H2 to MySql and postgresql

This guide moves Rundeck off its embedded H2 database and onto MySQL or PostgreSQL, which is what you should use in production. The steps above set up…

Read More

Deploying Nexus Certificate Manager (CM) and Protocol Gateway (PGWY) on Kubernetes

Step-by-step tutorial, written from an actual deployment of Certificate Manager 8.15.1-1 (CF server image 8.15.1-1, PGW image 8.15.1-1) on a 2-node kubeadm cluster. Status of this document (2026-11-12) Steps 1-8 were executed…

Read More

Sonar Maven Plugin 1.0-beta-2: What Changed for Archiva/Nexus-Based Builds

This historical release note introduced simpler Sonar Maven Plugin behavior when using repository managers like Archiva or Nexus. Hereโ€™s what changed from beta-1, how compatibility works with Sonar versions, and how to validate legacy CI behavior.

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x