简体   繁体   中英

Using shell variable in psql command

I need some help into sending a shell variable to a psql -c query inside a .sh:

#!/bin/sh

    timeStamp=`date +%s`
    timeStampLast24H= expr $timeStamp - 86400

    psql -U postgres -c "COPY (SELECT atribute FROM table WHERE ts>$timeStampLast24H) TO stdout DELIMITER ';' CSV HEADER; > (...)"

I'm doing this to get only rows from the last day because the person who created the database used rectimestamp as an integer, and normal functions from postgres won't work ( now()-interval 'Given_Period' , for example).

Changing "$timeStampLast24H" by something that makes postgres understand an integer that corresponds exactly to the timestamp in from the last 24h is also ok for me. If I use an exact timestamp in its place, the query works well.

Thanks.

This assignment

timeStampLast24H= expr $timeStamp - 86400

is incorrect.

Notice how it differs from this assignment?

timeStamp=`date +%s`

You added a space after the = and left out the backticks or $() wrapping.

Fix those problems and your issue will likely go away. As it is you are putting no value into your query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM