---
title: Monitor PostgreSQL with Custom PSQL Scripts and Remote Actions in Nodinite
description: Monitor PostgreSQL with custom PSQL scripts in Nodinite. Execute business logic, set alerts, and automate management with state-based monitoring.
---

# :fa-database: Managing PostgreSQL Statements

Unlock the full potential of your PostgreSQL environments with the :Nodinite: [Database Monitoring Agent][]. This page shows you how to leverage custom PSQL scripts for proactive monitoring, instant alerts, and self-service management—empowering both integration experts and business users.

* ✅ Monitor PostgreSQL health and business data using your own PSQL logic  
* ✅ Instantly receive alerts and automate remediation with [Remote Actions][]  
* ✅ Empower self-service for business users to execute pre-defined scripts  
* ✅ Centralize monitoring and management for all your PostgreSQL resources  

>Bring your own PSQL logic and :Nodinite: will monitor and alert you whenever there is a problem detected!

To use this feature, provide **AdHoc** PSQL statements with your business logic as described in the examples on this page. As for SQL Server, a PostgreSQL [SQL Statement][SQL Statements] holds one connection and a list of monitoring statements on its **Monitoring Statements (n)** tab. The [Database Monitoring Agent][Database Monitoring Agent] creates one [Resource][Resources] for each enabled monitoring statement. Each monitoring statement returns its evaluated state, which you can monitor in [Monitor Views][] and push external alerts using any of the [Alarm Plugins][].  

PostgreSQL PSQL Statements are grouped by the [Category][Categories] **PostgreSQL - SQL Statements**.

!['PostgreSQL - SQL Statements' Category][0]  
*Example: Monitor View displaying PostgreSQL SQL Statement resources grouped by category.*  

* Your custom-built **PostgreSQL - SQL Statements** are listed in :Nodinite: as [Resources][], where the **Display Name** of the monitoring statement becomes the [Resource][Resources] name. It must be unique within the configuration, also across its SQL Statements.

<div class="mermaid">
graph LR
    subgraph "Configuration"
        c["fal:fa-code 1 SQL Statement"] --> m["fal:fa-traffic-light n monitoring statements"]
        m --> r[fal:fa-lightbulb n Resources]
    end
</div>

