Script to write python dictionaries into a PostgreSQL database. I wrote this to work as a module for a in house monitoring solution in a PostgreSQL/TimeScale database.
This is a JSON file, use it tell where to write:
{
"target_database": [
{
"db": "psqlmetrics",
"host": "localhost",
"user": "postgres",
"port": "50000",
"type": "pgsql12",
"table_prefix": "psqlmetrics.",
"table_suffix": ""
}
]
}
Don't have a password key/value because we have client SSL connection. You should use it too instead of passwords.
from writetarget import target_pgsql as pgsql
cpu_load_metrics = {
"hostname": "prd-superapp-01",
"nr_cores": "4",
"load_pct": 67
}
Dictionary KEYS must match column names in target database.
Dictionary VALUES represent the data to write.
First argument as the Postgres target table name.
pgsql().write('cpu_load',cpu_load_metrics)