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

Search for a command to run...

No comments yet. Be the first to comment.
Meta Description: Learn how to convert your Next.js website into an Android app using Capacitor on Windows. Step-by-step guide with Android Studio setup, APK generation, and deployment. If you've alre

This is Part 2 of my Next.js Caching series. In Part 1, we covered Browser Cache, CDN, Router Cache, Full Route Cache, Data Cache, unstable_cache, and use cache. Previously... We learned that a requ

When someone opens your website, does Next.js immediately query your database? Not always. A request may pass through multiple cache layers before your database is even touched. The more requests serv

Introduction If you've been building with React or Next.js, you've probably heard terms like: SSR SSG ISR CSR PPR React Server Components unstable_cache At first, they all sound like differen

While building My Project, one of the biggest challenges wasn't building the UI—it was deciding how to cache data correctly. I wanted my application to: ⚡ Feel instant while navigating 🔍 Remain SEO

Akash Blog
19 posts
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
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.
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
.gitlab-ci.ymlCreate a new file in the root of your project:
brtmenu/
├── app/
├── prisma/
├── package.json
├── .gitlab-ci.yml
└── ...
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_dump
Creates a SQL backup
Stores it as a downloadable GitLab artifact
git add .
git commit -m "Add database backup pipeline"
git push
Go to:
Project
↓
Settings
↓
CI/CD
↓
Variables
Click:
Add Variable
DATABASE_URL
postgresql://your-neon-production-url
I also recommend enabling:
✅ Protected
✅ Masked
This prevents your connection string from being exposed in logs.
Go to:
Build
↓
Pipelines
↓
Run Pipeline
Select:
main
Then click:
Run Pipeline
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.
Instead of manually running the pipeline every time, you can create a schedule.
Go to:
Build
↓
Pipeline Schedules
↓
New Schedule
Examples:
0 0 * * *
Runs every day at midnight UTC.
0 0 * * 0
Runs every Sunday.
Choose whatever frequency makes sense for your application.
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
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
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.
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.