# 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:

```env
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:

```text
brtmenu/
├── app/
├── prisma/
├── package.json
├── .gitlab-ci.yml
└── ...
```

* * *

# Step 2: Add the Backup Pipeline

Inside `.gitlab-ci.yml`:

```yaml
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

```bash
git add .
git commit -m "Add database backup pipeline"
git push
```

* * *

# Step 4: Add Your Database URL to GitLab

Go to:

```text
Project
↓
Settings
↓
CI/CD
↓
Variables
```

Click:

```text
Add Variable
```

### Key

```text
DATABASE_URL
```

### Value

```text
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:

```text
Build
↓
Pipelines
↓
Run Pipeline
```

Select:

```text
main
```

Then click:

```text
Run Pipeline
```

* * *

# Step 6: Verify the Backup

If everything succeeds, you'll see:

```text
backup_database ✔
```

Open the job and scroll down to:

```text
Artifacts
```

You'll find:

```text
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:

```text
Build
↓
Pipeline Schedules
↓
New Schedule
```

Examples:

### Daily Backup

```text
0 0 * * *
```

Runs every day at midnight UTC.

### Weekly Backup

```text
0 0 * * 0
```

Runs every Sunday.

Choose whatever frequency makes sense for your application.

* * *

# Restoring a Backup

If you ever need to restore:

```bash
psql "$DATABASE_URL" < database.sql
```

Or if you're restoring to another database:

```bash
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:

```yaml
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:

```text
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.
