Problem Nagios check_postgres.pl custom query - plugins

I have a problem, I launch the query from the local nagios server and I get an invalid format error, can you tell me the reason for this error?
/check_postgres.pl -H lab --p 5432 -db test --action=custom_query \
--query="select max_conn,used,res_for_super,max_conn-used-res_for_super \
res_for_normal from (select count(*) used from pg_stat_activity) t1, (select setting::int res_for_super from pg_settings where name='superuser_reserved_connections') t2, (select setting::int max_conn from pg_settings where name='max_connections') t3;" \
--valtype=string -w 5 -c 10 --timeout 100
This is the error that appears
POSTGRES_CUSTOM_QUERY UNKNOWN: DB "test" (host:lab) Invalid format returned by custom query | time=0.03s used=15;5;10
As you can see it shows me the data but not in nagios readable format. Best regards

Related

KubeCTL with Postgres query doesn't work and throws "LINE 1: select * FROM "employees" where employeeData::text like someSampleHere;"

The field employeeData is of type json.
Consider the query:
select * FROM "employees" where employeeData::text like '%someSampleHere%'
When I'm running the query inside Postgres it works perfect and I get the rows that I'm asking for.
However when I use it with KubeCTL and running this query outside of the PG App
psql -d employess_db -U postgres -c 'select * FROM "employees" where employeeData::text like 'someSampleHere';'
PG throws
ERROR: syntax error at or near "%"
LINE 1: select * FROM "employees" where employeeData::text like %someSampleHere%;
How can we fix it ?
Sounds like a quoting problem to me. You neglected to show your actual kubectl command line in your question, but this works for me without any errors:
kubectl exec postgres-pod -- psql -U postgres -d employees_db \
-c "select * from \"employees\" where employeeData::text like '%someSampleHere%'"

Error using select statement in psql command

I try to perform follow command
psql -c '\\COPY (SELECT file_name, status, reported, operator_id, load_dt AT TIME ZONE GMT FROM mytable) TO STDOUT' > myfile
I receive an error
ERROR: column "gmt" does not exist
LINE 1: ...atus, reported, operator_id, load_dt AT TIME ZONE GMT FROM p...
Can this be of help for you? This is on windows machine. Consider that query command might need modification, depending of what your PC operating system is.
psql -U postgres -d aambackend_dev -c "SELECT \"createdAt\"::timestamp without time zone FROM \"User\" limit(2)" -H -o "D:\xxx1.html"
-U postgres - user
-d typeorm - my database to which i want to connect
-c ... - my query command
-H format html, -o output file
For more info you can refer to: https://www.postgresql.org/docs/13/app-psql.html
Note 1: depending of column i want to select, for majority of columns i don't need escape backslash character. Only for columns of type timestamp with/out time zone i needed to add backslash for quotes.
Note 2: semicolon at the end was not needed in this case (in some other commands and cases it is necessary)
This is my exported file:
If you wish CSV format:
psql -U postgres -d aambackend_dev -c "SELECT "email",\"createdAt\"::timestamp without time zone FROM \"User\" limit(2)" -o "D:\xx2212.csv"

Why can't non-superuser see data in stl_load_errors in Redshift?

The Amazon Redshift documentation for stl_load_errors states that "This table is visible to all users." However, I get different results when querying as a superuser (936 rows) vs a non-superuser (0 rows). Why does the query run as the non-superuser return 0 rows?
Here are the queries I ran.
Superuser:
$ psql -U masteruser -h XX.XX.XX.XX -p 5439 bi -w -c "select count(*) from stl_load_errors"
count
-------
936
(1 row)
Non-superuser:
$ psql -U emonsen -h XX.XX.XX.XX -p 5439 bi -w -c "select count(*) from stl_load_errors"
count
-------
0
(1 row)
Furthermore I can use HAS_TABLE_PRIVILEGE to show that Redshift thinks "emonsen" has the correct privileges on stl_load_errors:
$ psql -U masteruser -h XX.XX.XX.XX -p 5439 bi -w -c "select has_table_privilege('emonsen', 'stl_load_errors', 'select')"
has_table_privilege
---------------------
t
(1 row)
the solution here is to be granted with the SYSLOG ACCESS permissions. Here is the documentation: AWS - Visibility of Data in System Tables and Views
Any user can see all the stl_load_errors results of other users by granting access to syslog as shown below here.
alter user username SYSLOG ACCESS { UNRESTRICTED }
You are right! This table is visible to all users, but the catch here is Masteruser can see all the load errors across all users.
Whereas other users can see their own error logs only. In your case 'emonsen' can see his own error logs only, as in load error generated by 'emonsen'. Nothing wrong with privileges

error syntax ( execute psql query from shell script )

i got this query want to to be executed remotely on my 2nd server and
#!/bin/bash
QUERY=`psql -h my2ndserverip -d testdb -U testuser 'select count(*) as result
from testdb.user where last_logged_date > (clock_timestamp() -interval '90 minutes)
echo "users = $QUERY" > tmp.txt
any tips to fix syntax ?
Use a here document (heredocuments preserve quotes AND allow shell-variable subtitution, as illustrated by the parameter 90 which is used inside single quotes):
#!/bin/bash
limit=${1:-90}
psql -h my2ndserverip -d testdb -U testuser -t -A <<EOFEOF > tmp.txt
SELECT count(*) AS result
FROM "user"
WHERE last_logged_date > clock_timestamp()-interval '${limit} minutes'
;
EOFEOF
exitcode=$?
result=`cat tmp.txt`
echo "Limit=${limit} Exitcode=${exitcode} Result=${result}"
#Eof
I suppose you want psql to omit the column headers etc, so I added the -t -A flags to the psql commandline.
BTW I changed from testdb.user, to FROM user, I don't think you have a schema named 'testdb'.
there are more than one issue
instead quotes in SQL query, you can use $$
postgres=# select interval $$90 min$$;
interval
──────────
01:30:00
(1 row)

Store PostgreSQL query result to Shell or PostgreSQL variable

For instance, I have a table stores value:
select * from myvalue;
val
-------
12345
(1 row)
How can I save this 12345 into a variable in postgresql or shell script?
Here's what I tried in my shell script:
var=$(psql -h host -U user -d db <<SQLSTMT
SELECT * FROM myvalue;
SQLSTMT)
but echo $var gives me:
val ------- 12345 (1 row)
I've also tried
\set var (select * from myvalue)
in psql and when I type \set it lists:
var = '(select*frommyvalue)'
No, no, no! Use "raw data" switch from psql, like "-t" or "\t" and pipe the query to psql instead of parsing ascii-table, come on :-)
echo 'select * from myvalue;' | psql -t -h host -U user -d db
If you really need parse psql output, you could also use -H switch ( turns on HTML output ), and parse it with some perl module for parsing html tables, I used that once or twice.. Also, you may want to use a pgpass file and ~/.psqlrc for some defaults, like default DB to connect, when not specified.
psql has a -c/--command= option to accept SQL from the command line, and -t/--tuples-only option to control output formatting.
$ psql -c 'select 1+1'
?column?
----------
2
(1 row)
$ psql -t -c 'select 1+1'
2
$ VALUE=`psql -t -c 'select 1+1'`
$ echo $VALUE
2
var=`psql -Atc "select 1;"`
echo $var
1
In this answer I explain one way to do it, using a co-process to communicate back-and-forth with psql. That's overkill if all you need is to run a query and get a single result, but might be good to know if you're shell scripting with psql.
You can filter the result you get with your psql command:
var=$(psql -h host -U user -d db <<SQLSTMT
SELECT * FROM myvalue;
SQLSTMT)
var=$(cut -d' ' -f3 <<<$var)
None of these worked for me, but this did:
median_avm=psql "host=${dps1000} port=#### dbname=### user=${reduser} password=${redpass}" -c "SELECT AVG(column) FROM db.table;" -t
using a source file with ${dps1000}, ${reduser}, ${redpass} defined and manually entering port and dbname