PostgreSQL pg_cron Troubleshooting: cron.timezone, nodename Socket Errors, and Bloat
Fix pg_cron connection refused errors on RDS/Supabase/Cloud SQL, configure cron.timezone safely, prevent duplicate cron.schedule entries, and purge job_run_details.
Incident Symptom & Protocol Root Cause
Production Failure Symptom
Scheduled SQL jobs in PostgreSQL pg_cron fail in cron.job_run_details with 'connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed' or 'connection to localhost refused', duplicate jobs pile up after migrations run cron.schedule(...), or the cron.job_run_details audit table bloats to tens of gigabytes!
Scheduler Parser & Clock Mechanics
pg_cron runs inside the primary database (default postgres), connects via libpq to cron.job.nodename (defaulting to 'localhost' TCP unless cron.host = '' Unix socket is set), evaluates schedules in GMT/UTC regardless of session TimeZone unless cron.timezone is configured in postgresql.conf, and creates a brand-new duplicate jobid when cron.schedule(schedule, command) is called WITHOUT a unique job_name argument!
Broken vs Verified Configuration
-- BUG 1: 2-arg cron.schedule(expr, sql) inserts a NEW row every migration run!
-- Run migrations 5 times -> 5 identical vacuum/rollup jobs running in parallel!
SELECT cron.schedule('*/10 * * * *', $$REFRESH MATERIALIZED VIEW CONCURRENTLY mv_daily_kpis$$);
-- BUG 2: Assuming session SET TIME ZONE affects pg_cron (it ignores session TZ!)
-- BUG 3: Never pruning cron.job_run_details on per-minute jobs (~525k rows/job/year!)-- 1. Always pass the 3-arg named signature cron.schedule(job_name, schedule, command)!
-- Upserts cleanly by job_name instead of creating duplicate rows:
SELECT cron.schedule(
'refresh-mv-daily-kpis',
'*/10 * * * *',
$$REFRESH MATERIALIZED VIEW CONCURRENTLY public.mv_daily_kpis$$
);
-- 2. If targeting another database on managed Postgres (RDS / Supabase / AlloyDB),
-- use cron.schedule_in_database() and fix socket nodename if TCP localhost is disabled:
UPDATE cron.job SET nodename = '' WHERE jobname = 'refresh-mv-daily-kpis';
-- 3. Schedule automatic daily retention cleanup of cron.job_run_details (keep 14 days):
SELECT cron.schedule(
'purge-pg-cron-history',
'25 3 * * *',
$$DELETE FROM cron.job_run_details WHERE end_time < now() - interval '14 days'$$
);Operational Deep Dive
Three operational nuances govern every production pg_cron deployment. First, ALWAYS use the three-argument overload SELECT cron.schedule('unique-job-name', 'expr', $$SQL$$) (or four-argument cron.schedule_in_database). The legacy two-argument cron.schedule('expr', $$SQL$$) generates a fresh integer jobid on every call—if placed inside an idempotent database migration or startup script, it multiplies active background workers on every deploy!
Second, diagnose failed runs immediately by querying SELECT jobid, status, return_message, start_time FROM cron.job_run_details ORDER BY start_time DESC LIMIT 20;. On self-hosted Linux servers or hardened RDS/Cloud SQL instances where TCP loopback authentication rejects passwordless local connections (pg_hba.conf), jobs fail with 'connection to localhost refused' or 'fe_sendauth: no password supplied'. Setting cron.host = '/var/run/postgresql' in postgresql.conf (or UPDATE cron.job SET nodename = '' so libpq uses the local Unix domain socket with trust/peer local auth, or configuring pg_cron.use_background_workers = on in pg_cron 1.4+ to skip libpq sockets completely!) eliminates loopback auth failures.
Third, verify timezone and high-availability failover behavior! pg_cron evaluates cron strings in UTC by default unless cron.timezone is explicitly set in postgresql.conf (requires pg_cron 1.5+). Furthermore, because cron.job and cron.job_run_details are standard replicated PostgreSQL tables inside cron.database_name, standby read replicas suppress the pg_cron launcher automatically until promoted to primary—making failover seamless without split-brain double execution.
PostgreSQL pg_cron Production Checklist
| Engine / Case | Runtime Behavior | Production Remediation |
|---|---|---|
| Preventing duplicate jobs in migrations | 2-arg cron.schedule(expr, cmd) appends duplicate jobs; 3-arg cron.schedule(name, expr, cmd) updates in place. | Always supply explicit unique job_name as the first argument. |
| Loopback TCP / password auth errors | Default libpq connection to nodename='localhost' hits pg_hba.conf password prompts. | Enable cron.use_background_workers = on (max_worker_processes headroom!) or set nodename = ''. |
| Timezone evaluation | Ignores database/role TimeZone; defaults strictly to GMT/UTC. | Keep expressions in UTC or set cron.timezone = 'America/New_York' in DB parameter group. |
| cron.job_run_details disk bloat | Logs every single run forever by default (10 every-minute jobs = 5.25M rows/year). | Schedule daily DELETE FROM cron.job_run_details WHERE end_time < now() - interval '14 days' (or cron.log_run = off). |
Edge Cases & Engineering FAQs
Does pg_cron support sub-minute intervals like every 10 seconds?
Yes, since pg_cron 1.5 you can pass an interval string '10 seconds' (up to '59 seconds') instead of a 5-field cron expression—for example SELECT cron.schedule('fast-outbox', '15 seconds', $$SELECT process_outbox()$$);.
Can two runs of the same pg_cron job overlap if a query takes longer than its schedule interval?
No! A single pg_cron jobid has built-in non-overlapping execution: if job 7 is still running when its next tick arrives, pg_cron skips launching a duplicate session until the active run completes.
Why can't I run CREATE EXTENSION pg_cron inside my application database?
The pg_cron background launcher binds to a single metadata database defined by cron.database_name (almost always postgres on RDS, Supabase, and Cloud SQL). Install the extension in postgres and use cron.schedule_in_database('job-name', '0 * * * *', $$SQL$$, 'my_app_db') to execute SQL inside your application database.
Runtime Dead-Man's-Switch Verification
Dead-man's-switch heartbeat checks that page on-call when a scheduled job skips a DST gap, stalls on a lock, or exits non-zero.