store postgresql result in bash variable

后端 未结 2 1866
感动是毒
感动是毒 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条回答
  •  猫巷女王i
    2020-12-15 17:08

    Using -t option or --tuples-only will give you the rows only, so it will easier to store them in array variable (if the result from query more than one)

    vartest =(`psql -t -d $dbname -U $username -c "SELECT gid FROM testtable WHERE aid='1';"`)
    echo $vartest
    

    example:

    query result

    ubuntu@ratnakri:~$ psql -h localhost -p 5432 -t -U postgres -d postgres -c "select slot_name from pg_replication_slots"
    barman
    barman2
    

    make it into array variable

        ubuntu@ratnakri:~$ RESULT=(`psql -h localhost -p 5432 -t -U postgres -d postgres -c "select slot_name from pg_replication_slots"`)
        ubuntu@ratnakri:~$ echo ${RESULT[0]}
        barman
        ubuntu@ratnakri:~$ echo ${RESULT[1]}
        barman2
    

提交回复
热议问题