Automated data dumps from postgres database into .sql using django/python and celery

Viewed 237

I'm trying to automate regular data dumps from my postgres database using django/python. I'm using celery to automate it as a task since I want the data dumps to run regularly - every day or every two days. Currently i'm using sh, here's my code:

from sh import pg_dump

@periodic_task(
    run_every=(crontab(minute='*/2')), #set to 2 min interval for testing
    name="portal_backup",
    ignore_result=True)


def portal_backup():
    filename = "portal_backup.sql"
    with gzip.open(filename) as f:
        pg_dump('-U', 'postgres', '-W', '-F', 'p', 'Portal', _out=f)
    print("backup done")

All of my database information is in my .env file , however when I run my code I get this error:

**sh.ErrorReturnCode_1: 
  RAN: /usr/bin/pg_dump -U postgres -W -F p Portal
  STDOUT:
  STDERR:
Password: 
pg_dump: error: connection to database "Portal" failed: FATAL:  Peer authentication failed for user "postgres"**

I don't know why the authentication failed. If anyone has an better ideas on how I can automate this process through django/python and celery any info would be appreciated.

0 Answers
Related