Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL - Default value for CURRENT_SETTING

Tags:

postgresql

This is how I am accessing variable in PostgreSQL.

select CURRENT_SETTING('abc.var1')

Is there an optimal way so that I can get default value i.e. 0 in case variable doesn't exist, instead of receiving an error ?

like image 567
Pratik Avatar asked Sep 02 '25 01:09

Pratik


1 Answers

Use the two parameter version of that function (which prevents an error if the variable is missing), then use coalesce():

select coalesce(current_setting('abc.var1', true), '0');

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!