我在psql中设置变量就像这样
abc=# \set test 'select * from tableName;'
我正在使用安装了postgresql的ubuntu操作系统
当我退出终端时它会丢失所以为了永久目的,我该怎么办?
答案 0 :(得分:0)
最好的方法是创建一个配置表来存储所有参数。
但是,如果您希望使用操作系统级别变量,那么就可以在psql
中传递Shell变量
[pguser@test ~]$ export test_var="select 'Hello';"
[pguser@test ~]$ psql -A -t -q -d YOURDB -U youruser -p 5432 <<INP
${test_var}
INP
Hello
-A -t -q
选项只是为了抑制列标题和错误输出。
要设置变量永久,请将此行放在~/.bashrc , ~/.bash_profile
或任何适当的shell初始化文件中。
export test='select * from tableName;'
如果要在pg环境中定义全局变量,可以参考:Is it possible to define global variables in postgresql