How I Automated Neon PostgreSQL Backups Using GitLab CI/CD (Without GitHub Actions)

I wanted an automated way to back up my production database. Most tutorials use GitHub Actions, but my code is hosted on GitLab, so I decided to use GitLab CI/CD instead.
In this guide, I'll show you how to automatically generate PostgreSQL backups from your Neon production database and store them as GitLab artifacts.
Tech Stack
Neon PostgreSQL
Prisma ORM
GitLab Repository
GitLab CI/CD Pipelines
Why Backup Your Database?
No matter how small your application is, accidents happen:
A migration goes wrong
Someone accidentally deletes data
You introduce a bug that corrupts records
You need historical snapshots for recovery
Having backups means you can restore your application without losing everything.
Before We Begin
Make sure you have:
A GitLab repository
A Neon PostgreSQL database
Your production database connection string
Example:
DATABASE_URL=postgresql://user:password@ep-xxxx.ap-southeast-1.aws.neon.tech/neondb?sslmode=require
Step 1: Create .gitlab-ci.yml
Create a new file in the root of your project:
brtmenu/
βββ app/
βββ prisma/
βββ package.json
βββ .gitlab-ci.yml
βββ ...
Step 2: Add the Backup Pipeline
Inside .gitlab-ci.yml:
stages:
- backup
backup_database:
stage: backup
image: postgres:16
script:
- mkdir backups
- pg_dump "$DATABASE_URL" > backups/database.sql
artifacts:
paths:
- backups/database.sql
expire_in: 30 days
only:
- main
What this does:
Uses a PostgreSQL Docker image
Executes
pg_dumpCreates a SQL backup
Stores it as a downloadable GitLab artifact
Step 3: Commit and Push
git add .
git commit -m "Add database backup pipeline"
git push
Step 4: Add Your Database URL to GitLab
Go to:
Project
β
Settings
β
CI/CD
β
Variables
Click:
Add Variable
Key
DATABASE_URL
Value
postgresql://your-neon-production-url
I also recommend enabling:
β Protected
β Masked
This prevents your connection string from being exposed in logs.
Step 5: Run the Pipeline
Go to:
Build
β
Pipelines
β
Run Pipeline
Select:
main
Then click:
Run Pipeline
Step 6: Verify the Backup
If everything succeeds, you'll see:
backup_database β
Open the job and scroll down to:
Artifacts
You'll find:
database.sql
Download it.
Congratulations π
You now have your first automated PostgreSQL backup running on GitLab.
Step 7: Schedule Automatic Backups
Instead of manually running the pipeline every time, you can create a schedule.
Go to:
Build
β
Pipeline Schedules
β
New Schedule
Examples:
Daily Backup
0 0 * * *
Runs every day at midnight UTC.
Weekly Backup
0 0 * * 0
Runs every Sunday.
Choose whatever frequency makes sense for your application.
Restoring a Backup
If you ever need to restore:
psql "$DATABASE_URL" < database.sql
Or if you're restoring to another database:
psql "postgresql://user:password@host/dbname" < database.sql
Optional: Run Prisma Migrations in GitLab
Once backups are working, you can also automate Prisma migrations.
Update your pipeline:
stages:
- backup
- migrate
backup_database:
stage: backup
image: postgres:16
script:
- mkdir backups
- pg_dump "$DATABASE_URL" > backups/database.sql
artifacts:
paths:
- backups/database.sql
expire_in: 30 days
migrate_database:
stage: migrate
image: node:22
script:
- npm ci
- npx prisma migrate deploy
Your deployment flow becomes:
Push Code
β
Backup Production Database
β
Run Prisma Migrations
β
Deploy Application
A Few Notes About GitLab Artifacts
GitLab artifacts are great for automated backups, but I wouldn't rely on them as the only backup strategy.
For extra safety, I use a layered approach:
GitLab CI generates automated SQL dumps.
Neon provides point-in-time recovery and database branching.
Before major releases, I create a backup branch in Neon.
Occasionally, I download important backups and store them elsewhere.
This gives me multiple ways to recover if something goes wrong.
Final Thoughts
GitHub Actions gets most of the attention, but GitLab CI/CD is equally capable of automating PostgreSQL backups.
With only a few lines of YAML, you can:
β Automatically back up your Neon database
β Download SQL dumps directly from GitLab
β Schedule daily or weekly backups
β Integrate backups with your Prisma deployment workflow
This setup is simple, free, and provides enough safety to confidently ship changes to production.





