0

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?

2
  • If you need to store data in the database, I would highly recommend a table. Commented 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 again Commented Apr 30, 2018 at 14:18

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.