|
| 1 | +### docker-postgres-pg-cron |
| 2 | + |
| 3 | +This repository builds Docker images that package PostgreSQL with the pg_cron extension across multiple base OS variants, and publishes multi-arch images for both amd64 and arm64. |
| 4 | + |
| 5 | +#### What you get |
| 6 | +- **PostgreSQL + pg_cron**: pg_cron is built/installed and ready to enable per database. |
| 7 | +- **Base OS variants**: Alpine (including specific minor versions) and Debian Trixie. |
| 8 | +- **Multi-arch**: Images are pushed as a manifest list covering amd64 and arm64; Docker automatically pulls the right image for your platform. |
| 9 | + |
| 10 | +#### Variants (by Dockerfile in this repo) |
| 11 | +- `Dockerfile.alpine` |
| 12 | +- `Dockerfile.alpine3.21` |
| 13 | +- `Dockerfile.alpine3.22` |
| 14 | +- `Dockerfile.trixie` (Debian Trixie) |
| 15 | + |
| 16 | +Tag names in your registry may follow a pattern like: |
| 17 | +- `postgres-<MAJOR>-alpine` |
| 18 | +- `postgres-<MAJOR>-alpine3.21` |
| 19 | +- `postgres-<MAJOR>-alpine3.22` |
| 20 | +- `postgres-<MAJOR>-trixie` |
| 21 | + |
| 22 | +Replace `<MAJOR>` with the PostgreSQL major version you want (e.g., `16`, `15`). See the GHCR package page for the authoritative list of available tags. |
| 23 | + |
| 24 | +### Quick pull instructions |
| 25 | + |
| 26 | +Images are published to GHCR under `ghcr.io/suda/docker-postgres-pg-cron`. |
| 27 | + |
| 28 | +```bash |
| 29 | +# Alpine latest for a major version (example: Postgres 16 on Alpine) |
| 30 | +docker pull ghcr.io/suda/docker-postgres-pg-cron:16-alpine |
| 31 | + |
| 32 | +# Specific Alpine base version |
| 33 | +docker pull ghcr.io/suda/docker-postgres-pg-cron:16-alpine3.22 |
| 34 | + |
| 35 | +# Debian Trixie variant |
| 36 | +docker pull ghcr.io/suda/docker-postgres-pg-cron:16-trixie |
| 37 | + |
| 38 | +# Older major versions (examples) |
| 39 | +docker pull ghcr.io/suda/docker-postgres-pg-cron:15-alpine |
| 40 | +docker pull ghcr.io/suda/docker-postgres-pg-cron:15-trixie |
| 41 | +``` |
| 42 | + |
| 43 | +Because these are multi-arch images, you do not need to specify architecture; Docker will pull the correct `amd64` or `arm64` image automatically based on your platform. |
| 44 | + |
| 45 | +### Enabling pg_cron in your database |
| 46 | + |
| 47 | +Once the container is running and you are connected to a database, enable the extension: |
| 48 | + |
| 49 | +```sql |
| 50 | +CREATE EXTENSION IF NOT EXISTS pg_cron; |
| 51 | +``` |
| 52 | + |
| 53 | +You can then schedule jobs, for example: |
| 54 | + |
| 55 | +```sql |
| 56 | +SELECT cron.schedule('nightly-vacuum', '0 3 * * *', $$VACUUM$$); |
| 57 | +``` |
| 58 | + |
| 59 | +### Example docker run |
| 60 | + |
| 61 | +```bash |
| 62 | +docker run -d \ |
| 63 | + --name pg-cron \ |
| 64 | + -e POSTGRES_PASSWORD=postgres \ |
| 65 | + -p 5432:5432 \ |
| 66 | + ghcr.io/suda/docker-postgres-pg-cron:16-alpine |
| 67 | +``` |
| 68 | + |
| 69 | +Then connect with your preferred client and enable `pg_cron` as shown above. |
| 70 | + |
| 71 | +### Building locally (optional) |
| 72 | + |
| 73 | +If you want to build locally for testing, you can target a specific Dockerfile: |
| 74 | + |
| 75 | +```bash |
| 76 | +# Alpine 3.22 example |
| 77 | +docker build -f Dockerfile.alpine3.22 -t local/docker-postgres-pg-cron:postgres-16-alpine3.22 . |
| 78 | + |
| 79 | +# Debian Trixie example |
| 80 | +docker build -f Dockerfile.trixie -t local/docker-postgres-pg-cron:postgres-16-trixie . |
| 81 | +``` |
| 82 | + |
| 83 | +Push and tag according to your registry conventions. |
| 84 | + |
| 85 | + |
| 86 | +### Troubleshooting |
| 87 | + |
| 88 | +If you see an error like: |
| 89 | + |
| 90 | +``` |
| 91 | +Jobs must be scheduled from the database configured in cron.database_name |
| 92 | +``` |
| 93 | + |
| 94 | +set the database `pg_cron` should run against by providing PostgreSQL server args via environment variable when starting the container: |
| 95 | + |
| 96 | +```bash |
| 97 | +docker run -d \ |
| 98 | + --name pg-cron \ |
| 99 | + -e POSTGRES_PASSWORD=postgres \ |
| 100 | + -e POSTGRES_ARGS="-c cron.database_name=YOUR_DB" \ |
| 101 | + -p 5432:5432 \ |
| 102 | + ghcr.io/suda/docker-postgres-pg-cron:16-alpine |
| 103 | +``` |
| 104 | + |
| 105 | +Replace `YOUR_DB` with the target database name. Schedule jobs from that database after enabling `pg_cron`. |
| 106 | + |
| 107 | + |
0 commit comments