매일 내 postgreSQL 데이터베이스에서 데이터를 검색하고 우편으로 결과를 보내고 싶습니다. 어떻게 할 수있는 가장 좋은 방법입니까?쉘 스크립트를 사용하여 postgreSQL에서 데이터 가져 오기 및 메일로 보내기
내 SELECT를 실행 한 다음 메일 기능으로 보내는 쉘 스크립트에 대해 생각하지만 SELECT 부분을 수행하는 방법을 모르겠습니다.
#!/bin/sh
todayDate = $(date);
nbUsers = mySelect;
nbPosts = mySelect;
echo "We are ".$date." dans there are ".$nbUsers." users and ".$nbPosts." posts " | mail -s "[DAILY]" [email protected]
수정 (업데이트 코드) :
#!/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" [email protected]
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" [email protected]
가능한 중복 메일이 명령의 출력을 보낼 수 있습니다 (http://stackoverflow.com/questions/18223665/postgresql-query-from- bash-script-as-postgres-user) –