jueves, 1 de junio de 2017

TIL: pipe postgres output to console

If you use postgres and want to run a query from the console, and react on the output of the console, here are the flags that will make postgres just output the output of the query, without counts or titles, or anything.

psql -A -q -h host1 -U user1 -d database1 -p 5439 -c  "select count(*) from stv_sessions where user_name = 'usertofind'"  -F, -t |
 grep "^0$" || send-me-a-mail "we detected usertofind"

In this case, we're not using '-F,' but it would make the output be a CSV, so you could do fancier things here.

In this case I want to check if a user has an active session. You can put it in a cronjob and leave it running for some time. 

Yes. there are logs for that . 

No hay comentarios: