store postgresql result in bash variable

后端 未结 2 1863
感动是毒
感动是毒 2020-12-15 16:54

How to atore a scalar postgresql-value on a bash-variable like in script below?

dbname=\"testlauf\"
username=\"postgres\"

vartest=\'psql -c -d $dbname -U $u         


        
2条回答
  •  抹茶落季
    2020-12-15 17:05

    Put the -c option just before its argument - the query. Mind also using the additional -t option to get just the tuple value. And of course, use the backticks (`) operator.

    Using the -X option is also recommended, as sometimes a .psqlrc file might add some redundant output, as well as the -A option, which disables column aligning (whitespaces).

    vartest=`psql -X -A -d $dbname -U $username -h localhost -p 5432 -t -c "SELECT gid FROM testtable WHERE aid='1'"`
    

提交回复
热议问题