Skip to main content

Command Palette

Search for a command to run...

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

Updated
β€’4 min read
How I Automated Neon PostgreSQL Backups Using GitLab CI/CD (Without GitHub Actions)
A
πŸ€– Curious AI enthusiast and Software Engineer with 7+ years of experience. πŸ› οΈ I enjoy getting my hands dirty with new technologies β€” experimenting, building side projects, and learning by doing. πŸš€ Currently exploring how AI can enhance real-world web applications.

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:

  1. Uses a PostgreSQL Docker image

  2. Executes pg_dump

  3. Creates a SQL backup

  4. 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:

  1. GitLab CI generates automated SQL dumps.

  2. Neon provides point-in-time recovery and database branching.

  3. Before major releases, I create a backup branch in Neon.

  4. 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.