Skip to content

celery cannot connect to postgresql #626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rashidiy opened this issue Jul 5, 2023 · 3 comments
Open

celery cannot connect to postgresql #626

rashidiy opened this issue Jul 5, 2023 · 3 comments

Comments

@rashidiy
Copy link

rashidiy commented Jul 5, 2023

what is wrong?

my postgresql database incide docker

my celery.py

from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'root.settings')

background_worker = Celery('root')
background_worker.conf.broker_connection_retry_on_startup = True
background_worker.config_from_object('django.conf:settings', namespace='CELERY')
background_worker.autodiscover_tasks()
@auvipy
Copy link
Member

auvipy commented Jul 6, 2023

you are trying to use a discontinued package.

@rashidiy
Copy link
Author

rashidiy commented Jul 7, 2023

@auvipy which package discounted?

@amirhosein-kia-darbandsary
Copy link

amirhosein-kia-darbandsary commented Mar 9, 2025

Why is this code (os.environ.setdefault) not enough?

This code only specifies the path to the Django settings, but:

  1. If the database settings in settings.py depend on environment variables (e.g., DB_NAME, DB_USER, etc.), these variables must be present in the environment where Celery is running.
  2. If these variables are not defined in the environment where Celery is running, Celery will not be able to connect to the database.

Final Solution:

  1. Ensure that all required environment variables (e.g., DB_NAME, DB_USER, DB_PASSWORD, etc.) are defined in docker-compose.yml for the celery service.
  2. If you are not using Docker, make sure these variables are defined in the environment where Celery is running (e.g., in the operating system or in a .env file).
  3. If you are using django-environ or similar libraries, ensure that the .env file is loaded correctly.

P.s

I've this problem for the celery-beat too so don't forget.

sample

  celery_worker_web:
    build: .
    container_name: celery_worker_web
    depends_on:
      - web
    environment:
      - CELERY_BROKER_URL=${CELERY_BROKER_URL}
      - CELERY_RESULT_BACKEND=${CELERY_RESULT_BACKEND}
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants