GitHub Action that backs up your PostgreSQL database weekly and stores it as a GitHub artifact.
name: Postgres DB Backup
on:
schedule:
# Every Sunday at 6am UTC
- cron: '0 6 * * 0'
workflow_dispatch:
jobs:
backup:
runs-on: ubuntu-latest
steps:
- name: Install PostgreSQL client
run: sudo apt-get update && sudo apt-get install -y postgresql-client
- name: Create backup
env:
DIRECT_URL: ${{ secrets.DIRECT_URL }}
run: |
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
pg_dump "$DIRECT_URL" \
--format=custom \
--no-owner \
--no-privileges \
--file="backup_${TIMESTAMP}.dump"
echo "BACKUP_FILE=backup_${TIMESTAMP}.dump" >> $GITHUB_ENV
- name: Upload backup artifact
uses: actions/upload-artifact@v4
with:
name: db-backup-${{ github.run_id }}
path: ${{ env.BACKUP_FILE }}
retention-days: 90How to use
Copy to .github/workflows/db-backup.yml in your repo. Add a DIRECT_URL secret with your Postgres connection string. Runs automatically every Sunday at 6am UTC.
A scheduled GitHub Actions workflow that runs pg_dump against your PostgreSQL database every Sunday at 6am UTC. Saves the backup as a custom-format dump file, uploads it as a GitHub artifact with 90-day retention. Can also be triggered manually. Uses DIRECT_URL secret for the connection string.
1 file included