I set the variable in psql like this
abc=# \set test 'select * from tableName;'
I am using ubuntu operating system in which postgresql installed
When I exit the terminal it becomes lost So for permanent purpose what should I do?
-
If you need to store data in the database, I would highly recommend a table.Jacob H– Jacob H2018-04-30 14:09:39 +00:00Commented Apr 30, 2018 at 14:09
-
No I just create the shortcut for long queries so I set the variables, but when I quit to psql it lost and again I need to set the variable, For permanent purpose what should I do so not need to set the variable again and againuser9479132– user94791322018-04-30 14:18:11 +00:00Commented Apr 30, 2018 at 14:18
Add a comment
|
1 Answer
The best way would be to create a configuration table to store all your parameters.
But, if you are looking to use OS level variables, this is how you could pass Shell variables in psql
[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 options are just to suppress column headers and error output.
To set your variable permanently, put this line in ~/.bashrc , ~/.bash_profile or any appropriate initialisation file for your shell.
export test='select * from tableName;'
If you want to define global variables in pg environment, you may refer this: Is it possible to define global variables in postgresql