I'm currently trying to use a bash variable to PSQL functions, I run the PSQL file in bash script like so...
psql -h *DBHOST* -p *DBPORT* -U *DBUSER* -d *DBNAME* -f ./file.sql \
-v var1=$bash_var1 -v var2=$bash_var2
In the psql file, I can access the variables through...
:'var1' and :'var2'
I tried it in a function, here is a portion of the function
CREATE OR REPLACE FUNCTION rename()
RETURNS void LANGUAGE plpgsql AS
$func$
DECLARE
title TEXT;
newTitle TEXT;
docId TEXT;
new_docId TEXT;
var1 INT;
var2 TEXT;
BEGIN
CREATE TABLE IF NOT EXISTS files_renamed (
uuid char(40) ,
name char (250),
origin char (20)
);
FOR _r IN SELECT * from table LOOP
docId := _r.id ;
title := _r.title ;
SELECT substring(docId, 1, doc_id_len) INTO new_docId;
SELECT CONCAT(new_docId,:'var2') INTO newTitle;
-- other codes --
END LOOP;
END
$func$;
But inside this PSQL function, it didn't work. It returned this error...
ERROR: syntax error at or near ":"
Does anyone have an idea on how to do this correctly? Thanks, guys.