Job scheduling with Postgres: Improve database management with automation

Efficient job scheduling is essential for automating repetitive tasks and ensuring the smooth, uninterrupted operation of a PostgreSQL database. From routine backups to executing stored procedures and SQL scripts, automation reduces manual intervention, minimizes human error and improves data consistency across critical use cases.
You have several options for scheduling tasks in a PostgreSQL environment:
- pg_cron: An extension that runs inside PostgreSQL
- pgAgent: A separate service that stores jobs in PostgreSQL and is managed via pgAdmin
- Linux cron: An OS-level scheduler that runs shell/psql scripts outside PostgreSQL
- Enterprise schedulers: For cross-platform, event-driven orchestration
Here, we’ll look at how each works and when to use them.
What is a database management system?
Before exploring scheduling options, it’s important to understand what a database management system (DBMS) is. A DBMS is software that provides an interface for creating, organizing, accessing and modifying data stored in a database. It simplifies data manipulation through structured commands like SQL statements and supports various administrative functions such as access control, performance tuning and job scheduling.
PostgreSQL, or Postgres, is an advanced open source DBMS with a strong reputation for standards compliance, flexibility and high availability. It supports custom data types, JSON/XML, concurrency control, complex joins and full-text search.
With native support for triggers, background workers and extensions like pg_cron, PostgreSQL is a favorite for developers building scalable applications.
A closer look at PostgreSQL
pgAgent is a dedicated job scheduler for PostgreSQL databases. It integrates with pgAdmin and enables users to run automated jobs using SQL commands, stored procedures or shell scripts. It’s a mature tool for managing jobs like backups, index rebuilds and data processing tasks in on-premises or hybrid environments.
The job scheduler runs as a separate service (daemon) outside the PostgreSQL server. It connects to your database to read job definitions and write logs, while execution and monitoring appear in pgAdmin.
The process for scheduling jobs with Postgres and pgAgent involves several steps, including installing pgAgent on the machine where the DBMS is running. After pgAgent is installed, it needs to be connected to the Postgres database using a client like psql or pgAdmin.
pgAgent is installed separately from PostgreSQL. Install the pgAgent package/binaries for your OS, then run the schema script to create the required tables and functions.
After installing the pgAgent binaries, initialize the pgAgent schema by running the provided SQL file (location can vary by OS/package; examples include /usr/share/pgagent/pgagent.sql or <postgres_share_dir>/pgagent.sql).
\i /path/to/pgagent.sql
Next, start the pgAgent service (daemon) so it can run jobs. For example, on Linux you might run a command like:
pgagent host=<db_host>
dbname=<database_name> user=<db_user>
or use your OS service manager to start pgagent. On Windows, install pgAgent as a service via the installer, then start the service.
After pgAgent has been set up, the most reliable way to create jobs is through pgAdmin:
- In pgAdmin, expand your server —> the database —> pgAgent —> Jobs.
- Right-click Jobs —> Create —> Job. Give the job a Name and set Enabled = Yes.
- Open the Steps tab —> Add a step. Choose Kind = SQL (for database tasks) or Batch/Shell (for OS scripts). Enter your SQL (e.g., SELECT COUNT(*) FROM my_table;).
- Open the Schedule tab —> Add a schedule. Set frequency (e.g., every 15 minutes) and time zone.
- Save. The pgAgent service will execute the job on schedule and write logs you can view under pgAgent —> Jobs —> [Your Job] —> Steps/Logs.
(Direct inserts into pgAgent tables are version-specific and error-prone; pgAdmin enforces the correct structure for jobs, steps and schedules.)
Users can monitor job execution and view job logs with pgAdmin. pgAgent provides a set of tables to store job-related metadata and logs like pga_jobsteplog
and pga_schedule
.
How to schedule jobs with Postgres and cron jobs
Another solution for scheduling and running jobs in Postgres is using the cron job functionality available in Unix operating systems like Linux. Cron is a time-based job scheduler that allows users to automate tasks on a recurring basis. By combining the power of cron with Postgres, jobs can be scheduled that interact with the database.
Cron is a daemon, or a background process that executes non-interactive jobs. A cron file is a simple text file containing commands to run periodically at specific times. The default system cron table, or crontab, config file is /etc/crontab
.
Cron jobs are scheduled by creating a shell script or command-line executable function that performs the desired database operations. This can be done using SQL statements, psql commands or other means of interacting with the database within the script. Once the script is created, cron is configured to execute the new job at specified intervals.To schedule jobs with Postgres using Linux cron, create a shell script that runs your SQL via psql, then register it with crontab. For example, your script might call:
psql "dbname=<database_name> user=<db_user>" -c "SELECT my_function();"
Add an entry with crontab -e like
*/5 * * * * /path/to/script.sh
This approach runs outside PostgreSQL and is managed by the OS.
(Note: The pg_cron extension is a different method that runs inside PostgreSQL. Its usage is covered in the pg_cron section.)
Enterprise-grade scheduling with PostgreSQL
For advanced orchestration needs, especially in hybrid cloud and multi-application environments, RunMyJobs by Redwood offers a fully hosted, cloud-native job scheduler that integrates easily with PostgreSQL.
RunMyJobs supports:
- Event-driven workflows and API-triggered jobs
- Cross-platform scheduling for Linux, Windows, and cloud systems
- Native support for PostgreSQL, MySQL, SQL Server, Oracle, and more
- SLA tracking with real-time alerts via email, SMS, or webhook
- Visual job templates and drag-and-drop design tools
- Seamless automation across SAP, Microsoft and custom applications
With agentless architecture and robust monitoring features, RunMyJobs simplifies enterprise-wide scheduling without the overhead of managing on-premises infrastructure or background workers.