我想每天从我的postgreSQL数据库中检索数据并通过邮件发送结果,请问最好的方法是什么?
我想一个shell脚本执行我的SELECT然后用mail函数发送它但我不知道怎么做SELECT部分。
#!/bin/sh
todayDate = $(date);
nbUsers = mySelect;
nbPosts = mySelect;
echo "We are ".$date." dans there are ".$nbUsers." users and ".$nbPosts." posts " | mail -s "[DAILY]" test@test.com
编辑(更新后的代码):
#!/bin/sh
todayDate=$(date +%y%m%d%H%M%S)
nbUsers=$(su postgres -c "psql -d database -c 'SELECT COUNT(*) FROM table1'")
nbPosts=$(su postgres -c "psql -d database -c 'SELECT COUNT(*) FROM table2'")
echo "We are $todayDate. There are $nbUsers users and $nbPosts posts." | mail -s "[DAILY] Databse" test@test.com
EDIT2(更新后的代码)
#!/bin/sh
su - postgres
todayDate=$(date +"%d/%m/%y")
todayTime=$(date +"%T")
nbUsers=$(psql -A -t -d database -c "SELECT COUNT(id) FROM table1;")
nbPosts=$(psql -A -t -d database -c "SELECT COUNT(id) FROM table2;")
echo "We are $todayDate. There are $nbUsers users and $nbPosts posts." | mail -s "[DAILY] Databse" test@test.com
答案 0 :(得分:1)
您可以使用postgresql数据库中的psql命令检索数据
nbUsers=`su postgres -c "psql -d databasename -c 'SELECT * FROM tableName'"`
之后,您可以将此命令的输出发送到邮件