How can I schedule psql-based scripts for the built-in Postgres database?

Article ID: 314
Category: Database
Applies to: 2.93 and newer
Updated: 2020-11-05

1) Make a pgpass.conf file
First you would want to create a pgpass.conf file to store your postgres connection and password. Open Notepad.exe and paste this:
127.0.0.1:5432:EventSentry:postgres:password
*replace "password" with your postgres account's password
Click File > Save As, choose "All Files" for Save As Type, and for File Name use pgpass.conf and click OK.
Paste your pgpass.conf in a designated location such as C:\temp - do not paste it anywhere user-specific such as C:\users\username since it cannot load on a schedule from user-specific locations. You would also want to make sure that the SYSTEM account has read permission to the pgpass.conf file and the folder that contains the pgpass.conf file.

2) Make an environment variable
In the Windows Control Panel choose System and then click Advanced System Properties. Go to the Advanced tab and then click the Environment Variables button at the bottom. In the bottom pane click the New button. For the top line, use:
PGPASSFILE
For the bottom line, use:
c:\setup\pgpass.conf
You can specify a different folder in accordance with where you pasted pgpass.conf in step 1 but the filename must be pgpass.conf

3) Make your psql script
Open Notepad.exe and paste your psql commands, making sure the last line is \q to close the connection. For example:
SELECT * FROM EVENTSENTRY.ESEVENTLOGCOMPUTER WHERE ID=1
\q
Click File > Save As, and select the same folder where you put pgpass.conf file in step 1, which would be c:\setup in this example. Name your file something without spaces, such as my_psql.txt, which would create c:\setup\my_psql.txt in this example.

4) Make an embedded script in EventSentry
In the EventSentry console toolbar, click Tools > Embedded Scripts. Click the New button and name your script with the .bat extension, for example myscript.bat.
On the right side of the Embedded Scripts editor, paste your psql command:
"C:\Program Files (x86)\EventSentry\postgresql96\bin\psql.exe" --host=127.0.0.1 --port=5432 --username=postgres --dbname=EventSentry -w --file=c:\setup\my_psql.txt
*replace file=c:\setup\my_psql.txt with your psql script name and folder name from step 3
Save your EventSentry configuration before proceeding to step 5

5) Schedule your psql script in EventSentry
On the left side of the EventSentry console, go to Packages > System Health, and either select an existing System Health package that contains an Application Scheduler object, or make a new System Health package and right-click it and choose Add > Application Scheduler. Either way, select the Application Scheduler object inside your System Health package on the left, and on the right click the + button to the right of the scheduled items list to add a new schedule. In the Filename field, choose your embedded script name from step 4, which would be @myscript.bat in this example. Select the scheduling frequency that you prefer and click the Test button to ensure your script completes without errors.

Please note: If you still use PostgreSQL version 9.1, the default location of psql.exe for step 4 is:
"C:\Program Files (x86)\EventSentry\postgresql\bin\psql.exe"




Try EventSentry on-premise

FREE 30-day evaluation

Download Now