Skip to main content

SQL

The postgres component lookup allows you to form components from the records in a Postgres database.

In this example below, we form components from all the tables in the incident_commander database.

postgres-tables.yml
apiVersion: canaries.flanksource.com/v1
kind: Topology
metadata:
name: postgres-tables
namespace: default
spec:
schedule: '@every 30s'
components:
- name: Postgres
type: Table
icon: postgres
lookup:
postgres:
- connection: postgres://postgres:gunners@localhost:5432/incident_commander?sslmode=disable
query: |
SELECT
schemaname || '.' || relname AS table_name,
n_live_tup AS num_rows
FROM
pg_catalog.pg_stat_user_tables
ORDER BY
n_live_tup DESC;
display:
expr: |
results.rows.map(row, {
'name': row.table_name,
'type': "Table",
'properties': [{
"name": "Records",
"headline": true,
"value": double(row.num_rows),
}]
}).toJSON()
FieldDescriptionSchemeRequired
connectionconnection string to connect to the serverstringYes
passwordSet password for authentication using string, configMapKeyRef, or SecretKeyRef.EnvVarYes
usernameSet username for authentication using string, configMapKeyRef, or SecretKeyRef.EnvVarYes
displayTemplate to display query results in text (overrides default bar format for UI)Template
queryquery that needs to be executed on the serverstringYes

Results

The results variable in the template will contain the following fields

FieldDescriptionScheme
rowsstderr from the script[]map[string]any
countexit code of the scriptint

Connection Types

mssql SQL Server

# ...
kind: Topology
spec:
components:
- lookup:
mssql:
connection: mssql://sa:password@localhost:1433/db
# ...

MySQL

# ...
kind: Topology
spec:
components:
- lookup:
mssql:
connection: mysql://root:password@localhost:3306/db
# ...

Postgres

# ...
kind: Topology
spec:
components:
- lookup:
postgres:
connection: postgres://postgres:gunners@localhost:5432/incident_commander?sslmode=disable
# ...