PostgreSQL query (postgres_query)
Connects to a PostgreSQL server, runs one read-only query of your own and reports how long it took, how many rows it returned and one value from the first row. Use it for what a person would otherwise check by hand: the number of failed jobs, the age of the newest row, the replication lag a view reports, whether a flag is set.
The same sensor exists for MySQL and MariaDB and for SQL Server. They share their parameters, their channels and their rules, and this page describes all of that; the other two pages say what differs.
What it looks like

The sensor's page after a few polls, counting the client connections of a PostgreSQL 18 server. In the dark theme: the sensor's page. The form for adding it, with its parameters filled in: light, dark.
One kind for each engine
There are three kinds (postgres_query, mysql_query, mssql_query) and not one kind with an engine parameter. The product's other families are cut the same way: the Proxmox, SNMP, SSH and Windows sensors are one kind for each thing they read, and a page, a template and a credential requirement belong to a kind. The engines differ where it matters:
- what a person has to fill in: SQL Server has two kinds of login and PostgreSQL always has a database;
- the default port, the server's own words for its failures, and the types of the values that come back;
- how the query is kept from writing, which is a read-only transaction on PostgreSQL and MySQL and nothing at all on SQL Server (see what keeps it read-only);
- what the page can promise about it: each was run against its own real server, and the verification table in the sensor reference says which.
With one kind, a form would show SQL Server's login choice to people watching PostgreSQL, and a failure in one engine's driver would sit in the same page as another's. The shared part is code (proto/db and sensors/db_query.rs), not a reason to share a name.
Who can attach it
Only administrators can create a postgres_query sensor or change its parameters. An operator can still rename it, pause it and set its limits. A device template cannot attach one, so discovery never proposes it. The query is the administrator's own and it runs with a login the operator cannot see, so it reads whatever that login may read, which is more than the fixed sensors read. Give the sensor an account that can read only what the query needs.
What keeps it read-only
Three layers, none of which replaces the others.
- The account. This is the only protection that cannot be got around. Create a login that may read what the query reads and nothing else; the setup below does that.
- The transaction. The query runs in
BEGIN READ ONLY, so PostgreSQL refuses any write the query tries, including one hidden in a function it calls. It also setsstatement_timeoutandlock_timeoutto the poll's timeout, so the server stops a query the sensor has given up on. - The check. The query is checked when the sensor is saved and again before every poll, and only this form is accepted: one statement that starts with
SELECT,WITH,VALUESorTABLE. Everything else is refused with the reason and never sent: a second statement (a;outside a string, a quoted name or a comment), andINTO, which makes aSELECTwrite a table. Strings, quoted names,$$quotes and comments are read the way PostgreSQL reads them, so;orDELETEinside a string is text.FOR UPDATEis accepted; the read-only transaction refuses it when it runs.
The query is run through a cursor, so PostgreSQL sends the rows as they are asked for in batches of 200, and the sensor stops asking once it has more than max_rows.
Channels
| Key | Name | Unit | Default limits |
|---|---|---|---|
response_ms | Response time | ms | warning above 2000, error above 10000. Connecting, logging in, running the query and reading the rows. |
query_ms | Query time | ms | none. The query alone, from sending it to its last row. |
rows | Rows returned | none | |
value | Value | none. Set limits on the sensor. |
value is the first row's value in the chosen column, as a number: integers, decimals, floats and the booleans PostgreSQL prints (t, f) become numbers (a boolean is 1 or 0). A value that is not a number (text, a date, NULL) leaves the channel unknown. Give it a name and a unit with value_name and value_unit; they show in the channel table, the graphs and the limits editor. With no rows, value is unknown and rows is 0.
The sensor's verdict is Down only when expected is set and the value is not it. Otherwise the limits on the channels decide, as for every sensor.
Parameters
| Parameter | Default | Meaning |
|---|---|---|
query | required | The query, as above, up to 8000 characters. |
database | postgres | The database to open. |
column | the first column | The column to read value from, by name (case does not matter). A name the result does not have is a failure that lists the columns. |
expected | none | The value must equal this: a number (compared as a number, so 5 equals 5.0), a string (compared exactly) or a boolean. Anything else, a NULL, and a query that returns no row, make the sensor Down. |
max_rows | 1000 | The most rows the query may return, 1 to 100000. More is a failure, not a cut-off count: a count that quietly stops at a limit would be wrong. Count in the query (SELECT count(*) ...) when the rows are what you want. |
port | 5432 | TCP port. |
tls | required | required: encrypt, or fail. optional: encrypt when the server offers it. off: never. |
verify_tls | true | Check the server's certificate against the system's trust store. Servers ship self-signed certificates, so turn this off or pin the certificate until you install one of your own. |
tls_fingerprint | none | Trust exactly the certificate with this SHA-256 fingerprint (colon-separated or plain hex, either case) and no other, whatever its chain, dates or name say; verify_tls is then not needed. See pinning a certificate. |
server_name | the device's host name, or its address | The name the certificate is checked against. |
value_name, value_unit | none | The name (up to 80 characters) and unit (up to 16) of the value channel. |
Suggested interval: 300 seconds. Credential: a secret whose value is user:password, assigned as the secret credential of the device or a group above it. The user is everything before the first colon, so the password may contain colons. The credential is never shown or logged. The poll's timeout covers connecting, the login, the query and reading the rows.
Examples
Failed jobs, with a warning above 0 set on the channel:
{
"database": "app",
"query": "SELECT count(*) AS failed FROM jobs WHERE state = 'failed'",
"value_name": "Failed jobs",
"tls": "optional"
}Whether the replica is in recovery, Down when it is not:
{
"query": "SELECT pg_is_in_recovery()",
"expected": true,
"tls_fingerprint": "AB:CD:EF:01:23:45:67:89:AB:CD:EF:01:23:45:67:89:AB:CD:EF:01:23:45:67:89:AB:CD:EF:01:23:45:67:89"
}On the server
CREATE ROLE pwmonitor LOGIN PASSWORD '...';
GRANT pg_monitor TO pwmonitor; -- the statistics views, such as pg_stat_activity
GRANT CONNECT ON DATABASE app TO pwmonitor;
GRANT USAGE ON SCHEMA public TO pwmonitor;
GRANT SELECT ON jobs TO pwmonitor; -- only what the query readspg_hba.conf has to allow the probe's address for that role (hostssl for tls: required). With ALTER ROLE pwmonitor SET default_transaction_read_only = on; the role is read-only for every client, not just this sensor.
Failures
| Message | Meaning |
|---|---|
the server refused the login (...) (kind auth_failed) | Wrong user or password, or pg_hba.conf does not allow this role from this address. |
the database does not exist (...) (kind config) | database names none. |
the query tried to write; the sensor only reads (...) (kind config) | A function the query calls writes. The read-only transaction refused it. |
the account may not read what the query reads (...) (kind config) | Missing SELECT or USAGE. |
PostgreSQL said: ... (SQLSTATE 42...) (kind config) | The query is not valid for this server: a syntax error, a table or column that is not there. |
the query did not finish within N ms and was cancelled by the server (kind timeout) | The query ran into the poll's timeout. Make it cheaper, or raise the sensor's timeout. |
the query returned more than N rows; ... (kind config) | See max_rows. |
the query is refused: ... (kind config) | The query is not a single read-only statement. The sensor was saved before the check existed, or came in with a file that was not checked. |
the server closed the connection (...) (kind protocol) | Often pg_hba.conf requiring TLS while tls is off, or refusing the role from this address. |
the server does not support TLS; ... (kind config) | tls is required and the server has none. |
TLS error: the certificate is not trusted ..., ... which is not the pinned ... (kind protocol) | The certificate rules. The message names the fingerprint the server presented, so it can be compared and pinned. |
cannot connect: connection refused (kind unreachable) | Nothing listens on that port, or a firewall is in the way. |
Verification
Run against real PostgreSQL 18 servers, in a container with TLS and a self-signed certificate and in the development database of the test suite: a value, a count of rows, no rows, NULL, a result larger than one fetch, the row limit, a wrong password, a database that does not exist, a syntax error, a query cut off by the timeout, a refused address, the read-only transaction (the server reports transaction_read_only as on), encryption without verification, refusal of an untrusted certificate, a wrong pin, and the right pin with verify_tls on and off. The query check is tested with a table of queries for each engine, including statements chained with ;, writes hidden in strings, quoted names, comments and dollar quotes, and the escapes of each engine. The sensor's own rules (value, column, expected, NULL, the credential, the parameters) are checked against a scripted client, and the API tests show an operator refused and an administrator accepted.
Not seen: a server behind a connection pooler in transaction mode, scram-sha-256 with channel binding (the plain variant is used inside the encrypted channel), GSSAPI or certificate logins.
Testing it yourself
The live tests in crates/probeward-sensors/tests/db_live.rs use PROBEWARD_TEST_PGQ_ADDR (host:port), PROBEWARD_TEST_PGQ_USER and PROBEWARD_TEST_PGQ_PASSWORD, and fall back to the server of PROBEWARD_TEST_DATABASE_URL, which just ci always has. PROBEWARD_TEST_PGQ_TLS=1 says the server has TLS with a certificate that is not trusted, which turns on the encryption and pinning tests. The tests only run SELECTs of constants and of system views.