最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

postgresql - Sonarqube and Postgres running on Docker throws ‘Create initial schema’ failed and ERROR: relation "active

programmeradmin1浏览0评论

I tried to implement an example of Spring Boot with Postgres through Docker. After running the command named docker-compose up -d --build, they are all greeen. However, I got the errors in the console when I looked through postgres and sonarqube.

Sonarqube throws

2025-03-13 19:03:37 Caused by: java.lang.IllegalStateException: Fail to execute CREATE TABLE active_rule_parameters (uuid VARCHAR (40) NOT NULL,value VARCHAR (4000) NULL,rules_parameter_key VARCHAR (128) NULL,active_rule_uuid VARCHAR (40) NOT NULL,rules_parameter_uuid VARCHAR (40) NOT NULL, CONSTRAINT pk_active_rule_parameters PRIMARY KEY (uuid))
Caused by: .postgresql.util.PSQLException: ERROR: relation "active_rule_parameters" already exists

Postgres throws

2025-03-13 19:02:48 2025-03-13 16:02:48.584 UTC [494] ERROR:  relation "active_rule_parameters" already exists
2025-03-13 19:02:48 2025-03-13 16:02:48.584 UTC [494] STATEMENT:  CREATE TABLE active_rule_parameters (uuid VARCHAR (40) NOT NULL,value VARCHAR (4000) NULL,rules_parameter_key VARCHAR (128) NULL,active_rule_uuid VARCHAR (40) NOT NULL,rules_parameter_uuid VARCHAR (40) NOT NULL, CONSTRAINT pk_active_rule_parameters PRIMARY KEY (uuid))

Here is my docker-compose.yml shown below

services:

  postgres:
    image: postgres:latest
    container_name: postgres-container
    restart: always
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=footballteamdatabase
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - footballteamapi_network

  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: pgadmin-container
    restart: always
    ports:
      - "5050:80"
    environment:
      - [email protected]
      - PGADMIN_DEFAULT_PASSWORD=admin
    depends_on:
      - postgres
    networks:
      - footballteamapi_network

  footballteamapi:
    image: 'footballteamapi:latest'
    build:
      context: .
      dockerfile: Dockerfile
    container_name: footballteamapi
    restart: on-failure
    env_file:
      - .env  # Use the .env file for environment variables
    ports:
      - "3112:3112"
    environment:
      - server.port=3112
      - FOOTBALL_TEAM_DB_IP=postgres
      - FOOTBALL_TEAM_DB_PORT=5432
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - JAVA_OPTS=-Dname=footballteamapi
    depends_on:
      - postgres
      - sonarqube
    networks:
      - footballteamapi_network

  sonarqube:
    image: sonarqube:latest
    container_name: sonarqube
    restart: always
    ports:
      - "9000:9000"
    environment:
      - SONAR_JDBC_URL=jdbc:postgresql://postgres:5432/footballteamdatabase
      - SONAR_JDBC_USERNAME=${POSTGRES_USER}
      - SONAR_JDBC_PASSWORD=${POSTGRES_PASSWORD}
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
    depends_on:
      - postgres
    networks:
      - footballteamapi_network

  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    restart: unless-stopped
    ports:
      - "9090:9090"
    volumes:
      - ./data/prometheus/config:/etc/prometheus/
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
    networks:
      - footballteamapi_network

  grafana:
    image: "grafana/grafana-oss:latest"
    pull_policy: always
    container_name: grafana
    restart: unless-stopped
    ports:
      - "3000:3000"
    volumes:
      - ./data/grafana:/var/lib/grafana
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin
      - GF_SERVER_DOMAIN=localhost
    networks:
      - footballteamapi_network

  loki:
    image: grafana/loki:latest
    container_name: loki
    restart: unless-stopped
    ports:
      - "3100:3100"
    volumes:
      - ./data/loki/config/loki-config.yaml:/etc/loki/loki-config.yaml
    networks:
      - footballteamapi_network

volumes:
  postgres_data:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:

networks:
  footballteamapi_network:
    driver: bridge

How can I fix the issue?

I tried to implement an example of Spring Boot with Postgres through Docker. After running the command named docker-compose up -d --build, they are all greeen. However, I got the errors in the console when I looked through postgres and sonarqube.

Sonarqube throws

2025-03-13 19:03:37 Caused by: java.lang.IllegalStateException: Fail to execute CREATE TABLE active_rule_parameters (uuid VARCHAR (40) NOT NULL,value VARCHAR (4000) NULL,rules_parameter_key VARCHAR (128) NULL,active_rule_uuid VARCHAR (40) NOT NULL,rules_parameter_uuid VARCHAR (40) NOT NULL, CONSTRAINT pk_active_rule_parameters PRIMARY KEY (uuid))
Caused by: .postgresql.util.PSQLException: ERROR: relation "active_rule_parameters" already exists

Postgres throws

