title: Backup PostgreSQL database running in a container on Digital Ocean
body: <p>I'm a newbie to Docker, I'm working on a project that written by another developer, the project is running on Digital Ocean <strong>Ubuntu 18.04</strong>. It consists of 2 containers (1 container for Django App, 1 container for PostgreSQL database).</p>
  <p>It is required from me now to get a backup of database, I found a bash file written by previous programmer:</p>
  <pre><code>### Create a database backup.
  ###
  ### Usage:
  ###     $ docker-compose -f &lt;environment&gt;.yml (exec |run --rm) postgres backup
  set -o errexit
  set -o pipefail
  set -o nounset
  
  
  working_dir=&quot;$(dirname ${0})&quot;
  source &quot;${working_dir}/_sourced/constants.sh&quot;
  source &quot;${working_dir}/_sourced/messages.sh&quot;
  
  
  message_welcome &quot;Backing up the '${POSTGRES_DB}' database...&quot;
  
  
  if [[ &quot;${POSTGRES_USER}&quot; == &quot;postgres&quot; ]]; then
      message_error &quot;Backing up as 'postgres' user is not supported. Assign 'POSTGRES_USER' env with another one and try again.&quot;
      exit 1
  fi
  
  export PGHOST=&quot;${POSTGRES_HOST}&quot;
  export PGPORT=&quot;${POSTGRES_PORT}&quot;
  export PGUSER=&quot;${POSTGRES_USER}&quot;
  export PGPASSWORD=&quot;${POSTGRES_PASSWORD}&quot;
  export PGDATABASE=&quot;${POSTGRES_DB}&quot;
  
  backup_filename=&quot;${BACKUP_FILE_PREFIX}_$(date +'%Y_%m_%dT%H_%M_%S').sql.gz&quot;
  pg_dump | gzip &gt; &quot;${BACKUP_DIR_PATH}/${backup_filename}&quot;
  
  
  message_success &quot;'${POSTGRES_DB}' database backup '${backup_filename}' has been created and placed in '${BACKUP_DIR_PATH}'.&quot;
  
  </code></pre>
  <p>my first question is:<br />
  is that command right? i mean if i ran:</p>
  <pre><code>docker-compose -f production.yml (exec |run --rm) postgres backup
  </code></pre>
  <p>Would that create a backup for my database at the written location?</p>
  <p>Second question: can I run this command while the database container is running? or should I run <strong>docker-compose down</strong> then run the command for backup then run <strong>docker-compose up</strong> again.</p>