*Each enabled monitoring statement is manifested as a [Resource][Resources]. The tabs and validation work as described on [SQL Statements][SQL Statements], with the differences under [Monitoring Statements tab](#fa-traffic-light-monitoring-statements-tab).*

## :fa-key: What are the key features for Monitoring SQL Statements?

* **State Evaluation** – Monitor and evaluate the state based on your own PSQL logic. See PSQL/Stored procedure code examples further down in this guide.  
* **Actions** – Support for the execution of [Remote Actions][]

### What is evaluated for SQL Statements?

The different possible evaluated states for your custom coded SQL Statements are provided in the table below:

|Icon|State|Status|Description|:fa-bolt: Actions|  
|:---:|---|---|---|---|  
|:fa-times-octagon:|Unavailable|**Resource not available**|Evaluation of the 'SQL Statement' is not possible either due to network or security-related problems|Review [prerequisites][Prerequisites for Database Monitoring Agent]|  
|:fa-times-circle:|Error|**Error state raised**|SQL Statement reports that one or more major problems/errors were detected by raising an exception|:fa-edit: [Edit](#fa-edit-edit-sql-statement)|  
|:fa-exclamation-triangle:|Warning|**Warning state raised**|SQL Statement reports that one or more minor problems were detected by raising an exception with state warning|:fa-edit: [Edit](#fa-edit-edit-sql-statement)|  
|:fa-circle-check:|OK|**Online**|SQL Statement is valid, executes and reports that no problems were detected|:fa-edit: [Edit](#fa-edit-edit-sql-statement)|  

>[!TIP]
>The **evaluated state** may be reconfigured using the [Expected State][] functionality on every [Resource][Resources] within :Nodinite:.

## :fa-file: How do I add my own SQL Statements?

The [Database Monitoring Agent][] automatically monitors each configured **SQL Statement**. You can add new and modify existing **SQL Statements** using the [Remote Configuration][] dialogue for the [Database Monitoring Agent][].

### :fa-globe: Global configuration

From the **PostgreSQL databases** tab, configure global settings. Then click on the **Size check** tab.

A :Nodinite: Administrator can add new SQL Statements from the [Remote Configuration][] dialogue, as detailed in [Add or manage a Monitoring Agent][Add or manage Monitoring Agent].  

From the **PostgreSQL statements** tab for the **PostgreSQL Database Connection configuration**, click the :fa-square-plus: **Add** button.  
>Any number of SQL Statements can be provided, but make sure that each one is fast to execute or you may end up hogging the agent and probably resulting in various timeouts.

![Add button][3]  
*Example: Add button location for creating new PostgreSQL SQL Statement configurations.*  

Expand the accordion to edit available properties.

![SQL Statements configuration][4]  
*Example: PostgreSQL SQL Statement configuration properties panel.*

### :fa-traffic-light: Monitoring Statements tab

:new7.13:  
A SQL Statement of a **PostgreSQL Database** configuration has the tabs **General**, **Monitoring Statements (n)**, **User Action Scripts (n)**, and **Authentication**. The **Monitoring Statements** tab lists one accordion per monitoring statement; click :fa-square-plus: **Add** for another one.  
![PostgreSQL Database configuration Contoso PostgreSQL with a SQL Statement on its Monitoring Statements (2) tab, the monitoring statements Contoso Open Orders and Contoso Failed Orders, and the Add button (7.13)][5]  
*The **Monitoring Statements** tab of a PostgreSQL SQL Statement: the same list as for SQL Server.*

The fields and the save rules are the ones of [SQL Server][SQL Statements Monitoring Statements tab], with two differences:

* The **Authentication** tab has only the [Connection String][SQL Connection Strings]; there is no impersonation.
* Every monitoring statement needs an **Execute Command**, also a disabled one. On SQL Server, only the enabled monitoring statements of an enabled SQL Statement need one.

A **Display Name** used twice in one configuration, also across its SQL Statements, stops the save (*PSQL-DUPLICATE-003*).

The properties below describe one monitoring statement.

You can manage the following properties:

* :fa-square-plus: **Enabled**  
* :fa-box-open: **ApplicationId**  
* :fa-font: **Display Name**  
* :fa-play: **Command Type**  
* :fa-code: **Command Text**  
* :fa-file-alt: **Description**

#### :fa-square-plus: Enabled

Flag to set if monitoring for this configuration is enabled or not.

##### :fa-box-open: ApplicationId

Select the Id of which [Application][Applications] to associate this [Resource][Resources] (SQL Statement configuration) with.

##### :fa-font: Display Name

You can manage the user-friendly **Name**. This setting must be unique for each SQL Statement configuration.

##### :fa-play: Command Type

The **Type** of 'SQL Command', select one of the following:

* **Text** – For AdHoc PSQL Statements

##### :fa-code: Command Text

Enter your **Command text**, see examples further down in this guide.

* :fa-play: **Execute Command** – the SQL command(s) to execute

An AdHoc PSQL query with your own custom logic.

>[!IMPORTANT]
>This feature can be dangerous and the [connection string][PostgreSQL Configuration] used should be configured with a user with read-only access. For password special character escaping, see the [SQL Connection Strings][SQL Connection Strings] guide.

##### :fa-file-alt: Description

**Description** – user-friendly description for SQL Statement resource

## :fa-bolt: Actions

The following **Actions** are currently implemented:

### :fa-edit: Edit SQL Statement

Modify SQL Statement configuration directly from the [Resource][Resources] using [Remote Actions][].

## :fa-save: Save

You must click '**Save**' or '**Save and close**' for changes to persist. The new settings/thresholds are evaluated on the next synchronization.  
![Save and Close buttons][png_SaveAndClose]  
*Example: Save and Save and close button options.*

>[!NOTE]
>Depending on the synchronization interval set for the agent, there might be a delay before the [Web Client][] reflects upon the change. You can force the agent to synchronize the [Configuration][] for all :Nodinite: [Monitoring Agents][].

**Save and close** saves and closes the dialogue.  
**Cancel** closes the dialogue without saving any changes.

___

## :fa-code: Examples

The outcome (State) from the logic in your **PostgreSQL - SQL Statements** is based on whether you raise an exception or not.

Internally, the [Database Monitoring Agent][Database Monitoring Agent] executes the [Execute Scalar][] method. This will pick up data from the first column at the first row, and set this as **Log Text** unless you raised an exception.

>[!WARNING]
>Do make sure your logic does not perform any processing or have side effects. We recommend that all calls should execute within a second. This means you may need to adjust the background process and involve [SQL Jobs][] (or other scheduled background tasks) to perform processing and then the logic here merely gets the end result.

### Select + throw (raiserror)

The LogText presented for the **PostgreSQL Statement** configuration (The [Resource][Resources]) comes from your last SELECT statement (1st row, 1st column) or from the Exception. If you do not select anything the **Log Text** simply becomes empty.

|Return Code|Evaluated State|LogText|  
|---|---|---|  
|No exception raised|**OK**|From SELECT operation (1st column)|  
|Throw exception|**Error**|From HINT text|  
|Throw exception with SQLSTATE '01000'|**Warning**|From HINT text|  

### Text Example

Example of using the SQL Statement with Command Type set to **Text**. This example counts the rows in the actual table. Depending on your logic, different states can be returned.

```sql
DO $$ BEGIN
DECLARE counter integer;
BEGIN
counter:= 0;
 SELECT count(*) into counter FROM public."LogEvents" WHERE "ValidationFailed"=true;
    IF (counter=0) THEN -- OK
		--return query SELECT 'OK';
	ELSEIF (counter<10) THEN -- WARNING
		RAISE EXCEPTION SQLSTATE '01000' 
      	USING HINT = 'Make sure to clean these up manually after inspection of the content within the ValidationFailedText column';
	ELSE -- EXCEPTION
		RAISE EXCEPTION 
      	USING HINT = 'Make sure to clean these up manually after inspection of the content within the ValidationFailedText column';
	END IF;
END;
END $$;
```

___

## :fa-hand-point-right: Next Steps

:fa-display: [Add or manage Monitor View][]  

## :fa-link-horizontal: Related Topics

:fa-shield-halved: [Secure Custom Database Scripts and Actions][]  
:fa-database: [Azure SQL Statements][]  
:fa-database: [SQL Statements][]  
:fa-database: [Database Monitoring Agent][]  
:fa-folder: [SQL Categories][]  
:fa-lightbulb-on: [Resources][]  
:fa-cloud-arrow-down: [Monitoring Agents][]  
:fa-display: [Monitor Views][]  

[0]:{ndm}/Documentation%20Pictures/MonitorAgent/SQL/CategoryPSQLStatements.png
[3]:{ndm}/Documentation%20Pictures/MonitorAgent/SQL/AddNewPSQLStatement.png
[4]:{ndm}/Documentation%20Pictures/MonitorAgent/SQL/ConfigurePSQLStatement1.png
[5]:{ndm}/Documentation%20Pictures/MonitorAgent/Database/SQLStatements_PostgreSQL_Config_MonitoringStatements.png

[Add or manage Monitor View]:{ndr}/Administration/2.%20Monitor%20Management/1.%20Monitor%20Views/Features/Add%20or%20manage%20Monitor%20View.md
[Add or manage Monitoring Agent]:{ndr}/Administration/2.%20Monitor%20Management/2.%20Monitor%20Agents/Features/Add%20or%20manage%20Monitor%20Agent.md
[Alarm Plugins]:{ndr}/Administration/5.%20Settings/1.%20Alarm%20Plugins/Features/What%20is%20an%20Alarm%20Plugin.md
[Applications]:{ndr}/Administration/2.%20Monitor%20Management/5.%20Applications/Features/What%20is%20an%20Application.md
[Azure SQL Statements]:{ndr}/Logging%20and%20Monitoring/Database/Features/SQL%20Statements/Azure%20-%20SQL%20Statements.md
[Categories]:{ndr}/Administration/2.%20Monitor%20Management/4.%20Categories/Features/What%20is%20a%20Category.md
[Configuration]:{ndr}/Logging%20and%20Monitoring/Database/Getting%20started/3.%20Configuration.md
[Database Monitoring Agent]:{ndr}/Logging%20and%20Monitoring/Database/Overview.md
[Execute Scalar]:https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.executescalar?redirectedfrom=MSDN&view=netframework-4.8
[Expected State]:{ndr}/Administration/2.%20Monitor%20Management/3.%20Resources/Features/Expected%20State.md
[Monitor Views]:{ndr}/Administration/2.%20Monitor%20Management/1.%20Monitor%20Views/Features/What%20is%20a%20Monitor%20View.md
[Monitoring Agents]:{ndr}/Administration/2.%20Monitor%20Management/2.%20Monitor%20Agents/Features/What%20is%20a%20Monitor%20Agent.md
[png_SaveAndClose]:{ndm}/Documentation%20Pictures/MonitorAgent/SQL/SaveAndClose.png
[PostgreSQL Configuration]:{ndr}/Logging%20and%20Monitoring/Database/Features/PostgreSQL/Configuration.md
[Prerequisites for Database Monitoring Agent]:{ndr}/Logging%20and%20Monitoring/Database/Getting%20started/1.%20Prerequisites.md
[Remote Actions]:{ndr}/Getting%20Started/3.%20Monitor/Remote%20Actions.md
[Remote Configuration]:{ndr}/Logging%20and%20Monitoring/Database/Getting%20started/3.%20Configuration.md
[Resources]:{ndr}/Administration/2.%20Monitor%20Management/3.%20Resources/Features/What%20is%20a%20Resource.md
[Secure Custom Database Scripts and Actions]:{ndr}/Logging%20and%20Monitoring/Database/Troubleshooting/Secure%20Custom%20Database%20Scripts%20and%20Actions.md
[SQL Categories]:{ndr}/Logging%20and%20Monitoring/Database/Features/Overview.md
[SQL Connection Strings]:{ndr}/Install%20and%20Update/Troubleshooting/SQL%20Server/Connection%20Strings.md
[SQL Jobs]:{ndr}/Logging%20and%20Monitoring/Database/Features/Jobs/Overview.md
[SQL Statements]:{ndr}/Logging%20and%20Monitoring/Database/Features/SQL%20Statements/SQL%20Server%20-%20Statements.md
[SQL Statements Monitoring Statements tab]:{ndr}/Logging%20and%20Monitoring/Database/Features/SQL%20Statements/SQL%20Server%20-%20Statements.md#fa-traffic-light-monitoring-statement-tab
[Web Client]:{ndr}/Getting%20Started/Overview.md