PostgreSQL: How to pass parameters from command line?

前端 未结 7 697
孤独总比滥情好
孤独总比滥情好 2020-12-23 15:42

I have a somewhat detailed query in a script that uses ? placeholders. I wanted to test this same query directly from the psql command line (outside the script

7条回答
  •  执笔经年
    2020-12-23 16:13

    You can use the -v construct e.g

    psql -v v1=12  -v v2="'Hello World'" -v v3="'2010-11-12'"
    

    and then refer to the variables in sql as :v1, :v2 etc

    select * from table_1 where id = :v1;
    

    Please pay attention on how we pass string/date value using two quotes " '...' "

提交回复
热议问题