如何在不重启PostgreSQL服务器的情况下重启pg_cron扩展
Got it, let's break down your two pg_cron-related problems with practical, straightforward solutions—these are super common scenarios once you start relying on pg_cron for scheduled tasks.
pg_cron is built to start automatically alongside your PostgreSQL service, but you need to make sure your configuration is set up correctly to guarantee that. Here's the step-by-step:
- First, check your
postgresql.conffile:- Ensure
shared_preload_librariesincludespg_cron(if you have other preloaded extensions, separate them with commas, e.g.,shared_preload_libraries = 'pg_cron, pg_stat_statements'). - Add or confirm the
cron.database_namesetting, which specifies which database pg_cron operates from (usuallypostgres, likecron.database_name = 'postgres').
- Ensure
- If you modified
postgresql.conf, you’ll need to restart PostgreSQL once—this is mandatory becauseshared_preload_librariesis a restart-only parameter. - Once PostgreSQL is running again, log into the database you set in
cron.database_nameand run this to ensure the extension is active (if it wasn’t already):CREATE EXTENSION IF NOT EXISTS pg_cron; - From now on, every time you start PostgreSQL (including after a system shutdown), pg_cron will launch automatically. Test this with a simple one-minute task:
Wait a minute, then check theSELECT cron.schedule('* * * * *', 'SELECT 1;');cron.job_run_detailstable to confirm it executed successfully.
That pg_cron scheduler shutting down log means you killed the background scheduler process, which stops all cron task management. The good news is you don’t have to restart the entire PostgreSQL server to fix this—here are two reliable methods:
Method 1: Use the built-in restart function (recommended)
If you’re running pg_cron version 1.3 or newer, there’s a handy function to restart the scheduler directly:
- Log into the database specified in
cron.database_namewith a superuser account (likepostgres). - Run this SQL command:
SELECT cron.restart_scheduler(); - Verify success by checking your PostgreSQL logs for a
pg_cron scheduler startedmessage, or create the test task mentioned earlier to confirm tasks are running again.
Method 2: Drop and re-create the extension (fallback)
If the restart function isn’t available in your version, you can drop and re-add the extension—just back up your existing tasks first, since dropping the extension will delete all scheduled jobs:
- Back up your current cron jobs:
Save the output somewhere safe (you’ll need it to re-add tasks later).SELECT * FROM cron.job; - Drop the extension:
DROP EXTENSION pg_cron; - Re-create the extension:
CREATE EXTENSION pg_cron; - Use your backup to re-schedule all tasks with
cron.schedule()commands.
Quick heads-up:
Avoid manually killing the pg_cron scheduler process in the future—it’s meant to run continuously to manage your tasks. If you need to pause tasks temporarily, disable individual jobs with cron.unschedule() instead of terminating the whole scheduler.
内容的提问来源于stack exchange,提问作者Nishant Soni

