How to Implement CI/CD with Postgres in PostgreSQL

Galaxy Glossary

How do I set up CI/CD for a PostgreSQL database?

Automate PostgreSQL schema changes, tests, and deployments through a CI/CD pipeline for safe, repeatable releases.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

Why add CI/CD to PostgreSQL?

Automated pipelines eliminate manual SQL execution, catch migration errors early, and keep every environment in sync. Teams ship features faster while protecting production data.

Which CI servers work best?

GitHub Actions, GitLab CI, CircleCI, and Jenkins all support PostgreSQL. Choose the platform that already handles your application builds to keep pipelines unified.

How should I structure migration files?

Store SQL in a migrations/ folder. Prefix each script with an ordered number and a short description: 001_create_customers.sql, 002_add_index_orders.sql. The CI runner executes files in lexical order.

What does a GitHub Actions job look like?

Spin up a postgres:15 service container, run migrations with psql, execute unit tests, and persist artifacts. The job fails if any SQL or test returns a non-zero status.

Example workflow file

name: db-ci
on: [push]

jobs:
test:
runs-on: ubuntu-latest
services:
db:
image: postgres:15
env:
POSTGRES_PASSWORD: postgres
ports: ["5432:5432"]
steps:
- uses: actions/checkout@v4
- name: Wait for DB
run: until pg_isready -h localhost -p 5432; do sleep 1; done
- name: Run migrations
run: |
for f in migrations/*.sql; do
psql "$POSTGRES_URL" -f "$f";
done
- name: Run unit tests
run: npm test # or pytest, go test, etc.

How do I handle rollbacks?

Pair every up script with a down script or create reversible SQL using tools like Sqitch. CI verifies that rollbacks succeed on a scratch database before merges.

Can I seed reference data?

Yes. Add seed.sql to load product categories or initial customers. Execute it after structural migrations in the same job so tests run against realistic data.

How are environment variables managed?

Store non-secret defaults in the workflow file and inject secrets—POSTGRES_URL, PGPASSWORD—through the CI platform’s secret manager. Avoid committing credentials to Git.

Best practices checklist

✓ Run migrations on a disposable database every pull request.
✓ Keep schema and application code in the same repo.
✓ Tag releases only after pipeline success.
✓ Back up production before deploying.

Why How to Implement CI/CD with Postgres in PostgreSQL is important

How to Implement CI/CD with Postgres in PostgreSQL Example Usage


-- 004_add_total_amount_trigger.sql
CREATE OR REPLACE FUNCTION update_total_amount() RETURNS TRIGGER AS $$
BEGIN
  NEW.total_amount := (
    SELECT SUM(p.price * oi.quantity)
    FROM OrderItems oi
    JOIN Products p ON p.id = oi.product_id
    WHERE oi.order_id = NEW.id);
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER calc_total
AFTER INSERT OR UPDATE ON Orders
FOR EACH ROW EXECUTE FUNCTION update_total_amount();

How to Implement CI/CD with Postgres in PostgreSQL Syntax


psql [CONNECTION_URI] -f <SQL_FILE>

Options:
  CONNECTION_URI   postgres://user:password@host:port/db
  -v var=value     pass variable to SQL script
  -1               wrap all commands in a single transaction
  -q               run quietly

Example (ecommerce):
psql $POSTGRES_URL -1 -f migrations/003_add_status_to_orders.sql

GitHub Actions sample:
- name: Apply migrations
  run: |
    for f in migrations/*.sql; do
      psql "$POSTGRES_URL" -1 -f "$f";
    done

Common Mistakes

Frequently Asked Questions (FAQs)

Is Docker mandatory for Postgres CI?

No, but using a Docker container guarantees a clean database for every run and avoids host-level conflicts.

How can I version-control large seed data?

Store CSV files in seeds/ and load them with \copy in your pipeline. Compress files or keep them in object storage if they exceed Git limits.

What about zero-downtime migrations?

Break destructive changes into additive steps—add new columns, backfill, switch code, then drop old columns—to avoid locks during deployments.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo