Showing posts with label database automation. Show all posts
Showing posts with label database automation. Show all posts

Sunday, 24 March 2024

How To Call A PostgreSQL Stored Procedure From pgAgent Schedule In pgAge...



🔍 𝐋𝐞𝐚𝐫𝐧 𝐡𝐨𝐰 𝐭𝐨 𝐚𝐮𝐭𝐨𝐦𝐚𝐭𝐞 𝐲𝐨𝐮𝐫 𝐃𝐁 𝐭𝐚𝐬𝐤𝐬 𝐰𝐢𝐭𝐡 𝐩𝐠𝐀𝐠𝐞𝐧𝐭 𝐚𝐧𝐝 𝐏𝐨𝐬𝐭𝐠𝐫𝐞𝐒𝐐𝐋! 🔍

In this comprehensive tutorial, I will guide you through the process of calling a PostgreSQL stored procedure from the pgAgent scheduler using the pgAdmin tool. Automating database tasks can significantly enhance your productivity and ensure your database operations run smoothly without manual intervention.

In this video, you’ll learn:

Introduction to pgAgent and pgAdmin: Understand what pgAgent is, why it’s used, and how it integrates with pgAdmin for automating PostgreSQL tasks.

Setting Up pgAgent: Step-by-step instructions on how to install and configure pgAgent in your PostgreSQL environment.

Creating Stored Procedures in PostgreSQL: Learn how to write and create stored procedures in PostgreSQL that can be called by pgAgent jobs.

Scheduling Jobs with pgAgent: Detailed guidance on setting up pgAgent jobs to call your PostgreSQL stored procedures. This includes configuring job schedules, defining job steps, and managing job execution.

Executing and Managing Jobs in pgAdmin: Practical examples and walkthroughs on how to monitor, manage, and troubleshoot pgAgent jobs within the pgAdmin interface.

Best Practices and Tips: Gain insights into best practices for scheduling jobs, ensuring reliability, and optimizing performance in PostgreSQL using pgAgent.

By the end of this tutorial, you’ll have a solid understanding of how to automate your PostgreSQL database tasks using pgAgent, enhancing your workflow and database management capabilities.

🎥 Watch the full video [here]

If you find this tutorial helpful, please like the video, share it with others who might benefit, and subscribe to my channel for more tech tutorials and database management tips!

🔔 Stay updated with the latest content by clicking the notification bell so you never miss an upload!

Feel free to leave any questions or feedback in the comments section below—I’d love to hear from you and help you with any challenges you’re facing.

PostgreSQL, pgAgent, stored procedure, pgAgent scheduler, pgAdmin, database automation, call stored procedure, PostgreSQL tutorial, job scheduling, database management, automate PostgreSQL, pgAgent jobs, database tasks

PostgreSQL, pgAgent, stored procedure, pgAdmin, database automation, job scheduling, PostgreSQL tutorial, call stored procedure, automate database tasks, database management, pgAgent jobs, pgAgent setup, PostgreSQL stored procedure, database scheduler

Thank you for watching and supporting the channel!


Select * from jobs order by 1 desc;

delete from jobs;

CREATE OR REPLACE PROCEDURE public.job_proc()
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
BEGIN
Insert into jobs (entry_job) values (now());
END;
$BODY$;

Sunday, 12 February 2023

How To Create Triggers In PostgreSQL || How To Create Trigger Function I...


In this comprehensive tutorial, we delve into the world of triggers in PostgreSQL, showing you how to create both triggers and trigger functions from scratch. Triggers are a powerful feature in PostgreSQL that allow you to automatically execute a function when certain events occur on a table, such as INSERT, UPDATE, or DELETE operations.

We start by explaining the concept of triggers and their importance in maintaining data integrity and automating routine tasks. You’ll learn how to define a trigger function, which contains the logic that will be executed when the trigger is fired. From there, we guide you through the process of creating a trigger that links this function to a specific table, ensuring that your database reacts automatically to changes.

The video includes detailed, step-by-step examples, making it easy for you to follow along and implement these techniques in your own PostgreSQL environment. We also cover best practices for managing and optimizing triggers, ensuring that they perform efficiently without negatively impacting your database.

Whether you're a database administrator, developer, or just starting with PostgreSQL, this tutorial will equip you with the knowledge needed to effectively use triggers to automate and secure your database operations.


PostgreSQL triggers, create triggers PostgreSQL, trigger functions PostgreSQL, SQL triggers, database automation, PostgreSQL tutorial, database management, trigger functions examples, PostgreSQL triggers step-by-step, SQL automation


-- Table: public.students

-- DROP TABLE IF EXISTS public.students;

CREATE TABLE IF NOT EXISTS public.students
(
    roll numeric(10,0),
    name character varying(30) COLLATE pg_catalog."default",
    course character varying(30) COLLATE pg_catalog."default"
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.students
    OWNER to postgres;

-- Table: public.students_logs

-- DROP TABLE IF EXISTS public.students_logs;

CREATE TABLE IF NOT EXISTS public.students_logs
(
    roll_old numeric(10,0),
    name_old character varying(30) COLLATE pg_catalog."default",
    course_old character varying(30) COLLATE pg_catalog."default"
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public.students_logs
    OWNER to postgres;


-- FUNCTION: public.student_logs_trg_func()

-- DROP FUNCTION IF EXISTS public.student_logs_trg_func();

CREATE OR REPLACE FUNCTION public.student_logs_trg_func()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF
AS $BODY$
declare
begin
insert into students_logs
(roll_old,name_old,course_old)
values(old.roll,old.name,old.course);
return new;
end;
$BODY$;

ALTER FUNCTION public.student_logs_trg_func()
    OWNER TO postgres;


-- Trigger: student_trg

-- DROP TRIGGER IF EXISTS student_trg ON public.students;

CREATE TRIGGER student_trg
    BEFORE DELETE OR UPDATE 
    ON public.students
    FOR EACH ROW
    EXECUTE FUNCTION public.student_logs_trg_func();

insert into students values (1,'Akram','MCA');

select * from students;
select * from students_logs;

update students set name = 'Akram Sohail' where roll = 1;

update students set name = 'Akram Sohail (Modified)' where roll = 1;

delete from students where roll = 1;


----------------------------------------------------------------------------------------------

Triggers are special types of functions that are called/invoked/executed/performed automatically as per the trigger event declaration. A trigger is usually set before or after updating, deleting, insert DML statements on a table. That means, whenever a record is to be inserted, updated, or deleted, based on the time of execution which can be before or after, the trigger executes. Generally, the execution of the Trigger is called “Trigger Fired”.

In PostgreSQL, to create a trigger and make it usable, the sequence of object creation will be:
•   Create a Table on which a trigger to be implemented
•   Create another table to keep logs, so we can see the Trigger work
•   Create a Trigger Function that holds the trigger
•   Create the Trigger Function and Define the Trigger Logic
•   Perform Insert/Update/Delete Operations on the Table

Sunday, 17 July 2022

How To Call A Stored Procedure From A Schedule Job Using pgAgent Jobs In...


In this tutorial, we dive into the world of automating PostgreSQL tasks by calling a stored procedure from a scheduled job using pgAgent in PostgreSQL. PgAgent is a powerful job scheduling tool that integrates seamlessly with PostgreSQL, allowing you to automate repetitive tasks such as running stored procedures, backups, and more.

We begin by guiding you through the setup of pgAgent in pgAdmin, ensuring that you have the necessary environment to start automating your database operations. Then, we move on to creating a stored procedure that performs a specific task within your database. This stored procedure will later be triggered automatically by a scheduled job.

The tutorial provides a step-by-step demonstration of how to create and configure pgAgent Jobs. You’ll learn how to set up schedules, define the tasks that need to be performed, and link these tasks to your stored procedures. We also discuss the various options available within pgAgent, such as setting execution frequency, handling job success or failure, and logging the job outcomes.

Additionally, we explore practical use cases where automating stored procedure execution can significantly enhance your database management strategy. Whether it’s for regular maintenance tasks, complex data manipulations, or timed reports, pgAgent Jobs provide a reliable and efficient solution.

By the end of this video, you'll have a solid understanding of how to use pgAgent to automate stored procedure calls in PostgreSQL, helping you streamline your database operations and save time.

PostgreSQL pgAgent jobs, call stored procedure pgAgent, PostgreSQL automation, pgAdmin scheduled jobs, PostgreSQL job scheduler, automate PostgreSQL tasks, PostgreSQL tutorial, pgAgent setup, stored procedures, SQL automation


In this video, we will see how to schedule a stored procedure
to call in pgAgent Jobs

In the last video, we have seen how to schedule a job.
In this video, we will call a stored procedure.

So, let's start.

How To Call A Stored Procedure From A Schedule Job Using pgAgent Jobs In PostgreSQL Database pgAdmin || PostgreSQL pgAgent 



-- Creating a table

CREATE TABLE public.emp
(
    id numeric,
    name character(30),
    salary numeric,
insert_date timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);



-- Now we will create a stored procedure

CREATE OR REPLACE PROCEDURE public.testing_procedure()
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
BEGIN
Insert into emp(id,name,salary) values(1,'Akram Sohail',100);
Insert into emp(id,name,salary) values(2,'Knowledge 360',200);
END;
$BODY$;

CALL public.testing_procedure();

-- When this procedure will be called, two entries will be there in the Emp table.

-- Let's do it through a scheduled job using pgAgent Jobs

-- We have scheduled the procedure to be called every minute, every hour, every day, and all.

-- the First scheduler will be executed at 11:30 PM IST.
-- So, let's forward the video and wait.

Select * from emp;

-- There is no record as of now because the scheduler is not executed by now.
-- waiting...

-- We get two records at 11:30 PM as we can see from a timestamp.
-- Now, again new entries will come at 11:31 PM

-- So, our schedule job is working.
-- All the source codes and notes are in the description.
-- Please subscribe to my channel...Thank you :)




DO $$
DECLARE
    jid integer;
    scid integer;
BEGIN
-- Creating a new job
INSERT INTO pgagent.pga_job(
    jobjclid, jobname, jobdesc, jobhostagent, jobenabled
) VALUES (
    1::integer, 'New_Job'::text, 'This job will be used to call a stored procedure.'::text, ''::text, true
) RETURNING jobid INTO jid;

-- Steps
-- Inserting a step (jobid: NULL)
INSERT INTO pgagent.pga_jobstep (
    jstjobid, jstname, jstenabled, jstkind,
    jstconnstr, jstdbname, jstonerror,
    jstcode, jstdesc
) VALUES (
    jid, 'Step1'::text, true, 's'::character(1),
    ''::text, 'postgres'::name, 'f'::character(1),
    'CALL public.testing_procedure(); -- Calling the stored procedure'::text, 'This is Step1'::text
) ;

-- Schedules
-- Inserting a schedule
INSERT INTO pgagent.pga_schedule(
    jscjobid, jscname, jscdesc, jscenabled,
    jscstart, jscend,    jscminutes, jschours, jscweekdays, jscmonthdays, jscmonths
) VALUES (
    jid, 'Scheduler1'::text, 'The Scheduler'::text, true,
    '2022-07-17 23:30:00+05:30'::timestamp with time zone, '2022-07-31 23:31:00+05:30'::timestamp with time zone,
    -- Minutes
    ARRAY[true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true]::boolean[],
    -- Hours
    ARRAY[true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true]::boolean[],
    -- Week days
    ARRAY[true,true,true,true,true,true,true]::boolean[],
    -- Month days
    ARRAY[true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true]::boolean[],
    -- Months
    ARRAY[true,true,true,true,true,true,true,true,true,true,true,true]::boolean[]
) RETURNING jscid INTO scid;
END
$$;




How To Call A Stored Procedure From A Schedule Job Using pgAgent Jobs,
How To Call A Stored Procedure From A Schedule Job Using pgAgent Jobs In PostgreSQL Database pgAdmin,
PostgreSQL Database pgAdmin,
PostgreSQL,
pgAgent,
Call A Stored Procedure,
Call A Stored Procedure From A Schedule Job,
Stored Procedure From A Schedule Job,
Schedule Job Using pgAgent Jobs,
Call A Stored Procedure,
Call A Stored Procedure From A pgAgent Jobs,
How To Call A Stored Procedure,
PostgreSQL Database,
Knowledge 360,
Akram Sohail,
Postgres,
database,
stored procedure,
procedure

Sunday, 12 June 2022

How To Add/Install pgAgent Jobs In Existing PostgreSQL Database | PostgreSQL Tutorials | pgAgent Job

 

In this blog, I will provide the download link to pg_Agent for the PostgreSQL database. So that you can install it and schedule a job in PostgreSQL.

 


In this comprehensive tutorial, we dive into the process of adding and installing pgAgent Jobs in an existing PostgreSQL database, a vital tool for automating and scheduling routine database tasks. PgAgent is an extension for PostgreSQL that allows you to schedule jobs, such as running SQL scripts, performing backups, or conducting maintenance tasks, at specified intervals or times.

The video begins with a detailed walkthrough of installing pgAgent in an existing PostgreSQL setup, covering both Windows and Linux environments. We guide you through the necessary prerequisites, such as ensuring that your PostgreSQL instance is properly configured to support pgAgent and downloading the required packages.

Once the installation is complete, we move on to the configuration phase, where you'll learn how to set up pgAgent within pgAdmin, including creating the necessary database objects and setting up the pgAgent service. We provide clear instructions on how to verify that pgAgent is running correctly and how to troubleshoot any issues that may arise during the installation process.

Next, the tutorial explores how to create, manage, and schedule jobs using pgAgent. You'll learn how to define job steps, set up schedules, and monitor job execution. We also cover best practices for organizing and maintaining your pgAgent Jobs, ensuring that your automated tasks run smoothly and efficiently.

By the end of this video, you'll be equipped with the knowledge to effectively install and utilize pgAgent in your PostgreSQL environment, empowering you to automate various database operations and improve your overall workflow.

pgAgent installation, add pgAgent jobs, PostgreSQL job scheduling, install pgAgent PostgreSQL, PostgreSQL automation, pgAdmin pgAgent, database automation, PostgreSQL tutorial, schedule jobs PostgreSQL, PostgreSQL database management

Sunday, 5 June 2022

How To Install pgAgent Jobs To Schedule A Job In PostgreSQL Database || ...


In this tutorial, we walk you through the complete process of installing pgAgent Jobs to enable job scheduling within a PostgreSQL database. PgAgent is an essential tool for database administrators and developers who want to automate routine tasks such as backups, maintenance, or custom SQL scripts, ensuring that these operations run smoothly without manual intervention.

The video begins with an introduction to pgAgent, highlighting its role in automating PostgreSQL database tasks. We then guide you through the installation process, including how to download and set up pgAgent on both Windows and Linux environments. You'll learn how to configure pgAgent within pgAdmin and ensure that the pgAgent service is properly installed and running.

After the installation, we focus on how to create and schedule jobs using pgAgent. This includes setting up job steps, defining schedules, and managing job execution. We also cover how to monitor your jobs, check for errors, and troubleshoot any issues that might arise during the setup.

Additionally, the video provides best practices for job scheduling to optimize performance and avoid potential conflicts or resource overloads. You'll gain insights into how to use pgAgent effectively to manage your PostgreSQL database operations more efficiently.

By the end of this tutorial, you’ll have the knowledge and skills to install and configure pgAgent in your PostgreSQL database, enabling you to automate a wide range of tasks and improve your database management workflow.

pgAgent installation, schedule job PostgreSQL, PostgreSQL job scheduling, install pgAgent PostgreSQL, PostgreSQL automation, pgAdmin tutorial, database management, automate PostgreSQL tasks, PostgreSQL tutorial, SQL job scheduler