django-dbbackup doesn't work. "[Errno 2] No such file or directory: 'pg_dump'"

In my Django project, I installed django-dbbackup to backup my database.

When I type the command “docker-compose exec web python manage.py dbbackup”,
it gives me an error that says “[Errno 2] No such file or directory: ‘pg_dump’”.

Does anyone know what’s the problem?

Here are the configurations of my project.
*I use docker and containerize each app.

*************** docker-compose.yml ************************
version: ‘3.8’

volumes:
postgres_volume:

services:
nginx:
image: nginx:1.21.3
ports:
- “8000:8000”
depends_on:
- web
restart: always

db:
image: postgres:13.4
volumes:
- postgres_volume:/var/lib/postgresql/data
environment:
- “POSTGRES_HOST_AUTH_METHOD=trust”
restart: always

web:
build: . #The image gets built by Dockerfile below
volumes:
- .:/code
tty: true
command: uwsgi --socket :8001 --module config.wsgi
expose:
- “8001”
depends_on:
- db
restart: always

*************** Dockerfile ************************

Pull base image

FROM python:3.9

Set environment variables

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

Set work directory

WORKDIR /code

Install dependencies

RUN pip install django==3.1.7
RUN pip install psycopg2==2.8.6
RUN pip install uwsgi==2.0.19.1
RUN pip install whitenoise==5.2.0
RUN pip install django-crispy-forms==1.11.1
RUN pip install django-allauth==0.44.0

#Install DB Backup app
RUN pip install django-dbbackup==3.3.0
RUN pip install postgres-client

Copy project

COPY . /code/

*************** settings.py ************************
INSTALLED_APPS = [
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘django.contrib.sites’,

#DB backup
'dbbackup',

]

DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.postgresql’,
‘NAME’: ‘aaaaaa’,
‘USER’: ‘bbbbbb’,
‘PASSWORD’: ‘cccccc’,
‘HOST’: ‘db’,
‘PORT’: 5432
}
}

******************** Command Prompt *************************
Command: docker-compose exec web python manage.py dbbackup
Response:(shown as below)

Backing Up Database: postgres
CommandConnectorError: Error running: pg_dump --host=db --port=5432 --username=postgres --no-password --clean postgres
[Errno 2] No such file or directory: ‘pg_dump’
File “/usr/local/lib/python3.9/site-packages/dbbackup/utils.py”, line 118, in wrapper
func(*args, **kwargs)
File “/usr/local/lib/python3.9/site-packages/dbbackup/management/commands/dbbackup.py”, line 61, in handle
self._save_new_backup(database)
File “/usr/local/lib/python3.9/site-packages/dbbackup/management/commands/dbbackup.py”, line 74, in _save_new_backup
outputfile = self.connector.create_dump()
File “/usr/local/lib/python3.9/site-packages/dbbackup/db/base.py”, line 78, in create_dump
dump = self._create_dump()
File “/usr/local/lib/python3.9/site-packages/dbbackup/db/postgresql.py”, line 38, in _create_dump
stdout, stderr = self.run_command(cmd, env=self.dump_env)
File “/usr/local/lib/python3.9/site-packages/dbbackup/db/postgresql.py”, line 21, in run_command
return super(PgDumpConnector, self).run_command(*args, **kwargs)
File “/usr/local/lib/python3.9/site-packages/dbbackup/db/base.py”, line 156, in run_command
raise exceptions.CommandConnectorError(

Traceback (most recent call last):
File “/usr/local/lib/python3.9/site-packages/dbbackup/db/base.py”, line 146, in run_command
process = Popen(cmd, stdin=stdin, stdout=stdout, stderr=stderr, env=full_env)
File “/usr/local/lib/python3.9/subprocess.py”, line 951, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File “/usr/local/lib/python3.9/subprocess.py”, line 1821, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: ‘pg_dump’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/code/manage.py”, line 22, in
main()
File “/code/manage.py”, line 18, in main
execute_from_command_line(sys.argv)
File “/usr/local/lib/python3.9/site-packages/django/core/management/init.py”, line 401, in execute_from_command_line
utility.execute()
File “/usr/local/lib/python3.9/site-packages/django/core/management/init.py”, line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File “/usr/local/lib/python3.9/site-packages/django/core/management/base.py”, line 330, in run_from_argv
self.execute(*args, **cmd_options)
File “/usr/local/lib/python3.9/site-packages/django/core/management/base.py”, line 371, in execute
output = self.handle(*args, **options)
File “/usr/local/lib/python3.9/site-packages/dbbackup/utils.py”, line 118, in wrapper
func(*args, **kwargs)
File “/usr/local/lib/python3.9/site-packages/dbbackup/management/commands/dbbackup.py”, line 61, in handle
self._save_new_backup(database)
File “/usr/local/lib/python3.9/site-packages/dbbackup/management/commands/dbbackup.py”, line 74, in _save_new_backup
outputfile = self.connector.create_dump()
File “/usr/local/lib/python3.9/site-packages/dbbackup/db/base.py”, line 78, in create_dump
dump = self._create_dump()
File “/usr/local/lib/python3.9/site-packages/dbbackup/db/postgresql.py”, line 38, in _create_dump
stdout, stderr = self.run_command(cmd, env=self.dump_env)
File “/usr/local/lib/python3.9/site-packages/dbbackup/db/postgresql.py”, line 21, in run_command
return super(PgDumpConnector, self).run_command(*args, **kwargs)
File “/usr/local/lib/python3.9/site-packages/dbbackup/db/base.py”, line 156, in run_command
raise exceptions.CommandConnectorError(
dbbackup.db.exceptions.CommandConnectorError: Error running: pg_dump --host=db --port=5432 --username=postgres --no-password --clean postgres
[Errno 2] No such file or directory: ‘pg_dump’

The executable file, pg_dump, would be part of the PostgreSQL suite of tools. It’s neither a Django nor a Python program. (In the Ubuntu world, it’s loaded as part of the postgresql-client-common package.)
Since you’re trying to run this program from your web component, you’ll need to ensure that that package is available in it.

Dear KenWhitesell,

Thank you so much for teaching that information!
I tried executing the command from the container db and it worked!
Of course, I changed a lot of configurations but basically, I understood that I had to execute the command from the container that is hosting the database.

I thank you so much for your help.

That’s actually not an accurate conclusion.

You can run that command:

  • From any container that has it installed,
  • From the host, outside of any container,
    or
  • From a different host, provided it has network connectivity to the PostgreSQL instance.

(In all these situations, PostgreSQL would need to be configured with the appropriate users and settings. But you are not restricted to running the command in that container.)