2025-03-13 19:02:48 2025-03-13 16:02:48.584 UTC [494] ERROR:  relation "active_rule_parameters" already exists
2025-03-13 19:02:48 2025-03-13 16:02:48.584 UTC [494] STATEMENT:  CREATE TABLE active_rule_parameters (uuid VARCHAR (40) NOT NULL,value VARCHAR (4000) NULL,rules_parameter_key VARCHAR (128) NULL,active_rule_uuid VARCHAR (40) NOT NULL,rules_parameter_uuid VARCHAR (40) NOT NULL, CONSTRAINT pk_active_rule_parameters PRIMARY KEY (uuid))

Here is my docker-compose.yml shown below

services:

  postgres:
    image: postgres:latest
    container_name: postgres-container
    restart: always
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=footballteamdatabase
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - footballteamapi_network

  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: pgadmin-container
    restart: always
    ports:
      - "5050:80"
    environment:
      - [email protected]
      - PGADMIN_DEFAULT_PASSWORD=admin
    depends_on:
      - postgres
    networks:
      - footballteamapi_network

  footballteamapi:
    image: 'footballteamapi:latest'
    build:
      context: .
      dockerfile: Dockerfile
    container_name: footballteamapi
    restart: on-failure
    env_file:
      - .env  # Use the .env file for environment variables
    ports:
      - "3112:3112"
    environment:
      - server.port=3112
      - FOOTBALL_TEAM_DB_IP=postgres
      - FOOTBALL_TEAM_DB_PORT=5432
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - JAVA_OPTS=-Dname=footballteamapi
    depends_on:
      - postgres
      - sonarqube
    networks:
      - footballteamapi_network

  sonarqube:
    image: sonarqube:latest
    container_name: sonarqube
    restart: always
    ports:
      - "9000:9000"
    environment:
      - SONAR_JDBC_URL=jdbc:postgresql://postgres:5432/footballteamdatabase
      - SONAR_JDBC_USERNAME=${POSTGRES_USER}
      - SONAR_JDBC_PASSWORD=${POSTGRES_PASSWORD}
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
    depends_on:
      - postgres
    networks:
      - footballteamapi_network

  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    restart: unless-stopped
    ports:
      - "9090:9090"
    volumes:
      - ./data/prometheus/config:/etc/prometheus/
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
    networks:
      - footballteamapi_network

  grafana:
    image: "grafana/grafana-oss:latest"
    pull_policy: always
    container_name: grafana
    restart: unless-stopped
    ports:
      - "3000:3000"
    volumes:
      - ./data/grafana:/var/lib/grafana
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin
      - GF_SERVER_DOMAIN=localhost
    networks:
      - footballteamapi_network

  loki:
    image: grafana/loki:latest
    container_name: loki
    restart: unless-stopped
    ports:
      - "3100:3100"
    volumes:
      - ./data/loki/config/loki-config.yaml:/etc/loki/loki-config.yaml
    networks:
      - footballteamapi_network

volumes:
  postgres_data:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:

networks:
  footballteamapi_network:
    driver: bridge

How can I fix the issue?

Share Improve this question edited Mar 13 at 19:22 Sercan Noyan Germiyanoğlu asked Mar 13 at 16:04 Sercan Noyan GermiyanoğluSercan Noyan Germiyanoğlu 2,7864 gold badges53 silver badges124 bronze badges 8
  • You're connected to a database where this table name is already used. It might have a different structure, but the name already exists. Please take a look at the error message. (or your code executes this command more than once, and only the second attempt fails with this error message) – Frank Heikens Commented Mar 13 at 16:23
  • I removed all volumes through docker system prune . Next, I run the command docker-compose up -d but I have the same errors. @FrankHeikens – Sercan Noyan Germiyanoğlu Commented Mar 13 at 17:49
  • docker system prune will remove anonymous volumes, but not named volumes. If you want to remove the postgres_data volume, you will need to do docker volume --prune – richyen Commented Mar 13 at 18:39
  • @richyen I already did it but nothing changed. – Sercan Noyan Germiyanoğlu Commented Mar 13 at 19:00
  • What parts of this setup actually execute the CREATE TABLE statement? The Compose file you've shown it is kind of long; is it possible to trim it down by removing services that aren't related to your problem (Prometheus, Grafana, PGAdmin) and options that aren't usually necessary (networks:, container_name:)? – David Maze Commented Mar 13 at 19:25
 |  Show 3 more comments

1 Answer 1

Reset to default 0

I fixed the issue.

Here is the solution way

1 ) Prepare the init.sql script to create separate databases and users.

Create a file called init.sql in the same folder as docker-compose.yml with this content

CREATE DATABASE footballteamdatabase;

CREATE DATABASE sonarqubedb;

CREATE USER sonar WITH PASSWORD 'sonar';

GRANT ALL PRIVILEGES ON DATABASE sonarqube TO sonar;

2 ) Configure SonarQube properly (using mounted volume)

SonarQube no longer supports database connection via environment variables

  sonarqube:
    image: sonarqube:latest
    container_name: sonarqube
    restart: always
    ports:
      - "9000:9000"
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
    depends_on:
      - postgres
    networks:
      - footballteamapi_network

3 ) Modify your existing docker-compose.yml through using a single Postgres instance with two databases

services:
  postgres:
    image: postgres:latest
    container_name: postgres-container
    restart: always
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    networks:
      - footballteamapi_network
发布评论

评论列表(0)

  1. 暂无评论