immich icon indicating copy to clipboard operation
immich copied to clipboard

[BUG] postgres fails to startup on new docker-compose

Open osresearch opened this issue 2 years ago • 2 comments

The bug

On first startup postgres fails to create tables:

immich_postgres            | 2023-07-11 08:17:29.479 UTC [60] ERROR:  duplicate key value violates unique constraint "pg_extension_name_index"
immich_postgres            | 2023-07-11 08:17:29.479 UTC [60] DETAIL:  Key (extname)=(uuid-ossp) already exists.
immich_postgres            | 2023-07-11 08:17:29.479 UTC [60] STATEMENT:  CREATE EXTENSION IF NOT EXISTS "uuid-ossp"
immich_postgres            | 2023-07-11 08:17:29.582 UTC [59] ERROR:  duplicate key value violates unique constraint "pg_class_relname_nsp_index"
immich_postgres            | 2023-07-11 08:17:29.582 UTC [59] DETAIL:  Key (relname, relnamespace)=(migrations_id_seq, 2200) already exists.
immich_postgres            | 2023-07-11 08:17:29.582 UTC [59] STATEMENT:  CREATE TABLE "migrations" ("id" SERIAL NOT NULL, "timestamp" bigint NOT NULL, "name" character varying NOT NULL, CONSTRAINT "PK_8c82d7f526340ab734260ea46be" PRIMARY KEY ("id"))
immich_microservices       | [Nest] 1  - 07/11/2023, 8:17:29 AM   ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
immich_microservices       | QueryFailedError: duplicate key value violates unique constraint "pg_class_relname_nsp_index"
immich_microservices       |     at PostgresQueryRunner.query (/usr/src/app/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:211:19)
immich_microservices       |     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
immich_microservices       |     at async PostgresQueryRunner.executeQueries (/usr/src/app/node_modules/typeorm/query-runner/BaseQueryRunner.js:424:13)
immich_microservices       |     at async PostgresQueryRunner.createTable (/usr/src/app/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:410:9)
immich_microservices       |     at async MigrationExecutor.createMigrationsTableIfNotExist (/usr/src/app/node_modules/typeorm/migration/MigrationExecutor.js:351:13)
immich_microservices       |     at async MigrationExecutor.executePendingMigrations (/usr/src/app/node_modules/typeorm/migration/MigrationExecutor.js:129:9)
immich_microservices       |     at async DataSource.runMigrations (/usr/src/app/node_modules/typeorm/data-source/DataSource.js:260:35)
immich_microservices       |     at async DataSource.initialize (/usr/src/app/node_modules/typeorm/data-source/DataSource.js:148:17)

Restarting the docker-compose seems to allow things to work.

The OS that Immich Server is running on

Ubuntu 20.04

Version of Immich Server

v1.66.1

Version of Immich Mobile App

none

Platform with the issue

  • [X] Server
  • [ ] Web
  • [ ] Mobile

Your docker-compose.yml content

version: "3.8"

services:
  immich-server:
    container_name: immich_server
    image: ghcr.io/immich-app/immich-server:${IMMICH_VERSION:-release}
    command: [ "start.sh", "immich" ]
    volumes:
      - ${UPLOAD_LOCATION}:/usr/src/app/upload
    env_file:
      - .env
    depends_on:
      - redis
      - database
      - typesense
    restart: always

  immich-microservices:
    container_name: immich_microservices
    image: ghcr.io/immich-app/immich-server:${IMMICH_VERSION:-release}
    command: [ "start.sh", "microservices" ]
    volumes:
      - ${UPLOAD_LOCATION}:/usr/src/app/upload
    env_file:
      - .env
    depends_on:
      - redis
      - database
      - typesense
    restart: always

  immich-machine-learning:
    container_name: immich_machine_learning
    image: ghcr.io/immich-app/immich-machine-learning:${IMMICH_VERSION:-release}
    volumes:
      - ./data/model-cache:/cache
    env_file:
      - .env
    restart: always

  immich-web:
    container_name: immich_web
    image: ghcr.io/immich-app/immich-web:${IMMICH_VERSION:-release}
    env_file:
      - .env
    restart: always

  typesense:
    container_name: immich_typesense
    image: typesense/typesense:0.24.1@sha256:9bcff2b829f12074426ca044b56160ca9d777a0c488303469143dd9f8259d4dd
    environment:
      - TYPESENSE_API_KEY=${TYPESENSE_API_KEY}
      - TYPESENSE_DATA_DIR=/data
    logging:
      driver: none
    volumes:
      - ./data/tsdata:/data
    restart: always

  redis:
    container_name: immich_redis
    image: redis:6.2-alpine@sha256:70a7a5b641117670beae0d80658430853896b5ef269ccf00d1827427e3263fa3
    restart: always

  database:
    container_name: immich_postgres
    image: postgres:14-alpine@sha256:28407a9961e76f2d285dc6991e8e48893503cc3836a4755bbc2d40bcc272a441
    env_file:
      - .env
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_USER: ${DB_USERNAME}
      POSTGRES_DB: ${DB_DATABASE_NAME}
      PG_DATA: /var/lib/postgresql/data
    volumes:
      - ./data/pgdata:/var/lib/postgresql/data
    restart: always

  immich-proxy:
    container_name: immich_proxy
    image: ghcr.io/immich-app/immich-proxy:${IMMICH_VERSION:-release}
    environment:
      # Make sure these values get passed through from the env file
      - IMMICH_SERVER_URL
      - IMMICH_WEB_URL
    ports:
      - 2283:8080
    depends_on:
      - immich-server
      - immich-web
    restart: always

Your .env content

DB_HOSTNAME=immich_postgres
DB_USERNAME=postgres
DB_PASSWORD=postgres
DB_DATABASE_NAME=immich
REDIS_HOSTNAME=immich_redis
UPLOAD_LOCATION=/home/ubuntu/immich/data/images
TYPESENSE_API_KEY=XXX
PUBLIC_LOGIN_PAGE_MESSAGE=
IMMICH_WEB_URL=http://immich-web:3000
IMMICH_SERVER_URL=http://immich-server:3001
IMMICH_MACHINE_LEARNING_URL=http://immich-machine-learning:3003

Reproduction steps

1. wget the docker-compose and .env
2. modify the .env
3. run docker-compose up

Additional information

No response

osresearch avatar Jul 11 '23 08:07 osresearch

Do you still have the immich-app_pgdata volume on your machine? I believe for a fresh instance the volumes need to be deleted as well.

ShinasShaji avatar Jul 11 '23 16:07 ShinasShaji

Has this issue been resolved now that you have opened different issues from using the app?

alextran1502 avatar Jul 11 '23 21:07 alextran1502

I can reproduce this with a 100% hit rate on my test instance on commit 2fb85f4a164e588bf70f65b107081bbb4834067b (didn't test any others yet).

EDIT: From discord: Seems to be caused by (too) simultaneous starting of server and microservices both trying to run initial migrations.

oddlama avatar Jul 16 '23 19:07 oddlama

Is this fixed now? How did we fix it?

jrasm91 avatar Sep 04 '23 23:09 jrasm91

same issue

whxciotw avatar Jan 08 '24 02:01 whxciotw