#Set database
database="testdb"#Execute psql commands:#Note: you can also add -h hostname -U username in the below commands.
psql -d $database -c "CREATE TABLE person(no integer, name character varying)"
psql -d $database -c "INSERT INTO person VALUES (1,'Tom'),(2,'Jack'),(3,'Rose')"
psql -d $database -c "SELECT *FROM person"#Assign table count to variable
TableCount=$(psql -d $database -t -c "select count(1) from person")
#Print the value of variable
echo "Total table records is :"$TableCount
2. 赋予执行权限
[postgres@ab50dcf74584 ~]$ chmod +x first.sh
3. 执行
[postgres@ab50dcf74584 ~]$ ./first.sh
INSERT 0 3
no | name
----+------
1 | Tom
2 | Jack
3 | Rose
(3 rows)
Total table records count....: 3